Binding to deserialized objects in an arraylist !?

A

Alex Nitulescu

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(objMemoryStream), 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.
 
S

Scott Allen

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/


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(objMemoryStream), 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.
 
M

Matt Berther

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(objMemoryStream), 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.
 
S

Scott Allen

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.
 
A

Alex Nitulescu

Yes, Scott, that was it - it works great now !

Thank you both for your answers !
Alex.
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top