Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
SqlDataAdapter da1 = new SqlDataAdapter("select * from city where state_id='" + ddlstate.SelectedValue + "'", con);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
        ddlcity.DataSource = dt1;
        ddlcity.DataTextField = "city_name";
        ddlcity.DataValueField = "city_id";
        ddlcity.DataBind();

above code is to select city for particular state...

and the below code is to insert city_id into the tabel

SQL
cmd.Parameters.AddWithValue("@state_id", ddlstate.SelectedValue);
    cmd.Parameters.AddWithValue("@city_id", ddlcity.SelectedValue);


but when i insert.. city_id is same as state_id
eg. if i select state having id=1, and city_id having id=3 then...
state_id gets inserted as 1 and also city_id as 1...
so plz help me to solve this..
Posted
Updated 30-Mar-13 6:37am
v2
Comments
Member 9581488 30-Mar-13 11:59am    
share your sql insert statement.
Member 9671810 30-Mar-13 12:12pm    
insert into registration (firstname,lastname,username,password,state_id,city_id,pincode,mobile_no,phone_no,e_mail,address,security_ques,security_ans,reg_date)
values(@firstname,@lastname,@username,@password,@state_id,@city_id,@pincode,@mobile_no,@phone_no,@e_mail,@address,@security_ques,@security_ans,getdate())
Member 9581488 30-Mar-13 12:18pm    
can you use break-point and see what is the value of ddlcity.selectedValue?

Your insert sql code and data binding code is completely alright. What your problem is Your Event handler code. When you change your State you should handle ddlState (state drop down) selected index change event. You should make sure when State is changed then in the same time your above city dropdown list changed code need to fire. When you change your state then debug your code and make sure ddlCity dropdown repoulated and after user selection city and press button then debug and see the ddlCity.SelectedValue and make sure it is showing the last changed value.
 
Share this answer
 
Comments
Member 9671810 30-Mar-13 13:58pm    
how to do this sir..? i mean i am new, so i need more explanation..
S. M. Ahasan Habib 30-Mar-13 14:13pm    
Show you code where you handle selected index change event, where you handle page load event and save event. Need to see where you bind your ddlState dropdown. My guess is when you press save button at the moment ddlState dropdown may be rebind if you not use page.IsPostBack property.
Member 9671810 31-Mar-13 0:48am    
i have binded ddlstate in page load..

if (!IsPostBack)
{
{
//state
ddlstate.Items.Insert(0, "select state");
SqlDataAdapter da = new SqlDataAdapter("select * from state", con);
DataTable dt = new DataTable();
da.Fill(dt);
ddlstate.DataSource = dt;
ddlstate.DataTextField = "state_name";
ddlstate.DataValueField = "state_id";
ddlstate.DataBind();
}

and ddlcity in ddlstate's selected index change..

and the i have chkboxes having autopost back on..

and do i have to bind ddlcity also in page load even.. or is it ok to bind in ddlstate's selected index chaged...?
S. M. Ahasan Habib 31-Mar-13 1:39am    
You should bind ddlCity in ddlState selected index changed event not page load event. Now from the save button click event you set break point to ddlState.SelectedValue and ddlCity.SelectedValue and see its value as the expected value found there or not.
C#
if (cmbstate.SelectedValue.ToString() == null)
            {
                MessageBox.Show("Please select proper state", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                int id;
                bool paresOK = Int32.TryParse(cmbstate.SelectedValue.ToString(), out id);
                SqlDataAdapter cityda = new SqlDataAdapter("select * from city where state_id = '" + id + "' ", con);
                DataTable citydt = new DataTable();
                cityda.Fill(citydt);
                cmbcity.Text = "-Select-";
                cmbcity.DataSource = citydt;
                cmbcity.DisplayMember = "city_name";
                cmbcity.ValueMember = "city_id";
            }
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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