Selecting a Row in a DataTable

N

Nick

Hi

I'm very new to this so forgive me if I've made very amateur mistakes.

I am writing a web page for work so that items in a database can be queried
and then amended if required.
I've written a function which returns a DataSet.

I call this function from a sub procedure and assign the returned DataSet to
a new dataset object (dsTable) with page level scope (dim at top of page).

I then bind this data to a DataGrid control. All of this works fine.

I then want to enter the row number of an item in the DataGrid and assign a
new value to a column of the selected row.
I'm doing this very simply by adding a text box for the user to enter the
row number then to click a button to apply the changes.
The button event calls the Preview sub and passes the row number.

The way I expected this to work was

private sub Preview(intLine as Integer)

dim tSelect as DataTable


tSelect = dsTable.Tables("Orders")
tSelect.Rows(strLine).Item("Cost")=txtCos.Text '****Error here ***


dgOrders.DataSource=dsTable dgOrders.DataBind()

end sub

However at the line of code I've highlighted I get an error saying "Object
reference not set to an instance of an object."
What does this mean.
Any help would be greatly appreciated as it's driving me mad :blush:).

Thanks
 
C

Cor Ligthert

Nick,

I have the idea, that you are you handling this the same as a windowform can
you tell me if I am right in this.

Reason.
When you have webform, get the dataset, bind that to the datagrid, and let
it than go, than that dataset is not anymore in your program when it returns
with an event.

One of the things you can do is save it in a session

Cor
 
N

Nick

The line ..
tSelect.Rows(strLine).Item("Cost")=txtCos.Text '****Error here ***

actually reads..
tSelect.Rows(intLine).Item("Cost")=txtCos.Text '****Error here ***
in my code.
Sorry about that typo.
 
N

Nick

Hi Cor

The event isn't raised from the datagrid but another asp:button

My thought process was
Creat a DataSet object with page level scope.
Bind this to the DataGrid to display it.

Then change the DataSet object and bind again to show the changes.
I've just had a thought. Does the Page Level object lose it's data between
postbacks?

You will have to bear with me as this is my first real attempt at a page.
Nick
 
W

William \(Bill\) Vaughn

ASP applications do not work like Windows Forms applications in this
respect: When a DataSet is created by the web page, it is discarded when the
page returns the generated HTML to the browser. This page is reloaded when
the user clicks "Okay" or whatever to commit the changes. The contents of
the TextBox controls are passed back via the "viewstate" but you're going to
have to recreate or resurrect the DataSet (or DataTable) to save the data.
One approach is to save the DataSet/DataTable in the Session state. This
means you'll need to restore from the Session when the user posts back from
the page. At that point you'll have access to the data again.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
C

Cor Ligthert

Nick,

I am not sure from your message if you understand my message.
I've just had a thought. Does the Page Level object lose it's data between
postbacks?

Yes

Cor
 
N

Nick

Hi Cor

At first no I didn't understand but I think I do now.
And Yes I was thinking along the same lines as a windowform. Thinking that
the object variable retained it's value but it doesn't. Does it!
Nick
 

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