469,578 Members | 1,843 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

Binding to deserialized objects in an arraylist !?

Hi. I'm fighting the following code:

__________________________________________________ ________________________
a. I have a class UserInfo.vb:
<Serializable()> Public Class UserInfo
Public Username As String
Public Password As String
Public Email As String
End Class

b. I have a dbTable name NewUserList, create with:
CREATE TABLE [dbo].[NewUserList] (
[user_id] [int] IDENTITY (1, 1) NOT NULL ,
[username] [varchar] (50) NOT NULL ,
[userinfo] [image] NOT NULL
)

c. I have the Serialize code (which works well):

Private Sub cmdSerializeClass_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdSerializeClass.Click

Dim objUserInfo As UserInfo
Dim objConn As New SqlConnection()
Dim objCommand As SqlCommand
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter

Try
objUserInfo = New UserInfo()
objUserInfo.Username = txtUsername.Text
objUserInfo.Email = txtEmail.Text
objUserInfo.Password = txtPassword.Text

objMemoryStream = New MemoryStream()
objBinaryFormatter = New BinaryFormatter()
objBinaryFormatter.Serialize(objMemoryStream, objUserInfo)

objConn.ConnectionString = "server=radu;database=workdb;integrated
security=sspi;"
objCommand = New SqlCommand("Insert into NewUserList (username,
userinfo) values (@username, @userinfo)", objConn
objCommand.Parameters.Add("@username", txtUsername.Text)
objCommand.Parameters.Add("@userinfo", objMemoryStream.ToArray)

objConn.Open()
If objCommand.ExecuteNonQuery > 0 Then
lblResults.Text &= "<li>" & "Succesfully added user '" &
txtUsername.Text & "' to the database...."
End If
objConn.Close()
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
Finally
objConn.Dispose()
objCommand.Dispose()
End Try
End Sub

d. I have the deserialize code, which works well up to the datagrid
databind:

Private Sub cmdDeserializeClass_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdDeserializeClass.Click

Dim objConn As New SqlConnection()
Dim objCommand As SqlCommand
Dim objDataReader As SqlDataReader
Dim arrByte() As Byte
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter
Dim objUserInfo As UserInfo
Dim colUserInfo As New ArrayList()

Try
objConn.ConnectionString = "server=radu;database=workdb;integrated
security=sspi;"
objCommand = New SqlCommand("select * from NewUserList order by
user_id", objConn)
objConn.Open()
objDataReader = objCommand.ExecuteReader

Do While objDataReader.Read
arrByte = objDataReader("userinfo")
objMemoryStream = New MemoryStream(arrByte)
objBinaryFormatter = New BinaryFormatter()
objUserInfo =
CType(objBinaryFormatter.Deserialize(objMemoryStre am), UserInfo)
colUserInfo.Add(objUserInfo)
Loop

DataGrid2.DataSource = colUserInfo
DataGrid2.DataBind()

objConn.Close()
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
Finally
objMemoryStream.Close()
objDataReader.Close()
objConn.Dispose()
objCommand.Dispose()
End Try
End Sub
__________________________________________________ ________________________

At the moment, the error I get is: "System.Web.HttpException: DataGrid with
id 'DataGrid2' could not automatically generate any columns from the
selected data source".

The problem is, you see, when I try to bind the datagrid to the arraylist.
How do I show the contents of my "userinfo" objects retrieved in the
arraylist ?

Thank you, Alex.

Thank you.
Nov 19 '05 #1
5 1120
Hi Alex:

The data binding goo looks for public properties on the objects - not
just public fields. If you can rewrite your class to have the three
attributes as actual properties you should be fine.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Sun, 20 Feb 2005 00:06:47 -0500, "Alex Nitulescu"
<RE***********************@yahoo.com> wrote:
Hi. I'm fighting the following code:

_________________________________________________ _________________________
a. I have a class UserInfo.vb:
<Serializable()> Public Class UserInfo
Public Username As String
Public Password As String
Public Email As String
End Class

b. I have a dbTable name NewUserList, create with:
CREATE TABLE [dbo].[NewUserList] (
[user_id] [int] IDENTITY (1, 1) NOT NULL ,
[username] [varchar] (50) NOT NULL ,
[userinfo] [image] NOT NULL
)

c. I have the Serialize code (which works well):

Private Sub cmdSerializeClass_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdSerializeClass.Click

Dim objUserInfo As UserInfo
Dim objConn As New SqlConnection()
Dim objCommand As SqlCommand
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter

Try
objUserInfo = New UserInfo()
objUserInfo.Username = txtUsername.Text
objUserInfo.Email = txtEmail.Text
objUserInfo.Password = txtPassword.Text

objMemoryStream = New MemoryStream()
objBinaryFormatter = New BinaryFormatter()
objBinaryFormatter.Serialize(objMemoryStream, objUserInfo)

objConn.ConnectionString = "server=radu;database=workdb;integrated
security=sspi;"
objCommand = New SqlCommand("Insert into NewUserList (username,
userinfo) values (@username, @userinfo)", objConn
objCommand.Parameters.Add("@username", txtUsername.Text)
objCommand.Parameters.Add("@userinfo", objMemoryStream.ToArray)

objConn.Open()
If objCommand.ExecuteNonQuery > 0 Then
lblResults.Text &= "<li>" & "Succesfully added user '" &
txtUsername.Text & "' to the database...."
End If
objConn.Close()
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
Finally
objConn.Dispose()
objCommand.Dispose()
End Try
End Sub

d. I have the deserialize code, which works well up to the datagrid
databind:

Private Sub cmdDeserializeClass_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdDeserializeClass.Click

Dim objConn As New SqlConnection()
Dim objCommand As SqlCommand
Dim objDataReader As SqlDataReader
Dim arrByte() As Byte
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter
Dim objUserInfo As UserInfo
Dim colUserInfo As New ArrayList()

Try
objConn.ConnectionString = "server=radu;database=workdb;integrated
security=sspi;"
objCommand = New SqlCommand("select * from NewUserList order by
user_id", objConn)
objConn.Open()
objDataReader = objCommand.ExecuteReader

Do While objDataReader.Read
arrByte = objDataReader("userinfo")
objMemoryStream = New MemoryStream(arrByte)
objBinaryFormatter = New BinaryFormatter()
objUserInfo =
CType(objBinaryFormatter.Deserialize(objMemoryStr eam), UserInfo)
colUserInfo.Add(objUserInfo)
Loop

DataGrid2.DataSource = colUserInfo
DataGrid2.DataBind()

objConn.Close()
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
Finally
objMemoryStream.Close()
objDataReader.Close()
objConn.Dispose()
objCommand.Dispose()
End Try
End Sub
_________________________________________________ _________________________

At the moment, the error I get is: "System.Web.HttpException: DataGrid with
id 'DataGrid2' could not automatically generate any columns from the
selected data source".

The problem is, you see, when I try to bind the datagrid to the arraylist.
How do I show the contents of my "userinfo" objects retrieved in the
arraylist ?

Thank you, Alex.

Thank you.


Nov 19 '05 #2
Hello Alex,

When you bind to a datagrid, you can not have AutoGenerateColumns set to
true if you are not binding to a DataTable. Set AutoGenerateColumns to false
and manually specify your columns.

--
Matt Berther
http://www.mattberther.com
Hi. I'm fighting the following code:

__________________________________________________ ____________________
____
a. I have a class UserInfo.vb:
<Serializable()> Public Class UserInfo
Public Username As String
Public Password As String
Public Email As String
End Class
b. I have a dbTable name NewUserList, create with:
CREATE TABLE [dbo].[NewUserList] (
[user_id] [int] IDENTITY (1, 1) NOT NULL ,
[username] [varchar] (50) NOT NULL ,
[userinfo] [image] NOT NULL
)
c. I have the Serialize code (which works well):

Private Sub cmdSerializeClass_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdSerializeClass.Click

Dim objUserInfo As UserInfo
Dim objConn As New SqlConnection()
Dim objCommand As SqlCommand
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter
Try
objUserInfo = New UserInfo()
objUserInfo.Username = txtUsername.Text
objUserInfo.Email = txtEmail.Text
objUserInfo.Password = txtPassword.Text
objMemoryStream = New MemoryStream()
objBinaryFormatter = New BinaryFormatter()
objBinaryFormatter.Serialize(objMemoryStream, objUserInfo)
objConn.ConnectionString =
"server=radu;database=workdb;integrated
security=sspi;"
objCommand = New SqlCommand("Insert into NewUserList
(username,
userinfo) values (@username, @userinfo)", objConn
objCommand.Parameters.Add("@username", txtUsername.Text)
objCommand.Parameters.Add("@userinfo",
objMemoryStream.ToArray)
objConn.Open()
If objCommand.ExecuteNonQuery > 0 Then
lblResults.Text &= "<li>" & "Succesfully added user '" &
txtUsername.Text & "' to the database...."
End If
objConn.Close()
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
Finally
objConn.Dispose()
objCommand.Dispose()
End Try
End Sub
d. I have the deserialize code, which works well up to the datagrid
databind:

Private Sub cmdDeserializeClass_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdDeserializeClass.Click

Dim objConn As New SqlConnection()
Dim objCommand As SqlCommand
Dim objDataReader As SqlDataReader
Dim arrByte() As Byte
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter
Dim objUserInfo As UserInfo
Dim colUserInfo As New ArrayList()
Try
objConn.ConnectionString =
"server=radu;database=workdb;integrated
security=sspi;"
objCommand = New SqlCommand("select * from NewUserList order
by
user_id", objConn)
objConn.Open()
objDataReader = objCommand.ExecuteReader
Do While objDataReader.Read
arrByte = objDataReader("userinfo")
objMemoryStream = New MemoryStream(arrByte)
objBinaryFormatter = New BinaryFormatter()
objUserInfo =
CType(objBinaryFormatter.Deserialize(objMemoryStre am), UserInfo)
colUserInfo.Add(objUserInfo)
Loop
DataGrid2.DataSource = colUserInfo
DataGrid2.DataBind()
objConn.Close()
Catch ex As Exception
lblResults.Text &= "<li>" & ex.ToString
Finally
objMemoryStream.Close()
objDataReader.Close()
objConn.Dispose()
objCommand.Dispose()
End Try
End Sub
__________________________________________________ ____________________
____

At the moment, the error I get is: "System.Web.HttpException: DataGrid
with id 'DataGrid2' could not automatically generate any columns from
the selected data source".

The problem is, you see, when I try to bind the datagrid to the
arraylist. How do I show the contents of my "userinfo" objects
retrieved in the arraylist ?

Thank you, Alex.

Thank you.


Nov 19 '05 #3
It will work, it's just the reflection code is going to look for
public properties on the objects in the array, it won't find public
fields.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Sun, 20 Feb 2005 07:05:19 -0800, Matt Berther
<mb******@hotmail.com> wrote:
Hello Alex,

When you bind to a datagrid, you can not have AutoGenerateColumns set to
true if you are not binding to a DataTable. Set AutoGenerateColumns to false
and manually specify your columns.


Nov 19 '05 #4
Hello Scott,

I learn something new everyday... Thanks!

--
Matt Berther
http://www.mattberther.com
It will work, it's just the reflection code is going to look for
public properties on the objects in the array, it won't find public
fields.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Sun, 20 Feb 2005 07:05:19 -0800, Matt Berther
<mb******@hotmail.com> wrote:
Hello Alex,

When you bind to a datagrid, you can not have AutoGenerateColumns set
to true if you are not binding to a DataTable. Set
AutoGenerateColumns to false and manually specify your columns.


Nov 19 '05 #5
Yes, Scott, that was it - it works great now !

Thank you both for your answers !
Alex.
Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ivan Demkovitch | last post: by
12 posts views Thread by Daniel Klein | last post: by
9 posts views Thread by Miro | last post: by
reply views Thread by aine_canby | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.