如何通过发送从两个drpdownlist中选择的ID从数据库中选择数据 [英] how to select data from database by sending id selected from two drpdownlist

查看:55
本文介绍了如何通过发送从两个drpdownlist中选择的ID从数据库中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我目前正在从数据库中获取数据以进行修改时遇到问题.
数据库由两个表组成:-
类别(CatId,CatName,CatDescrip)
品牌(CatId,BrandId,BName,BDescrip)

要修改品牌表,我需要列出CatId,BrandId.
我为此使用了一个下拉列表.

一个下拉列表包含类别,另一个下拉列表包含品牌.
当我选择类别时,第二个下拉列表中会填入相应的品牌.

至此,一切都很好.

但是,当我选择品牌"而不是类别"时,下拉菜单选择了已更改的索引,因此详细信息仅对应于第二个下拉菜单的第一个项目.

请帮帮我.
在页面加载中,代码行如下:

Hi,

I''m currently facing a problem while fetching data from database for modification purposes.
Database consisted of two tables:-
Category (CatId, CatName, CatDescrip)
Brand (CatId, BrandId, BName, BDescrip)

To modify brand table I am required to list CatId, BrandId.
I used a dropdown list for this.

One dropdown contains category, the other contains brand.
When I select category the corresponding brand is filled in the second dropdown.

Upto this point, all is well.

But when I select Brand rather then Category then dropdown selecteditemchanged index so detail corresponding to only for 1st item of 2nd dropdown.

Please help me.
in the page load the line of code is as:

if (!Page.IsPostBack)
        {
           
            DrpModifyBrandCategory();
        }
        DrpModifyBrand();
 public void DrpModifyBrandCategory()
    {
        try
        {
            ConnectionClass classobj = new ConnectionClass();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = classobj.GetCon();
            cmd.CommandText = "select CategoryId,CategoryName from Category";
            SqlDataReader dr = cmd.ExecuteReader();
            drpModifyBrandCategtory.Items.Clear();
            while (dr.Read())
            {
                ListItem l = new ListItem();
                l.Value = dr["CategoryId"].ToString();
                l.Text = dr["CategoryName"].ToString();
                drpModifyBrandCategtory.Items.Add(l);
            }
            dr.Close();
            cmd.Connection.Close();
        }
        catch (Exception ex)
        {
           lblBrandModify.Visible = true;
            lblBrandModify.Text = ex.Message;
        }
    
    }
    public void DrpModifyBrand()
    {
        try
        {
            ConnectionClass classobj = new ConnectionClass();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = classobj.GetCon();
            cmd.CommandText = "select BrandId,BrandName from Brand where CategoryId = " + drpModifyBrandCategtory.SelectedValue;
            SqlDataReader dr = cmd.ExecuteReader();
            drpModifyBrand.Items.Clear();
            while (dr.Read())
            {
                ListItem l = new ListItem();
                l.Value = Convert.ToString(dr["BrandId"]);   //dr["BrandId"];
                l.Text = dr["BrandName"].ToString();
                drpModifyBrand.Items.Add(l);
            }
            dr.Close();
            cmd.Connection.Close();
        }
        catch (Exception ex)
        {
            lblBrandModify.Visible = true;
            lblBrandModify.Text = ex.Message;
        }

推荐答案



据我了解的问题,我们需要绑定品牌"下拉列表,然后绑定类别"

我看到这里有错误
如果(!Page.IsPostBack)
{
DrpModifyBrandCategory();
}
DrpModifyBrand();

如果(!Page.IsPostBack)
{
DrpModifyBrandCategory();
DrpModifyBrand();
}

如果您可以发布整个代码,那么我可以找出问题所在,并在不起作用的情况下为您提供正确的答案.
Hi,

As per as i understand the problem, we need to bind the Brand dropdown list and then bind the Category

I see there is a error here
if (!Page.IsPostBack)
{
DrpModifyBrandCategory();
}
DrpModifyBrand();

if (!Page.IsPostBack)
{
DrpModifyBrandCategory();
DrpModifyBrand();
}

If you can post the entire code i can figure-out the issue and get you a right answer if this not working.


这篇关于如何通过发送从两个drpdownlist中选择的ID从数据库中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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