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