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

mazlina

Member
Joined
Aug 25, 2003
Messages
13
Location
malaysia
ive got this error while running my program. i have dropdownlist in my datagrid for updating my data in dbase.
this is part of my code that im using right now.......

Dim name As String = e.Item.Cells(1).Text
Dim accessDate As String = e.Item.Cells(0).Text

refrence textbox
Dim approve As String
Dim dataGridItem As DataGridItem

approve = CType(e.Item.FindControl("Label1"), DropDownList).SelectedItem.Text

updateApprove(accessDate, name, approve)

dgApproveStaff.EditItemIndex = -1
dgApproveStaff.DataSource = getStaff()
dgApproveStaff.DataBind()
 
Dim name As String = e.Item.Cells(1).Text
Dim accessDate As String = e.Item.Cells(0).Text

refrence textbox
Dim approve As String
Dim dataGridItem As DataGridItem

error occured at this point
approve = CType(e.Item.FindControl("Label1"), DropDownList).SelectedItem.Text


updateApprove(accessDate, name, approve)

dgApproveStaff.EditItemIndex = -1
dgApproveStaff.DataSource = getStaff()
dgApproveStaff.DataBind()
 
Is it possible that SelectedItem is nothing, because no item is selected?



Also, why do you have a DropDownList control called "Label1"? :p
 
value in SelectedItem is given after user select from the DropDownList right?

Label1 is my id.......

<asp:TemplateColumn HeaderText="Approval">
<ItemTemplate>
<asp:Label Width="200" Runat="server" text=<%# DataBinder.Eval(Container.DataItem, "Approval") %> ID="Label1">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:ListBox SelectionMode=Single ID="Approval" Runat="server" Rows="1" DataSource="<%# GetApproval()%>" DataTextField="Approval" DataValueField="Approval">
</asp:ListBox>
</EditItemTemplate>
</asp:TemplateColumn>
 
Can you give us the exact line the error is on? Step through your code with the debugger if you have to. It could be a number of things.. including the cryptic getStuff() method.
 
this is where the error stated :-

Source Error:


Line 131: approve = e.Item.Cells(6).Controls(0)
Line 132: approve = e.Item.FindControl("Label1")
Line 133: approve = CType(e.Item.Cells(6).Controls(0), ListBox).SelectedItem.Value
Line 134:
Line 135: approve = CType(e.Item.FindControl("Label1"), Label).Text


Source File: c:\inetpub\wwwroot\TimeManagement\WebForm5.aspx.vb Line: 133

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
TimeManagement.WebForm5.dgUserAttendance_Update(Object sender, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\TimeManagement\WebForm5.aspx.vb:133
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()

------------------------------------------
n this is my exact code

Sub dgUserAttendance_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

determine the value of the StaffID column

Dim name As String = e.Item.Cells(1).Text
Dim accessDate As String = e.Item.Cells(0).Text

refrence textbox
Dim approve As String
Dim dataGridItem As DataGridItem

approve = e.Item.Cells(6).Controls(0)
approve = e.Item.FindControl("Label1")

\***************************************
the error is in this line................

approve = CType(e.Item.Cells(6).Controls(0), ListBox).SelectedItem.Value
\***********************************
approve = CType(e.Item.FindControl("Label1"), Label).Text
CType(e.Item.FindControl("Label2"), Label).Text

updateApprove(accessDate, name, approve)

dgApproveStaff.EditItemIndex = -1
dgApproveStaff.DataSource = getStaff()
dgApproveStaff.DataBind()

End Sub

Function GetApproval()

conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\database\AttendanceReport.mdb;")
myCommandApproval = New OleDbCommand("SELECT * from TableApproval", conn)

conn.Open()

Dim approvalReader As OleDbDataReader

approvalReader = myCommandApproval.ExecuteReader

Return approvalReader
approvalReader.Close()
conn.Close()

End Function

Public Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

If (e.Item.ItemType = ListItemType.EditItem) Then
Dim drv As DataRowView

drv = e.Item.DataItem
Dim oldApproval As String
oldApproval = drv("Approval").ToString()
Dim ddl As ListBox = e.Item.FindControl("Approval")
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(oldApproval))

End If
End Sub
 
The SelectedItem must be null. Check it before you assign the Value member to approve.
if(the SelectedItem is != null) Assign!!
 
Back
Top