Click here to Skip to main content
12,826,806 members (40,672 online)
Rate this:
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();
        ddlcity.DataSource = dt1;
        ddlcity.DataTextField = "city_name";
        ddlcity.DataValueField = "city_id";

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
Updated 30-Mar-13 7:37am
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)
Member 9581488 30-Mar-13 12:18pm
can you use break-point and see what is the value of ddlcity.selectedValue?
Rate this: bad
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.
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)
ddlstate.Items.Insert(0, "select state");
SqlDataAdapter da = new SqlDataAdapter("select * from state", con);
DataTable dt = new DataTable();
ddlstate.DataSource = dt;
ddlstate.DataTextField = "state_name";
ddlstate.DataValueField = "state_id";

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.
Rate this: bad
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);
                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();
                cmbcity.Text = "-Select-";
                cmbcity.DataSource = citydt;
                cmbcity.DisplayMember = "city_name";
                cmbcity.ValueMember = "city_id";

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

Advertise | Privacy | Mobile
Web02 | 2.8.170326.1 | Last Updated 30 May 2014
Copyright © CodeProject, 1999-2017
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