Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 14th, 2008, 01:44 AM
Authorized User
Join Date: May 2007
Posts: 95
Thanks: 5
Thanked 0 Times in 0 Posts
Default Dropdownlist throws exception

Hi All

I am making a small web application using VS 2005(using C# as a language and SQL Server as a database). In my web page, there is a dropdownlist that fetches data from table. Data is numeric in nature. I have added ProjectID as a list item in dropdownlist. It is building successfully , fetching data from table too, but upon selecting the ListItem "ProjectID" it throws a input format exception. I want that after selecting ProjectID again from the dropdown, nothing should happen.

here is the C# code which i am using

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

namespace drplist
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            SqlConnection mycn;
            SqlDataAdapter myda;
            DataSet ds;
            String strConn;
            if (!IsPostBack)
                strConn = "server=GOWRI-ITG\\SQL_IIS;Integrated Security = false;initial catalog=SAAS;user id=sa;pwd=sa;";
                mycn = new SqlConnection(strConn);
                myda = new SqlDataAdapter("Select * from ab0", mycn);
                ds = new DataSet();
                myda.Fill(ds, "Table");
                if (ds.Tables[0].Rows.Count > 0)
                    DropDownList1.DataSource = ds.Tables[0];
                    DropDownList1.DataTextField = ds.Tables[0].Columns["ProjectID"].ColumnName.ToString();
                    DropDownList1.DataValueField = ds.Tables[0].Columns["ProjectID"].ColumnName.ToString();

                    //DropDownList1.Items.Insert(0, new ListItem("Select", ""));
                    DropDownList1.Items.Insert(0, "ProjectID");


I am using two tables

            protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
                string strConn = "server=GOWRI-ITG\\SQL_IIS;Integrated Security = false;initial catalog=SAAS;user id=sa;pwd=sa;";
                SqlConnection mycn = new SqlConnection(strConn);
                SqlConnection strconn = new SqlConnection(strConn);
                SqlCommand cmd = strconn.CreateCommand();
                int intProjectId = 0;

                intProjectId = Convert.ToInt32(DropDownList1.SelectedItem.ToStrin g());

                cmd.CommandText = "SELECT a.Windows,a.Linux from ab1 a,Test1 b where a.ProjectID=b.ProjectID and a.ProjectID='" + intProjectId + "'";
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                    TextBox1.Text = rdr[0].ToString();
                    TextBox2.Text = rdr[1].ToString();


I am using two tables abo and ab1. table ab0 fetches data such as 1,2... for ProjectID
and table ab1 fetches records and puts it into textboxes.

Please help, what C# code i have to give.

Old January 14th, 2008, 02:04 PM
samjudson's Avatar
Friend of Wrox
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts

If the selected item is "ProjectID" then how do you expect the following line to work:

Convert.ToInt32(DropDownList1.SelectedItem.ToStrin g());

Try putting this in there somewhere.

if( DropDownList1.SelectedIndex == 0 ) return;

/- Sam Judson : Wrox Technical Editor -/
Old January 14th, 2008, 03:05 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

Or you could do this:



Similar Threads
Thread Thread Starter Forum Replies Last Post
Vista: Getfiles throws security error sigilaea Visual Basic 2008 Professionals 2 February 9th, 2009 06:00 PM
column name starts with a number. c# throws error. jaikanth_n ADO.NET 0 March 18th, 2006 01:44 PM
in chapter 32 class QuoteServer throws exception vit100 BOOK: Professional C#, 2nd and 3rd Editions 1 January 29th, 2006 06:31 PM
Transformer.transform throws DOM006 error saladin XML 0 October 13th, 2004 08:53 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.