G
Guest
I am trying to load a droplist in VB when the edit is clicked in a datagrid.
I tried to use OnDataBinding and loading the droplist in subroutine "loaddd".
I get this error Object reference not set to an instance of an object.
Here is the code:
<asp:TemplateColumn runat="server" HeaderText="Id Type Option"
SortExpression="IdTypeOption">
<itemtemplate>
<asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
"TypeOption") %>' />
<asp:label runat="server" ID="LlbTypeOption" Visible=False Text='<%#
DataBinder.Eval(Container.DataItem, "IdTypeOption") %>'/>
</itemtemplate>
<EditItemTemplate>
<asp:dropdownlist id="deViews" OnDataBinding="loaddd"
runat="server"></asp:dropdownlist>
<asp:label runat="server" ID="IdTypeOption" Visible=False Text='<%#
DataBinder.Eval(Container.DataItem, "IdTypeOption") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
Sub loaddd(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As SqlCommand = New
SqlCommand("Get_All_TypeofOptions", myConnection)
Dim dtrControlOption As New DataTable
Dim dropRowOption As DataRow
dtrControlOption.Columns.Add( _
New DataColumn("TypeOption", GetType(String)))
dtrControlOption.Columns.Add( _
New DataColumn("IdTypeOption", GetType(String)))
myConnection.Open()
Dim Optionfile As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
While Optionfile.Read()
dropRowOption = dtrControlOption.NewRow()
dropRowOption("TypeOption") = Optionfile.Item("TypeOption")
dropRowOption("IdTypeOption") = Optionfile.Item("IdTypeOption")
dtrControlOption.Rows.Add(dropRowOption)
End While
deViews.DataSource = dtrControlOption
deViews.DataTextField = "TypeOption"
deViews.DataValueField = "IdTypeOption"
deViews.DataBind()
myConnection.Close()
BindOption()
Dim i As Integer
For i = 0 To deViews.Items.Count - 1
Dim opchk As String = deViews.Items(i).Value
Dim chk = deViews.DataValueField
If opchk = Session("typeofoption") Then
deViews.Items(i).Selected = True
End If
Next
End Sub
I tried to use OnDataBinding and loading the droplist in subroutine "loaddd".
I get this error Object reference not set to an instance of an object.
Here is the code:
<asp:TemplateColumn runat="server" HeaderText="Id Type Option"
SortExpression="IdTypeOption">
<itemtemplate>
<asp:label runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
"TypeOption") %>' />
<asp:label runat="server" ID="LlbTypeOption" Visible=False Text='<%#
DataBinder.Eval(Container.DataItem, "IdTypeOption") %>'/>
</itemtemplate>
<EditItemTemplate>
<asp:dropdownlist id="deViews" OnDataBinding="loaddd"
runat="server"></asp:dropdownlist>
<asp:label runat="server" ID="IdTypeOption" Visible=False Text='<%#
DataBinder.Eval(Container.DataItem, "IdTypeOption") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
Sub loaddd(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As SqlCommand = New
SqlCommand("Get_All_TypeofOptions", myConnection)
Dim dtrControlOption As New DataTable
Dim dropRowOption As DataRow
dtrControlOption.Columns.Add( _
New DataColumn("TypeOption", GetType(String)))
dtrControlOption.Columns.Add( _
New DataColumn("IdTypeOption", GetType(String)))
myConnection.Open()
Dim Optionfile As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
While Optionfile.Read()
dropRowOption = dtrControlOption.NewRow()
dropRowOption("TypeOption") = Optionfile.Item("TypeOption")
dropRowOption("IdTypeOption") = Optionfile.Item("IdTypeOption")
dtrControlOption.Rows.Add(dropRowOption)
End While
deViews.DataSource = dtrControlOption
deViews.DataTextField = "TypeOption"
deViews.DataValueField = "IdTypeOption"
deViews.DataBind()
myConnection.Close()
BindOption()
Dim i As Integer
For i = 0 To deViews.Items.Count - 1
Dim opchk As String = deViews.Items(i).Value
Dim chk = deViews.DataValueField
If opchk = Session("typeofoption") Then
deViews.Items(i).Selected = True
End If
Next
End Sub