选中的city_id未插入 [英] selected city_id is not getting inserted

查看:82
本文介绍了选中的city_id未插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();



以上代码是为特定州选择城市...



和下面的代码是将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);





但是当我插入.. city_id 相同state_id

例如。如果我选择 id = 1 city_id id = 3 然后......

state_id 插入为 1 并且city_id为 1 ...

所以plz帮我解决这个问题..



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

推荐答案

你的插入sql代码和数据绑定代码完全没问题。您的问题是您的事件处理程序代码。更改状态时,应处理ddlState(状态下拉列表)选定的索引更改事件。您应确保何时更改状态,然后在上述城市下拉列表更改代码需要触发的同时。当您更改状态然后调试代码并确保重新调整ddlCity下拉列表并在用户选择city并按下按钮后再调试并查看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.


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


这篇关于选中的city_id未插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆