如何通过组合框值获取数据 [英] how to get data in combobox value through

查看:214
本文介绍了如何通过组合框值获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我的应用程序中有组合框,它显示了数据库中的客户名称.现在,我想获取与该客户有关的其他数据并显示在应用程序上.数据就像客户地址,手机号码,城市等.

Hey Guys,
I have combo box in my application and it shows name of customer from the database. Now, I want to fetch other data related to that customer and display on application. the data is like customer address, mobile no, city etc.

推荐答案

尝试一下,


Try this,


<asp:DropDownList AutoPostBack="true" ID="dd1" runat="server" OnSelectedIndexChanged="dd_select_changed">

       </asp:DropDownList>
       <asp:DropDownList ID="dd2" runat="server">
       </asp:DropDownList>
       <asp:Label ID="label1" runat="server"></asp:Label>
       <asp:Label ID="label2" runat="server"></asp:Label>
       <asp:Label ID="label3" runat="server"></asp:Label>





 protected void Page_Load(object sender, EventArgs e)
    {
        connection();
        string sql = "select * from Customer ";
        cmd = new SqlCommand(sql, conn);
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);

           
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dd1.DataSource = ds.Tables[0];
                dd1.DataBind();
                dd1.DataTextField = "CustomerName";//Colmn  name of custmer name in database table
                dd1.DataValueField = "CustomerId";//Colmn  name of custmer name in database table
                Label1.Text = ds.Tables[0].Rows[i]["CustomerAddress"].ToString();
            }
   }

 public void connection()
    {
        string sqlcon = System.Configuration.ConfigurationManager.AppSettings["conn"];
        conn=new SqlConnection(sqlcon);
        conn.Open();
    }

protected void dd_select_changed(object sender, EventArgs e)
    {
        connection();
        string sql = "select * from Customer where CustomerId='"+ Convert.ToInt32(dd1.SelectedItem.Value.ToString()) + "' ";
        cmd = new SqlCommand(sql, conn);
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
            Label1.Text = ds.Tables[0].Rows[0]["CustomerAddress"].ToString();
            Label2.Text = ds.Tables[0].Rows[0]["CustomerMobile"].ToString();
            Label3.Text = ds.Tables[0].Rows[0]["CustomerCity"].ToString();
    }


这篇关于如何通过组合框值获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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