Object reference not set to an instance of an object

G

Guest

Hello All!

I get this error at this line
Line 368: Len(e.Item.Cells(5).Text) - 1)
Line 369: txtQty.Text = e.Item.Cells(5).Text
****Line 370: chkEbay.Checked =
CType(e.Item.Cells(6).FindControl("chkEbay"), _****
Line 371: CheckBox).Checked
Line 372: chkSold.Checked =
CType(e.Item.Cells(7).FindControl("chkSold"), _

It comes up when I hit the Edit button on my data grid, here is my code for
that sub. Any thoughts?

Private Sub grdUinv_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grdUinv.ItemCommand

If e.Item.ItemType = ListItemType.Pager Or _
e.Item.ItemType = ListItemType.Header Then Exit Sub
grdUinv.EditItemIndex = -1


Dim btn As Button = CType(e.CommandSource, Button)
If btn.Text = "Edit" Then
txbProductID.Text = e.Item.Cells(0).Text
txtProdName.Text = e.Item.Cells(1).Text
txtModNum.Text = e.Item.Cells(2).Text
txtSrp.Text = Microsoft.VisualBasic.Right(e.Item.Cells(3).Text, _
Len(e.Item.Cells(4).Text) - 1)
txtCost.Text = Microsoft.VisualBasic.Right(e.Item.Cells(4).Text, _
Len(e.Item.Cells(5).Text) - 1)
txtQty.Text = e.Item.Cells(5).Text
chkEbay.Checked = CType(e.Item.Cells(6).FindControl("chkEbay"), _
CheckBox).Checked
chkSold.Checked = CType(e.Item.Cells(7).FindControl("chkSold"), _
CheckBox).Checked
pnlAdd.Visible = True
Else
' DeleteItem(grdUinv.DataKeys(e.Item.ItemIndex).ToString)

End If
btnSave.CommandArgument = ""
End Sub

TIA!!!

Rudy
 
S

Scott Allen

Hi Rudy:

You'll need to step into the code with the Visual Studio debugger and
see what object is null (Nothing). I'd suspect either chkEbay is
Nothing or the reference returned by FindControl is Nothing.

One other tip: you can use FindControl on the Item object instead of a
cell (e.Item.FindControl(...)), this makes the code a bit more robust
if you ever change column ordering.
 
G

Guest

Thanks fot the tip Scott. Can you please give a quick example of the Find
CFontrol on the item object.

You were right, refrence was nothing.

Have a great weekend!

Rudy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top