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....:(

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
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)
    {
        if (!IsPostBack)
        { 
          binddropdownlist1();

        }

     }
    protected void binddropdownlist1()
    {

        myobj.myconnection();
        SqlCommand cmd = new SqlCommand("Select * from states", myobj.sqlcon);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds, "states");
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "statename";
        DropDownList1.DataValueField = "stateid";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
        DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));

    }
    protected void Button1_Click(object sender, EventArgs e)
    {


    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int stateid = Convert.ToInt32(DropDownList1.SelectedValue);
        myobj.myconnection();
        SqlCommand cmd = new SqlCommand("select * from cities where stateid="+stateid, myobj.sqlcon);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList2.DataSource = ds;
        DropDownList2.DataTextField = "cityname";
        DropDownList2.DataValueField = "Stateid";
        DropDownList2.DataBind();

     }

}

this is my solution after some editing......and its running correctly....this is called self help....:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.