inserting a row to dataset

G

Guest

I've been trying to insert a record to my already existing dataset which I
have it in my session and I don't know what is going on but, I got it to
create a datatable with the new row but, when I tried to append to my
exisiting dataset, the row does not get inserted. I did step through the
code, I can see that myTable has that record and dataset has 1000 rows and it
should be 1001 after performing _testDataSet.AcceptChanges(). What am I
doing wrong here. Please help. Thanks.
Henry

Code:
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Dim _testDataSet As DataSet = Session("testDataSet")
Dim myTable As DataTable
Dim myRow As DataRow
Dim myColumn As DataColumn
Dim adapter As New SqlDataAdapter

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col1"
myTable.Columns.Add(myColumn)

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col2"
myTable.Columns.Add(myColumn)

myRow("col1") = "John"
myRow("col1") = "doe"

myTable.Rows.Add(myRow)
adapter.Update(_testDataSet)
_testDataSet.AcceptChanges()
Session("testDataSet") = _testDataSet
 
G

Guest

Thanks for the quick reply. I guess it was my fault when I was coping into
this forum, I did have that statement. Here is the revised code however
still not working:
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Dim _testDataSet As DataSet = Session("testDataSet")
Dim myTable As DataTable
Dim myRow As DataRow
Dim myColumn As DataColumn
Dim adapter As New SqlDataAdapter

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col1"
myTable.Columns.Add(myColumn)

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col2"
myTable.Columns.Add(myColumn)

myTable = New DataTable
myRow = myTable.NewRow()

myRow("col1") = "John"
myRow("col1") = "doe"

myTable.Rows.Add(myRow)
adapter.Update(_testDataSet)
_testDataSet.AcceptChanges()
Session("testDataSet") = _testDataSet



Sahil Malik said:
In this code, I don't see any statement like

myRow = new DataRow() ;

or

myRow = myTable.NewRow() ;

Maybe that is the problem?

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
G

Guest

I Think you must try to change

myRow = myTable.NewRow()

myRow("col1") = "John"
myRow("col1") = "doe"

myTable.Rows.Add(myRow)

to below code, because i have the same problem with u, before u set set
endedit to the row, the row is not updated :

myRow = myTable.NewRow()

myRow.BeginEdit()
myRow("col1") = "John"
myRow("col1") = "doe"
myRow.EndEdit()

myTable.Rows.Add(myRow)


Regards,


Raharja

Henry said:
Thanks for the quick reply. I guess it was my fault when I was coping into
this forum, I did have that statement. Here is the revised code however
still not working:
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Dim _testDataSet As DataSet = Session("testDataSet")
Dim myTable As DataTable
Dim myRow As DataRow
Dim myColumn As DataColumn
Dim adapter As New SqlDataAdapter

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col1"
myTable.Columns.Add(myColumn)

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col2"
myTable.Columns.Add(myColumn)

myTable = New DataTable
myRow = myTable.NewRow()

myRow("col1") = "John"
myRow("col1") = "doe"

myTable.Rows.Add(myRow)
adapter.Update(_testDataSet)
_testDataSet.AcceptChanges()
Session("testDataSet") = _testDataSet
 
S

Sahil Malik

Okay 2 more questions -

a) At what point does myTable get added to _testDataSet ? And how is myTable
populated i.e. if you execute the below as is - it'll probably throw an
Object Reference not Set to I... kinda error cuz myTable is never done
either a new on .. or assigned from the dataset.Tables !! ?? !!
b) What are the various commands associated with adapter.Update? (can you
paste the SQL here .. the commands can be very easily gotten by doing a
adapter.SelectCommand.CommandText .. InsertCommand.CommandText .. etc. etc.)

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik



Henry said:
Thanks for the quick reply. I guess it was my fault when I was coping
into
this forum, I did have that statement. Here is the revised code however
still not working:
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Dim _testDataSet As DataSet = Session("testDataSet")
Dim myTable As DataTable
Dim myRow As DataRow
Dim myColumn As DataColumn
Dim adapter As New SqlDataAdapter

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col1"
myTable.Columns.Add(myColumn)

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col2"
myTable.Columns.Add(myColumn)

myTable = New DataTable
myRow = myTable.NewRow()

myRow("col1") = "John"
myRow("col1") = "doe"

myTable.Rows.Add(myRow)
adapter.Update(_testDataSet)
_testDataSet.AcceptChanges()
Session("testDataSet") = _testDataSet
 
G

Guest

Sahil, after myTable.Rows.Add(myRow) gets executed, I can see the row from
command window when I type ?myTable.Rows(0).Item(0) and returns back the word
"John". The problem I'm having and maybe using adapter.Update(_testDataSet)
was not appropriate for what I'm trying to do next is to take what's already
now in dataTable (myTable) and append the row into my dataSet (_testDataSet).
Note that my dataSet already has records in it. It was done some other page
and got stored via session. To sum it up, I got it to insert into dataTable
but how can I append that row to the dataSet. Or is it not possible? Thanks
and sorry for any mix up.
 
G

Guest

Raharja, thanks for the reply. What is happening is that myTable is being
inserted with a new row, (once that line is executed, I did verified from
command line, by typing ?myTable.Rows(0).Item(0) and returns back "John")
however it is when I tried to append that row to my already existing dataSet
with records in it . My dataSet originally comes from another page and get
stored in session (has 1000 records) and now if it works, dataSet should be
1001. Thanks.
 
G

Guest

I got it to work by changing it slightly. Here is what I did to insert a row
to my dataSet..
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Dim _testDataSet As DataSet = Session("testDataSet")
Dim myTable As DataTable
Dim myRow As DataRow
Dim myColumn As DataColumn
Dim adapter As New SqlDataAdapter

myTable = New DataTable
myRow = _testDataSet.Tables(0).NewRow()

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col1"
myTable.Columns.Add(myColumn)

myColumn = New DataColumn
myColumn.DataType = "System.String"
myColumn.ColumnName = "col2"
myTable.Columns.Add(myColumn)

myRow("col1") = "John"
myRow("col1") = "doe"

_testDataSet.Tables(0).Rows.Add(myRow)
_testDataSet.AcceptChanges()
Session("testDataSet") = _testDataSet

Finally, I have one more question. What is the best approach to take in
that now that I have in my final dataSet in Session("testDataSet") to insert
it back to the database table. Any examples will be nice. Thanks.
 
S

Sahil Malik

Henry,

Could u do something like this --

Dataset ds = (Dataset)Session("yourds") ;
DataTable myTable = ds.tables[0] ;
Datarow dr = myTable.NewRow() ;
// set values on dr
myTable.Rows.Add(dr) ;

But personally speaking I'd prefer the following in decreasing order of
preference -

a) Session("dataset") ;
b) Cache("dataset") ;
c) viewstate (not dataset, but a lighter object).

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 

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