AXGuy
AXGuy

Reputation: 335

cascading dropdownlist, second dropdown storing wrong value in database

I have two dropdowns Complaint_Type and Complaint_SubType. Using SqlDataSource I have populated both. SubType dropdown gets correctly populated when I choose the Type dropdown. When I select the values and press submit and check in the database both the type and subtype values are same.

.aspx code

  <asp:ScriptManager ID="smDDL" runat="server"></asp:ScriptManager>
                            <asp:UpdatePanel ID="upDDL" runat="server">
                                <ContentTemplate>
                                    <div class="form-group" style="margin-bottom: 25px;">
                                        <asp:Label ID="lblCType" runat="server" Text="Complaint Type" data-toggle="tooltip" title="Select Complaint Type" />
                                        <asp:RequiredFieldValidator ID="RfvDDLCompType" runat="server" ErrorMessage="Please Select Complaint Type" ControlToValidate="ddlCType" ForeColor="Red" InitialValue="0" Display="Dynamic"> *</asp:RequiredFieldValidator>
                                        <asp:DropDownList ID="ddlCType" runat="server" CssClass="form-control" DataSourceID="SqlDataSource1" DataTextField="Comp_Type" DataValueField="Type_ID" AutoPostBack="True" OnDataBound="DDLCTypeDataBound" Style="width: 400px">
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ComplaintWebsiteConnectionString %>" SelectCommand="SELECT * FROM [Complaint_Type]"></asp:SqlDataSource>
                                    </div>

                                    <div class="form-group" style="margin-bottom: 25px;">
                                        <asp:Label ID="lblSubType" runat="server" Text="Complaint Sub Type" data-toggle="tooltip" title="Select Complaint Sub Type" />
                                        <asp:RequiredFieldValidator ID="rfvDDlSubType" runat="server" ErrorMessage="Please Select Complaint Sub Type" ControlToValidate="ddlSubType" ForeColor="Red" InitialValue="0" Display="Dynamic"> *</asp:RequiredFieldValidator>
                                        <asp:DropDownList ID="ddlSubType" runat="server" CssClass="form-control" DataSourceID="SqlDataSource2" DataTextField="Comp_SubType" DataValueField="Type_ID" OnDataBound="DDLSubTypeDataBound" Style="width: inherit">
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ComplaintWebsiteConnectionString %>" SelectCommand="SELECT * FROM [Complaint_SubType] WHERE ([Type_ID] = @Type_ID2)">
                                            <SelectParameters>
                                                <asp:ControlParameter ControlID="ddlCType" Name="Type_ID2" PropertyName="SelectedValue" Type="Int32" />
                                            </SelectParameters>
                                        </asp:SqlDataSource>
                                    </div>
                                </ContentTemplate>
                            </asp:UpdatePanel>

.cs code

 protected void Page_Load(object sender, EventArgs e)
{

}

protected void DDLCTypeDataBound(object sender, EventArgs e)
{
    ddlCType.Items.Insert(0, new ListItem("Select", "0"));

}

protected void DDLSubTypeDataBound(object sender, EventArgs e)
{
    ddlSubType.Items.Insert(0, new ListItem("Select", "0"));
}

protected void btn_Reset(object Sender, EventArgs e)
{

}


protected void btnReport_Click(object Sender, EventArgs e)
{

        SqlConnection con = new SqlConnection(Utils.Connection);
        String query = "insert into CITIZEN_COMPLAINTS(TYPE, SUBTYPE, LOCATION, DESCRIPTION, IMAGE) values (@TYPE, @SUBTYPE, @LOCATION, @DESCRIPTION, @IMAGE)";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.Add("@TYPE", SqlDbType.Int).Value = ddlCType.SelectedValue;
        cmd.Parameters.Add("@SUBTYPE", SqlDbType.Int).Value = ddlSubType.SelectedValue;
        cmd.Parameters.Add("@LOCATION", SqlDbType.VarChar).Value = txtLoc.Text;
        cmd.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = txtDesc.Text;
        cmd.Parameters.Add("@IMAGE", SqlDbType.VarChar).Value = Utils.file_upload(fuImage);

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            //...
        }
        finally
        {
            con.Close();
        }
    }

Values stored in database

Upvotes: 1

Views: 102

Answers (1)

Sunil
Sunil

Reputation: 21396

The reason is because the DataValueField="Type_ID" is same for both drop downs in your markup for these drop downs.

For the second drop down for sub types, you should have something like DataValueField="SubType_ID".

Assuming the primary key column in Complaint_SubType table is SubType_ID, the markup below will solve your problem. Note the DataValueField in these two markups, which should be different.

Type drop down

<asp:DropDownList ID="ddlCType" runat="server" CssClass="form-control"
 DataSourceID="SqlDataSource1" DataTextField="Comp_Type" DataValueField="Type_ID" 
  AutoPostBack="True" OnDataBound="DDLCTypeDataBound" Style="width: 400px">
</asp:DropDownList>

Sub Type drop down

<asp:DropDownList ID="ddlSubType" runat="server" CssClass="form-control"
  DataSourceID="SqlDataSource2" DataTextField="Comp_SubType" DataValueField="SubType_ID"
  OnDataBound="DDLSubTypeDataBound" Style="width: inherit">
</asp:DropDownList>

Upvotes: 1

Related Questions