473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1205
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: berg | last post by:
All, I am binding an ArrayList full of custom objects to an DataGrid. The property in the custom class are all public. I define a DataGridTableStyle and set the MappingName to "ArrayList". I...
0
by: Terentius Neo | last post by:
I have binded a DataGrid to a custom hierarchy. It is an ArrayList of objects. Each object has a public property returning a new ArrayList of sublevel objects (which may again have sublevels of...
2
by: Ivan Demkovitch | last post by:
Hi! I wonder if thats a bug or something: I have an object which has arrayList of child objects exposed to clients. This child object also exposed to clients. Class A { public ArrayList ...
3
by: Jim Bancroft | last post by:
Hi everyone, I'm binding an ArrayList to a DataGrid for the first time (I'm used to binding DataSets and DataTables) and I was wondering if I could somehow "name" the ArrayList, so that I can...
4
by: Alan Silver | last post by:
Hello, I'm trying to use an ArrayList to do data binding, but am getting an error I don't understand. I posted this in another thread, but that was all confused with various other problems,...
12
by: Daniel Klein | last post by:
I think I've done my homework and checked around on Google Groups but this seems to be a situation not yet covered. Here is the scenario... There are two classes, Foo and Bar (actually there...
9
by: Jaybuffet | last post by:
my aspx has something like this <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <mycontrol:ctl id="ctlId" obj='<%# Container.DataItem %>' showItem="true"/> </ItemTemplate>...
9
by: Miro | last post by:
VB 2003 and Im still new to vb, so i hope i can explain this as best I can. I have a variable defined as such: ( simple example ) Dim AVariableOfSorts(,) As Object = _ { _ {"Last", "String",...
0
by: aine_canby | last post by:
Hi all, I have the following simple class which I use to store a set of lab objects: /// <summary> /// Represents a Collection of Labs /// </summary> public class CLabCollection {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.