下拉列表的数据绑定 [英] Databinding of drop down list

查看:103
本文介绍了下拉列表的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是该领域的初学者.我对下拉列表的数据绑定有问题.问题是当我在下拉列表1中选择一个项目时,下拉列表2不显示第一个下拉列表中与所选值相关的数据.问题出在下拉列表2的数据绑定中.该怎么办,请帮助我.

I am beginner in this field. i have problem with databinding of drop down list.Problem is that when i select an item in drop down list1 then drop down list2 doesn''t show data related to selected value in 1st drop down list.problem is in databinding of drop down list2. What to do, plz help me.

  public SqlConnection conn = new SqlConnection("Data Source=.; uid=sa; password=123456; database=GD_Test");


    public void BindCountry()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * from Country", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        DDList1.DataSource = dr;
        DDList1.Items.Clear();
        DDList1.Items.Add("--Please Select country--");
        DDList1.DataTextField = "Country";
        DDList1.DataValueField = "CountryId";
        DDList1.DataBind();
        conn.Close();
    }
    public void BindState()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select State,StateID from State where CountryID='" + DDList1.SelectedValue + "", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        DDList2.DataSource = dr;
        DDList2.Items.Clear();
        DDList2.Items.Add("--Please Select State--");
        DDList2.DataTextField = "State";
        DDList2.DataValueField = "StateID";
        DDList2.DataBind();
        conn.Close();
    }
    public void BindCity()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select* from City where StateID='" + DDList2.SelectedValue + "", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        DDList3.DataSource = dr;
        DDList3.Items.Clear();
        DDList3.Items.Add("--Please Select City--");
        DDList3.DataTextField = "City";
        DDList3.DataValueField = "CityID";
        DDList3.DataBind();
        conn.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
            BindCountry();
    }
    

    protected void DDList2_SelectedIndexChanged(object sender, EventArgs e)
{
    
        BindCity();
    }



    protected void DDList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindState();

    }



以下是在``SqlDataReader dr = cmd.ExecuteReader();''附近的BindState()方法的情况下显示的错误...

字符串"101"后的引号引起来.
'101'附近的语法不正确.



Following is the error its showing in case of BindState() method near ''SqlDataReader dr = cmd.ExecuteReader();''...

Unclosed quotation mark after the character string ''101''.
Incorrect syntax near ''101''.

推荐答案


您错过了在SQL语句末尾添加单引号的操作.

使用这个
Hi,
You missed adding single quotes at the end of SQL Statements.

Use this
SqlCommand cmd = new SqlCommand("Select State,StateID from State where CountryID='" + DDList1.SelectedValue + "", conn);



代替此



Instead of this

SqlCommand cmd = new SqlCommand("Select State,StateID from State where CountryID='" + DDList1.SelectedValue + "'", conn);




它可以解决您的错误.但是您总是在语句中使用参数.




Its a work around of your error. But you always use parameters in you statements.


这篇关于下拉列表的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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