Error: Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
-
Hi, I am Retrieving the binary image from sql database on web page. For that I have written following code in vb.net Dim stream As MemoryStream = New MemoryStream() Dim image As Byte() Dim bitmap As Bitmap Dim username As String username = Session("memberuser") con = New SqlConnection("server=ABC-0A55D6C3C90;database=satphere;uid=sa;pwd=") Try con.Open() cmd = New SqlCommand("select photo from ViewProfile where userid = @userid", con) cmd.Parameters.AddWithValue("@userid", username) >>>>>>>>> image = CByte(cmd.ExecuteScalar()) stream.Write(image, 0, image.Length) bitmap = New Bitmap(stream) Response.ContentType = "image/gif" 'bitmap.Save(Response.OutputStream, ImageFormat.Gif) bitmap.Save(Server.MapPath("TempImage/aa.gif")) Image1.ImageUrl = "TempImage/aa.gif" Catch ex As Exception con.Close() Stream.Close() End Try I am getting the error as Value of type 'Byte' cannot be converted to '1-dimensional array of Byte' line is mentioned by >>>>>> C#.net code for same is working fine--------------as follwos MemoryStream stream = new MemoryStream (); SqlConnection connection = new SqlConnection(@"server=ABC-0A55D6C3C90;database=Test;uid=sa;pwd="); try { connection.Open (); SqlCommand command = new SqlCommand("select Picture from Image", connection); byte[] image = (byte[])command.ExecuteScalar(); stream.Write(image, 0, image.Length); Bitmap bitmap = new Bitmap(stream); Response.ContentType = "image/gif"; bitmap.Save(Response.OutputStream, ImageFormat.Gif); //bitmap.Save(Server.MapPath("TempImage/aa.gif")); } finally { connection.Close (); stream.Close (); } can any one give me solution for this Thanks to all
-
Hi, I am Retrieving the binary image from sql database on web page. For that I have written following code in vb.net Dim stream As MemoryStream = New MemoryStream() Dim image As Byte() Dim bitmap As Bitmap Dim username As String username = Session("memberuser") con = New SqlConnection("server=ABC-0A55D6C3C90;database=satphere;uid=sa;pwd=") Try con.Open() cmd = New SqlCommand("select photo from ViewProfile where userid = @userid", con) cmd.Parameters.AddWithValue("@userid", username) >>>>>>>>> image = CByte(cmd.ExecuteScalar()) stream.Write(image, 0, image.Length) bitmap = New Bitmap(stream) Response.ContentType = "image/gif" 'bitmap.Save(Response.OutputStream, ImageFormat.Gif) bitmap.Save(Server.MapPath("TempImage/aa.gif")) Image1.ImageUrl = "TempImage/aa.gif" Catch ex As Exception con.Close() Stream.Close() End Try I am getting the error as Value of type 'Byte' cannot be converted to '1-dimensional array of Byte' line is mentioned by >>>>>> C#.net code for same is working fine--------------as follwos MemoryStream stream = new MemoryStream (); SqlConnection connection = new SqlConnection(@"server=ABC-0A55D6C3C90;database=Test;uid=sa;pwd="); try { connection.Open (); SqlCommand command = new SqlCommand("select Picture from Image", connection); byte[] image = (byte[])command.ExecuteScalar(); stream.Write(image, 0, image.Length); Bitmap bitmap = new Bitmap(stream); Response.ContentType = "image/gif"; bitmap.Save(Response.OutputStream, ImageFormat.Gif); //bitmap.Save(Server.MapPath("TempImage/aa.gif")); } finally { connection.Close (); stream.Close (); } can any one give me solution for this Thanks to all
try this code
image = CType(cmd.ExecuteScalar(),Byte())
Best Regard Pathan---------------------------------------------------
-
Hi, I am Retrieving the binary image from sql database on web page. For that I have written following code in vb.net Dim stream As MemoryStream = New MemoryStream() Dim image As Byte() Dim bitmap As Bitmap Dim username As String username = Session("memberuser") con = New SqlConnection("server=ABC-0A55D6C3C90;database=satphere;uid=sa;pwd=") Try con.Open() cmd = New SqlCommand("select photo from ViewProfile where userid = @userid", con) cmd.Parameters.AddWithValue("@userid", username) >>>>>>>>> image = CByte(cmd.ExecuteScalar()) stream.Write(image, 0, image.Length) bitmap = New Bitmap(stream) Response.ContentType = "image/gif" 'bitmap.Save(Response.OutputStream, ImageFormat.Gif) bitmap.Save(Server.MapPath("TempImage/aa.gif")) Image1.ImageUrl = "TempImage/aa.gif" Catch ex As Exception con.Close() Stream.Close() End Try I am getting the error as Value of type 'Byte' cannot be converted to '1-dimensional array of Byte' line is mentioned by >>>>>> C#.net code for same is working fine--------------as follwos MemoryStream stream = new MemoryStream (); SqlConnection connection = new SqlConnection(@"server=ABC-0A55D6C3C90;database=Test;uid=sa;pwd="); try { connection.Open (); SqlCommand command = new SqlCommand("select Picture from Image", connection); byte[] image = (byte[])command.ExecuteScalar(); stream.Write(image, 0, image.Length); Bitmap bitmap = new Bitmap(stream); Response.ContentType = "image/gif"; bitmap.Save(Response.OutputStream, ImageFormat.Gif); //bitmap.Save(Server.MapPath("TempImage/aa.gif")); } finally { connection.Close (); stream.Close (); } can any one give me solution for this Thanks to all
My knowledge of VB is rubbish, but I would say the problem is as follows: cmd.ExecuteScalar() returns object, which is supposed to be array of bytes, but your call CByte(cmd.ExecuteScalar()) is trying to convert this object into object of type byte (not array of bytes) and that causes your error. Unfortunately, I don't know correct syntax in VB for casting object into array, I hope you do (otherwise you have to google it :)) Pilo