sri ranjani

sri ranjani

  • NA
  • 81
  • 4.1k

Error in Binding a value selected in dropdown to sql table

Jul 17 2018 5:55 AM
Hi all,
I need to know how to pass the value selected from an unbound dropdown to tables
I've two dropdowns one is for site and another is warehouse..
site is bounded and warehouse dropdown need to change based on the site selected change, here i can change the warehouse based on site but i am not able to pass the selected value to table
 
 
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