i am trying to make application in which i used two dropdown list which gets its value from database and one shows states and another shows its corresponded cities ...and code for this is
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
dbclass myobj =new dbclass();
SqlConnection sqlcon=new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
myobj.myconnection();
SqlCommand cmd = new SqlCommand("select * from states", myobj.sqlcon);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ListItem li = new ListItem();
li.Value = reader["stateid"] as string;
li.Text = reader["statename"] as string;
DropDownList1.Items.Add(li);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
myobj.myconnection();
SqlCommand cmd2 = new SqlCommand("select * from cities where stateid='" + Convert.ToInt32(DropDownList1.SelectedValue)+"'", myobj.sqlcon);
SqlDataReader reader2=cmd2.ExecuteReader();
while(reader2.Read())
{
ListItem li2=new ListItem();
li2.Value=reader2["stateid"]as string;
li2.Text=reader2["cityname"]as string;
DropDownList2.Items.Add(li2);
}
}
but i am getting this error "Input string was not in a correct format."
plz help me....:(