|
-
Mar 3rd, 2004, 06:42 PM
#1
Thread Starter
Lively Member
Storing data from a form to a DataTable object in a Session Variable
I have another issue..
I have created a DataTable object in a first page into a dataset and into a Session variable. I have filled this table with data from a form in that page, and runs fine. Now on the next page I want to store more data in the same DataTable and Session. I have writed the code on the next page in order to do this, but when I load the page and I trigger the subroutine nothing is stored into the table (the new columns with its new values-row) in the current session. I don' t understand why, the code on the second page is thus:
Code:
Sub myData(Sender As Object, e As EventArgs)
Dim ds As DataSet
ds = CType(Session("UserData"), DataSet)
ds.Tables(0).columns.add("newColumn1")
ds.Tables(0).columns.add("newColumn2")
...
Dim row As DataRow
row = ds.Tables(0).NewRow
row.Item("newcolumn1") = control1.Text
row.Item("newColumn2") = control2.SelectedItem.Value
...
ds.Tables(0).Rows.Add(row)
End Sub
When I try to show one of this new fields created on the next page thus:
Code:
Dim UserDS As Data.DataSet = Session("UserData")
control.Text = UserDS.Tables(0).rows(0).Item("newColumn1")
an error message appears saying:
System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid.
Line 108: control.Text = UserDS.Tables(0).rows(0).Item(“newColumn1")
And I entered text in the ‘control1’ text field.
Somebody knows what it lacks?
Thanks
-
Mar 3rd, 2004, 07:59 PM
#2
PowerPoster
Yes, I see something lacking. I see no where in the code you provided where you are setting the session variable equal to the dataset, try this:
VB Code:
Sub myData(Sender As Object, e As EventArgs)
Dim ds As DataSet
ds = CType(Session("UserData"), DataSet)
ds.Tables(0).columns.add("newColumn1")
ds.Tables(0).columns.add("newColumn2")
...
Dim row As DataRow
row = ds.Tables(0).NewRow
row.Item("newcolumn1") = control1.Text
row.Item("newColumn2") = control2.SelectedItem.Value
...
ds.Tables(0).Rows.Add(row)
'ADDED:
Session("UserData") = ds
End Sub
When you grab something from the session object, you are obtaining a ByVal of it. It seems as if you are trying to act on it as if you had a reference to the object. You have a copy of it, and if you want your changes to be populated back to the session object, you will need to reassign the session object the value again.
-
Mar 4th, 2004, 05:15 AM
#3
Thread Starter
Lively Member
Hi !,
I had tried this possibility and it did the same, now too.
I do not know what is wrong… When the user clicks the button, ‘myData’ subroutine is triggered, and the data entered in the form would be stored in the DataTable object, in the correct new column and new row of the same DataTable -Table(0)- created on the first page. But 'null value' is stored in all the new columns, but the data stored on the first page, where the table was created, there are data and I can see/show them.
First Page:
Code:
Sub myData()
Dim ds As New DataSet("UserData")
Dim dt As New DataTable("NewUser")
dt.columns.add("Mail")
dt.columns.add("Password")
...
Dim row As DataRow
row = dt.NewRow()
row.Item("Mail") = mail.Text
row.Item("Password") = password.Text
...
dt.Rows.Add(row)
ds.tables.add(dt)
Session("UserData") = ds
End Sub
The name of the subroutine don’ t have the parameters ‘(Sender As Object, e As EventArgs)’ because is called from another subroutine in this way ‘Call myData()’.
Second Page:
Code:
Sub myData(Sender As Object, e As EventArgs)
Dim ds As DataSet
ds = CType(Session("UserData"), DataSet)
ds.Tables(0).columns.add("column1")
ds.Tables(0).columns.add("column2")
...
Dim row As DataRow
row = ds.Tables(0).NewRow()
row.Item(“column1") = control1.Text
row.Item("column2") = control2.SelectedItem.Value
...
ds.Tables(0).Rows.Add(row)
Session("UserData") = ds
response.Redirect("third_form_page.aspx")
End Sub
-
Mar 4th, 2004, 06:30 AM
#4
Thread Starter
Lively Member
YES !! I have solved the problem .
I have seen that the name of the original DataTable is ("NewUser") and not (0) as I referred it on the second page.
The other mistake was that I created another row on the second page, and I didn’ t need to add a new one, simply continue adding on the same row, so there was to write
‘row = ds.Tables(0).Rows(0)’ instead of ‘row = ds.Tables(0).NewRow()’.
Now I create all the table on the first page, and on the second I only continue filling the row created on the first page with the new data for the rest of the columns.
Now all runs fine. But now I have a little doubt in my mind, Why I need to define the data type of the DataTable columns? Thus:
Code:
dt.columns.add("Mail", System.Type.GetType("System.String"))
I think that I don’ t need to define it because the form controls (fields) are well defined all with their data type (textbox, ddl (DataValueField),..), and I have validators as well. And once reaches to the DataTable all the values are correct. Maybe the problem would appear in some inserts into the database, but now the insert that I do with 17 fields of this DataTable works fine.
What do you know about the necessity of defining the data type on the DataTable columns?
Thank you VM,
Cesar
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|