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.DataBind();
//DropDownList1.Items.Insert(0, new ListItem("Select", ""));
DropDownList1.Items.Insert(0, "ProjectID");
}
}
else
{
}
}
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);
strconn.Open();
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.
|