load a droplist in VB when the edit is clicked in a datagrid

  • Thread starter Thread starter Guest
  • Start date Start date
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 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
 
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 211: End While
Line 212:
Line 213: deViews.DataSource = dtrControlOption
Line 214: deViews.DataTextField = "TypeOption"
Line 215: deViews.DataValueField = "IdTypeOption"


Source File: C:\best\_OptionMaint.ascx.vb Line: 213
 
Once a control is in a grid you can't address it using its id but have to get
a reference to it using FindControl.

This is close to what you need to do but you will have to play around with
it a bit (its in C# sorry.

DropDownList tmpDDL = new DropDownList();
tmpDDL = e.Item.FindControl("deViews");
if (tmpDDL!=null) //here for testing mostly
{
.. . . manipulate the dataview
}

Don't forget that at the beginning of your load event you want to check if
you are on an Item or Alternating Item (hence the if (tmpDDL!=null). You
won't be able to find the DropDownList on the Header and the Footer. There
is an enum for ItemTypes you can use.


Let me know how it goes.
 
Back
Top