Dataset: NullReferenceException problem

P

postings

Hi

Can anybody help? (ASP.NET 1.1)

The event below is triggered when the user clicks an update button
column.

I am trying to update a dataset which eventually gets bound with a
datagrid, with values from that same datagrid.
Note there is no SQL code here, all the dataset data is coming from the
viewstate.

This seems to work fine, OK it's hardly efficient, and yes I should
probably be using a session variable instead.

Back to the dataset problem:

This is the offending line:
rowUpdate.CurrencyID = strCurrency

This is the error message:
[NullReferenceException: Object reference not set to an instance of an
object.]

Note if I take out the line with the error, my datagrid will be
displayed, no problems, it just won't show the updated value. So
everything appears well until I do the update.

I've spend a day looking at this and frankly I'm completely stuck!
Code is below.

Many thanks for you help!

Alex

'------------------------------------------------------------------

Private Sub MyDataGrid_Update(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
MyDataGrid.UpdateCommand

'Gets a value from a drop down list in my datagrid
Dim strCurrency As String = CType(e.Item.Cells(10).Controls(1),
DropDownList).SelectedValue
Response.Write(strCurrency) 'Yep definately got the value

Dim datasetsaved As MyDataset

'Gets dataset data previously saved from viewstate, not efficient but
works
datasetsaved = CType(viewstate.Item("Datagriddata"), MyDataset)
Response.Write("ROW TO EDIT = " & ViewState("ProductInvoiceID")) '
Shows the right row I just edited in a datagrid - definately works

Dim rowUpdate As MyDataset.MyDataTableRow =
MyDataset1.MyDataTable.FindByProductInvoiceID(ViewState("ProductInvoiceID"))


'No errors so far

rowUpdate.CurrencyID = strCurrency 'ERROR MESSAGE TRIGGERED HERE!!!

'Saved updated dataset back to the viewstate for a later time
viewstate.Remove("Datagriddata")
viewstate.Add("Datagriddata", datasetsaved)

'Populate and bind the datagrid
MyDataGrid.DataSource = datasetsaved.MyDataTable
MyDataGrid.databind

'Set datagrid to non editable
MyDataGrid.EditItemIndex = -1

End Sub
'------------------------------------------------------------------
FIN
 
E

e6matt

I would guess that rowUpdate is null, and that the line before your
error is not finding the MyDataTableRow you are looking for. Try a
if(rowUpdate != null) check before the line that is erroring, and see
if that is the problem.
 
S

Scott Allen

It would appear that FindByProductInvoiceID is not returning a row to
update. This leaves rowUpdate as a null (Nothing) reference - it
doesn't point to an an object, and so there is no CurrencyID property
to store data into.

Could you step through the method with the debugger and see why it
might not be finding a record?
 
P

postings

Sorry I was being incredibly thick:

MyDataset1.MyDataTable.FindByP­roductInvoiceID(ViewState("Pro­ductInvoiceID"))


Should be:

MyDataset1.datasetsaved.FindByP­roductInvoiceID(ViewState("Pro­ductInvoiceID"))


I was looking up the row wrong dataset (Doh!). The one without the
viewstate populated.
Sometime you look so hard you just can't see the obvious (and you write
off a day!)

Many thanks

Alex
 

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