Click here to Skip to main content
15,915,076 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I've two dropdowns one dropdown is databound(site) and another is unbound(warehouse),
second dropdown will change the value based on 1st dropdown
I need to insert the dropdown values to my table, when i select site it is inserting into table but when i select warehouse it isn't inserting into my table.
How should i bind the warehouse dropdown value?

What I have tried:

This is my design code:
<asp:TemplateField HeaderText="Site" SortExpression="SITE">
  <EditItemTemplate>
<asp:textbox ID="TxtsiteEdit" runat="server" Text='<%# Bind("SITE") %>'></asp:textbox>
</EditItemTemplate>
                                                                   <InsertItemTemplate>
    <asp:DropDownList ID="TxtsiteInsert" runat="server" Text='<%# Bind("SITE") %>'
                                                                           TabIndex="6" AutoPostBack="True" DataSourceID="inventsite"
                                                                           DataTextField="SITEID" DataValueField="SITEID"  OnSelectedIndexChanged="sitedropdown_SelectedIndexChanged">
                                                                                                                            </asp:DropDownList>
          </InsertItemTemplate>
    <ItemTemplate>
                                                                       <asp:Label ID="TxtSite" runat="server" Text='<%# Bind("SITE") %>'></asp:Label>
          </ItemTemplate>
          </asp:TemplateField>
            <asp:TemplateField HeaderText="Warehouse" SortExpression="WAREHOUSE">
                                                                   <EditItemTemplate>
                                                                       <asp:TextBox ID="TxtWarehouseEdit" runat="server" Text='<%# Bind("WAREHOUSE") %>'></asp:TextBox>

                                                                   </EditItemTemplate>
                                                                   <InsertItemTemplate>

                                                      <asp:DropDownList ID= "TxtWarehouseInsert" runat="server" AutoPostBack="True"  >

                                                                       </asp:DropDownList>
                                                                   </InsertItemTemplate>
                  <ItemTemplate>
       <asp:Label ID="TxtWarehouse" runat="server" Text='<%# Bind("WAREHOUSE") %>'></asp:Label>
          </ItemTemplate>
     </asp:TemplateField>

This is my vb code:
Protected Sub sitedropdown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

       Dim IdTxtsiteInsert As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtsiteInsert"), DropDownList)
       Response.Cookies("sitedropdownvalue").Value = IdTxtsiteInsert.SelectedValue


       Fillwarehouse(IdTxtsiteInsert.Text)
       'Dim IdTxtWarehouseInsert As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtWarehouseInsert"), DropDownList)
       'insertwarehouse(IdTxtsiteInsert.Text, IdTxtWarehouseInsert.Text)


   End Sub
   Public Sub Fillwarehouse(ByVal siteid As String)
       'Dim IdTxtWarehouseInsert As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtWarehouseInsert"), DropDownList)
       Dim ddr As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtWarehouseInsert"), DropDownList)
       Dim strConn As String = WebConfigurationManager.ConnectionStrings("BMGINC_DYNAX09_ProdConnectionString").ConnectionString
       Dim con As New SqlConnection(strConn)
       Dim cmd As New SqlCommand()
       cmd.Connection = con
       cmd.CommandType = CommandType.Text
       cmd.CommandText = "Select InventLocationId, Name from InventLocation where InventSiteId ='" & siteid & "'"
       cmd.Parameters.AddWithValue("InventSiteId", siteid)
       Dim objDs As New DataSet()
       Dim dAdapter As New SqlDataAdapter()
       dAdapter.SelectCommand = cmd
       con.Open()
       dAdapter.Fill(objDs)
       con.Close()
       If objDs.Tables(0).Rows.Count > 0 Then
           ddr.DataSource = objDs.Tables(0)
           ddr.DataTextField = "InventLocationId"
           ddr.DataValueField = "InventLocationId"
           ddr.DataBind()
           ddr.Items.Insert(0, "-Select-")


       Else

           LblError.Text = "No Warehouse found"

       End If
   End Sub
Posted
Updated 17-Jul-18 3:40am
v2
Comments
Richard Deeming 17-Jul-18 11:28am    
cmd.CommandText = "Select InventLocationId, Name from InventLocation where InventSiteId ='" & siteid & "'"


Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

You're already adding the parameter; all you need to do is use it:
cmd.CommandText = "Select InventLocationId, Name from InventLocation where InventSiteId = @InventSiteId"
Member 13914727 18-Jul-18 5:42am    
Public Sub Fillwarehouse(ByVal siteid As String)
'Dim IdTxtWarehouseInsert As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtWarehouseInsert"), DropDownList)
Dim ddr1 As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtWarehouseInsert"), DropDownList)
Dim strConn As String = WebConfigurationManager.ConnectionStrings("BMGINC_DYNAX09_ProdConnectionString").ConnectionString
Dim con As New SqlConnection(strConn)
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select InventLocationId, Name from InventLocation where InventSiteId =@InventSiteId"
'cmd.Parameters.AddWithValue("InventSiteId", siteid)
Dim objDs As New DataSet()
Dim dAdapter As New SqlDataAdapter()
dAdapter.SelectCommand = cmd
con.Open()
dAdapter.Fill(objDs)
con.Close()
If objDs.Tables(0).Rows.Count > 0 Then
ddr1.DataSource = objDs.Tables(0)
ddr1.DataTextField = "InventLocationId"
ddr1.DataValueField = "InventLocationId"
ddr1.DataBind()
ddr1.Items.Insert(0, "-Select warehouse-")

Else

LblError.Text = "No Warehouse found"

End If
'ddr.Text = ddr1.SelectedValue

End Sub


It is showing error as must declare scalar variable
Member 13914727 18-Jul-18 6:04am    
Public Sub Fillwarehouse(ByVal siteid As String)
'Dim IdTxtWarehouseInsert As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtWarehouseInsert"), DropDownList)
Dim ddr1 As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtWarehouseInsert"), DropDownList)
Dim strConn As String = WebConfigurationManager.ConnectionStrings("BMGINC_DYNAX09_ProdConnectionString").ConnectionString
Dim con As New SqlConnection(strConn)
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select InventLocationId, Name from InventLocation where InventSiteId =@InventSiteId"
cmd.Parameters.AddWithValue("@InventSiteId", InventSiteId)
Dim objDs As New DataSet()
Dim dAdapter As New SqlDataAdapter()
dAdapter.SelectCommand = cmd
con.Open()
dAdapter.Fill(objDs)
con.Close()
If objDs.Tables(0).Rows.Count > 0 Then
ddr1.DataSource = objDs.Tables(0)
ddr1.DataTextField = "InventLocationId"
ddr1.DataValueField = "InventLocationId"
ddr1.DataBind()
ddr1.Items.Insert(0, "-Select warehouse-")


Yes done like this

1 solution

Hi,

You did not set the event (selected index change) for the second dropdown.

You just have to add it and implment your logic.

Hope it helps.
 
Share this answer
 
Comments
Member 13914727 18-Jul-18 5:02am    
How could i pass the selected value in that dropdown to my field in selected index change?
Ziee-M 18-Jul-18 5:19am    
You should also check the comment posted by Richard, your code is vulnerable to Sql injection, there are many diffrent way to achive this, by using an ORM such as EntityFramework, or search for SqlParameter to insert your parmaeters in the sql query instead of hard coding them
Member 13914727 18-Jul-18 6:04am    
Yes changed it
Ziee-M 18-Jul-18 5:14am    
First add and generate your event for the second dropdown :
<asp:DropDownList ID= "TxtWarehouseInsert" runat="server" AutoPostBack="True" OnSelectedIndexChanged="TxtWarehouseInsert_SelectedIndexChanged">
Next, in the your new event, save your data to your datbase. You can get the dropdown text/value with TxtWarehouseInsert.SelectedValue/TxtWarehouseInsert.SelectedText
Member 13914727 18-Jul-18 5:38am    
This is my insert event
Protected Sub BtnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim Idtxtsiteinsert As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtsiteInsert"), DropDownList)
Dim Idtxtwarehouseinsert As DropDownList = CType(DetailsView3.Rows(0).FindControl("TxtWarehouseInsert"), DropDownList)
warehousedropdown = Idtxtwarehouseinsert.SelectedValue
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900