Saturday, March 24, 2012

Object reference not set to an instance of an object

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!!!
RudyHi 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.
Scott
http://www.OdeToCode.com/blogs/scott/
On Thu, 17 Mar 2005 14:59:02 -0800, "Rudy"
<Rudy@.discussions.microsoft.com> wrote:

>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
>
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 wend!
Rudy
"Scott Allen" wrote:

> 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.
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
> On Thu, 17 Mar 2005 14:59:02 -0800, "Rudy"
> <Rudy@.discussions.microsoft.com> wrote:
>
>
Hi Rudy:
I have some tips here:
http://odetocode.com/Articles/116.aspx
The code is in C# but the principles remain the same.
HTH,
Scott
http://www.OdeToCode.com/blogs/scott/
On Fri, 18 Mar 2005 10:05:05 -0800, "Rudy"
<Rudy@.discussions.microsoft.com> wrote:
>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 wend!
>Rudy
>"Scott Allen" wrote:
>

0 comments:

Post a Comment