Disconnected Datatsets?

  • Thread starter Thread starter J
  • Start date Start date
J

J

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

ip()

If Not IsPostBack Then

DataGrid1.DataBind()

End If

End Sub

Sub ip()

Dim newrow As DataRow





SqlConnection1.Open()

SqlCommand1.Parameters("@members").Value = "1007,4200,999"

daTest.SelectCommand = SqlCommand1

daTest.Fill(dsTest, "TestTable")

Dim dvTest As New DataView(dsTest.Tables("TestTable"), "", "",
DataViewRowState.CurrentRows)

DataGrid1.DataSource = dvTest

newrow = dsTest.Tables("TestTable").NewRow

newrow(0) = "1"

newrow(1) = "Jason"

newrow(2) = "1"

dsTest.Tables("TestTable").Rows.Add(newrow)

dsTest.AcceptChanges()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim newrow As DataRow

newrow = dsTest.Tables("TestTable").NewRow

newrow(0) = "2"

newrow(1) = "Jason"

newrow(2) = "1"

dsTest.Tables("TestTable").Rows.Add(newrow)

dsTest.AcceptChanges()

DataGrid1.DataBind()

End Sub



When I click the button, I would have thought that the datagrid would keep
increasing with the values "2,", "Jason", "1", but it doesn't.

I believe I have what is called a disconnected dataset, and I need the user
to work on a dataset before they post the final values to the server, hence
why I'm not using a SQL Adapter.

Can someone PLEASE explain why the above won't work, and how to work with
disconnected datasets.

TIA
 
J,

Every time you load your page the dataset is being recreated. You need to
either first save the new row to the database or only get the dataset the
first page load and then store it in memory (a session variable or the like)
between page views.

Since the web is a stateless environment (all objects are destroyed after
rendering a page) you have to take measures to store the objects you need to
remain between posts.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
A session variable!!

How would I store the dataset as a session variable?

Would it be simply session("Temp")=dsTest.tables("TestTable")

TIA
 
J,

You have to both store the datatable in the session variable and get it out
again when you go to use it.

Change your code to this: (I've put '********* above the new lines of code.)

'Put user code to initialize the page here

ip() '*******Move this (It should only fire the first page load)

dsTest = Session("Temp") '***********Move this (It should only be done on
post back)

If Not IsPostBack Then

ip() '*******Moved here.

DataGrid1.DataBind()

Else

dsTest = Session("Temp")

End If

End Sub

Sub ip()

Dim newrow As DataRow

SqlConnection1.Open()

SqlCommand1.Parameters("@members").Value = "1007,4200,999"

daTest.SelectCommand = SqlCommand1

daTest.Fill(dsTest, "TestTable")

Dim dvTest As New DataView(dsTest.Tables("TestTable"), "", "",
DataViewRowState.CurrentRows)

DataGrid1.DataSource = dvTest

newrow = dsTest.Tables("TestTable").NewRow

newrow(0) = "1"

newrow(1) = "Jason"

newrow(2) = "1"

dsTest.Tables("TestTable").Rows.Add(newrow)

dsTest.AcceptChanges()

Session("Temp") = dsTest '*********Add the datatable to the session

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim newrow As DataRow

newrow = dsTest.Tables("TestTable").NewRow

newrow(0) = "2"

newrow(1) = "Jason"

newrow(2) = "1"

dsTest.Tables("TestTable").Rows.Add(newrow)

dsTest.AcceptChanges()

Session("Temp") = dsTest.Tables("TestTable")

DataGrid1.DataBind()

End Sub

Sincerely,
--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
Well, I've changed the code accordingly, it now looks like...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

If Not IsPostBack Then

ip()

DataGrid1.DataBind()

Else

dsTest = Session("Temp")

End If

End Sub

Sub ip()

Dim newrow As DataRow

SqlConnection1.Open()

SqlCommand1.Parameters("@members").Value = "1007,4200,9999"

daTest.SelectCommand = SqlCommand1

daTest.Fill(dsTest, "TestTable")

Dim dvTest As New DataView(dsTest.Tables("TestTable"), "", "",
DataViewRowState.CurrentRows)

DataGrid1.DataSource = dvTest

newrow = dsTest.Tables("TestTable").NewRow

newrow(0) = "1"

newrow(1) = "Jason"

newrow(2) = "1"

dsTest.Tables("TestTable").Rows.Add(newrow)

dsTest.AcceptChanges()

Session("Temp") = dsTest

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim newrow As DataRow

newrow = dsTest.Tables("TestTable").NewRow

newrow(0) = "2"

newrow(1) = "Jason"

newrow(2) = "1"

dsTest.Tables("TestTable").Rows.Add(newrow)

dsTest.AcceptChanges()

Session("Temp") = dsTest.Tables("TestTable")

DataGrid1.DataBind()

End Sub



When I run, and click the button, the datagrid disappears.

When I click it a second time, I'm told ...



Specified cast is not valid.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:



Line 49: DataGrid1.DataBind()
Line 50: Else
Line 51: dsTest = Session("Temp")
Line 52: End If
Line 53:




Source File: c:\inetpub\wwwroot\hbitl_umc\test.aspx.vb Line: 51
 
J,

It's because the datasource of your table is actually the dataview. Inside
of the button click's event handler you need to recreate the dataview and
set it as the datasource of the grid before you rebind the grid.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
Back
Top