Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello, im just a beginner and need your help...
DROPDOWNLIST1: i have a table say "cat" with columns-id,category and categoryid... i successfully populated its value from the database.
DROPDOWNLIST2: Also have another table called "products" with columns-id,name,categoryid.i need to populate my DROPDOWNLIST2 based on categoryid i retrieve from DROPDOWNLIST1
In my code im getting an error at the below line of code
C#
int id = int.Parse(DropDownList1.SelectedValue.ToString());


here is my code
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindcatdropdown();
             
        }
    }
    protected void bindcatdropdown()
    {

        DataTable table = new DataTable();
        using (SqlConnection con = new SqlConnection(constr))
        {
            string sql = "select category,categoryid from cat ORDER BY categoryid";
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(table);
                }
            }

        }
        DropDownList1.DataSource = table;
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
       }

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       DataTable table = new DataTable();
       //int id = Convert.ToInt32(DropDownList1.SelectedValue);
       int id = int.Parse(DropDownList1.SelectedValue.ToString());
       using (SqlConnection con = new SqlConnection(constr))
       {
           string sql = "select name,categoryid from products WHERE categoryid=@cid ORDER BY categoryid";
           using (SqlCommand cmd = new SqlCommand(sql, con))
           {
               using (SqlDataAdapter da = new SqlDataAdapter(cmd))
               {
                   cmd.Parameters.AddWithValue("@cid", id);
                   da.Fill(table);
               }
           }

       }
       DropDownList2.DataSource = table;
       DropDownList2.DataBind();
   }
Posted
Updated 19-Aug-14 0:12am
v3
Comments
Dilan Shaminda 19-Aug-14 6:14am    
what is the error you are getting?
jin19 19-Aug-14 6:19am    
not error actualy... im getting an exception saying "Input string was not in a correct format." at d below line of code.
int id = int.Parse(DropDownList1.SelectedValue.ToString());
dis exception arises only when you select an item in DROPDOWNLIST1

1 solution

try it in the following manner:

C#
string cat_id=DropDownList1.SelectedValue.Tostring();
  int id;
  if(!(int.TryParse(cat_id.ToString(),out id)))
  return;


instead of
int id = Convert.ToInt32(DropDownList1.SelectedValue);


Hope it works for you. :)
 
Share this answer
 
v2
Comments
jin19 19-Aug-14 6:31am    
Wow gr8 :) Thank you sooo much!!:) but coudn't understand why u initialised
int id;------->also d condition in if statement...if you dont mind, can u explain it please!!??
i would be greatful if you could
Ashi0891 19-Aug-14 6:48am    
It states "bool int.TryParse(string s,out int result)".
Which means TryParse returns bool value so I have used if statement.
And it takes two arguements one is string and other is int("out" is used to give the result in that "int" variable) which should be defined before used in this statement.
For more reference check the below link:
http://msdn.microsoft.com/en-us/library/bb397679.aspx

Happy Coding :)
jin19 19-Aug-14 7:00am    
thnk you once again ;)
Ashi0891 19-Aug-14 7:03am    
You are welcome :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900