Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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
 
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 30-Mar-13 6:48am
Edited 30-Mar-13 7:37am
v2
Comments
Member 9581488 at 30-Mar-13 11:59am
   
share your sql insert statement.
Member 9671810 at 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 at 30-Mar-13 12:18pm
   
can you use break-point and see what is the value of ddlcity.selectedValue?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Member 9671810 at 30-Mar-13 13:58pm
   
how to do this sir..? i mean i am new, so i need more explanation..
S. M. Ahasan Habib at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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";
            }
  Permalink  
v2

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 30 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100