在下拉列表中获取所选值的ID

时间:2014-11-18 10:21:26

标签: c# sql asp.net drop-down-menu

我有两个下拉列表,用于获取Category和Subcategory。场景是两个值都来自单个表。我可以在第一个下拉列表中获取类别。 我想要的是选择任何类别,CategoryName的CategoryID也应该成为SubCategory的ParentID。 请参阅在DDL中调用类别的代码。: -

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * from dbo.CategoriesForMerchant where ParentId is null", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        ddlCategories.DataSource = dr;
        ddlCategories.Items.Clear();
        ddlCategories.DataTextField = "CategoryName";
        ddlCategories.DataValueField = "CategoryId";
        ddlCategories.DataBind();
        ddlCategories.Items.Insert(0, new ListItem("--Select Category--", "0"));
        conn.Close();
    }
}
protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString);
    conn.Open();
    string value = "";
    if (!String.IsNullOrEmpty(ddlCategories.SelectedValue.ToString()))
    {
        value = ddlCategories.SelectedValue.ToString();
    }
    SqlCommand cmd = new SqlCommand("Select * from CategoriesForMerchant where ParentId ='" + value + "'", conn);
    SqlDataReader dr = cmd.ExecuteReader();
    ddlSubCategories.DataSource = dr;
    ddlSubCategories.Items.Clear();
    ddlSubCategories.DataTextField = "CategoryName";
    ddlSubCategories.DataValueField = "CategoryName";
    ddlSubCategories.DataBind();
    ddlSubCategories.Items.Insert(0, new ListItem("--Select Sub Category--", "NA"));
    conn.Close();
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand("Insert into CategoriesForMerchant (CategoryName) values (@CategoryName)", conn);
    cmd.Parameters.AddWithValue("@CategoryName", txtCategory.Text);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    Response.Write("<script>alert('File uploaded successfully');</script>");
}

另请参阅下拉列表的HTML代码: -

<asp:TextBox ID="txtCategory" runat="server" placeholder="Add category"></asp:TextBox>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="110" OnClick="btnSubmit_Click" />

                        <br />

                        <asp:TextBox ID="txtSubCategory" runat="server" placeholder="Add subcategory"></asp:TextBox>
                        <asp:Button ID="btnSubCategory" runat="server" Text="Submit" Width="110" OnClick="btnSubCategory_Click" />

另外,请参阅我的表格结构: -

CREATE TABLE [dbo].[categoriesformerchant] 
( 
 id     INT IDENTITY(1, 1) NOT NULL, 
 NAME   NVARCHAR(50) NOT NULL, 
 ParentId INT NULL, 
 CONSTRAINT [pk_CategoriesForMerchant ] PRIMARY KEY CLUSTERED (id ASC) 
) 

go 

ALTER TABLE [dbo].[categoriesformerchant] 
  WITH CHECK ADD CONSTRAINT [fk_subcategories] FOREIGN KEY(ParentId) REFERENCES 
  [dbo].[categoriesformerchant] ([id]) 

go 

ALTER TABLE [dbo].[categoriesformerchant] 
  CHECK CONSTRAINT [fk_subcategories] 

go 

2 个答案:

答案 0 :(得分:1)

对于添加已引用外键的Id,我需要添加

cmd.Parameters.Add("@ParentId",  ddlCategories.SelectedValue);

另请参阅整个代码供您参考: -

 <asp:TextBox ID="txtSubCategory" runat="server" placeholder="Add sub category"></asp:TextBox>

按钮代码: -

 <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="110" OnClick="btnSubmit_Click" />

另请参阅代码隐藏,resloving中的主要内容: -

protected void btnSubmit_Click(object sender, EventArgs e)
{   
    SqlCommand cmd = new SqlCommand("Insert into CategoriesForMerchant (CategoryName, ParentId) values (@CategoryName, @ParentId)", conn);
    cmd.Parameters.AddWithValue("@CategoryName", txtSubCategory.Text);
    cmd.Parameters.Add("@ParentId", ddlCategories.SelectedValue);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    Response.Write("<script>alert('File uploaded successfully');</script>");
    txtSubCategory.Text = "";
}

答案 1 :(得分:0)

ParentId is Int value but Your passing at String so you got error convert to int and then you try


protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("Select * from CategoriesForMerchant where ParentId =" + Convert.ToInt32(ddlcategories.SelectedValue) + "", conn);
    SqlDataReader dr = cmd.ExecuteReader();

    if (dr.HasRows())
    {
    ddlSubCategories.DataSource = dr;
    ddlSubCategories.Items.Clear();
    ddlSubCategories.DataTextField = "CategoryName";
    ddlSubCategories.DataValueField = "CategoryName";
    ddlSubCategories.DataBind();
    ddlSubCategories.Items.Insert(0, new ListItem("--Select Sub Category--", "NA"));
    conn.Close();
    dr.Close();
    }
}