By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,649 Members | 696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,649 IT Pros & Developers. It's quick & easy.

after updating image from data grid view getting black color instead of images

P: 7
I have a data grid view like this:



i am loading data grid view like this:
Expand|Select|Wrap|Line Numbers
  1. Dim cd As SqlCommandBuilder = New SqlCommandBuilder(adapter)
  2.         adapter = New SqlDataAdapter("select c.cid,c.CompanyName,d.dtId,d.dtName as Department,d.dtPhone as Phone,d.dtEmail as Email,d.empimage from CompanyMaster_tbl c join  DepartmentMaster_tbl d on c.Cid=d.cId order by cid", con.connect)
  3.  
  4.         dt1 = New DataTable
  5.         bSource = New BindingSource
  6.         adapter.Fill(dt1) 'Filling dt with the information from the DB
  7.         bSource.DataSource = dt1
  8.         gv.DataSource = bSource
  9.         gv.Columns("cid").Visible = False
  10.         gv.Columns("dtId").Visible = False
in update button i wrote code like this:
Expand|Select|Wrap|Line Numbers
  1. adapter = New SqlDataAdapter()
  2.       Dim cid As Integer
  3.         Dim dtid As Integer
  4.         Dim cmpname As String
  5.         Dim dtname As String
  6.         Dim dtPhone As String
  7.         Dim dtEmail As String
  8.         Dim dtimage As Image
  9.  
  10.  
  11.         For i As Integer = 0 To gv.RowCount - 2
  12.            Dim rv = DirectCast(gv.Rows(i).DataBoundItem, DataRowView)
  13.             cid = rv.Row.Field(Of Integer)("Cid")
  14.  
  15.             dtid = rv.Row.Field(Of Integer)("dtId")
  16.             cmpname = rv.Row.Field(Of String)("CompanyName")
  17.             dtname = rv.Row.Field(Of String)("Department")
  18.             dtPhone = rv.Row.Field(Of String)("Phone")
  19.             dtEmail = rv.Row.Field(Of String)("Email")
  20.  
  21.             Using ms As New MemoryStream(rv.Row.Field(Of Byte())("empimage"))
  22.                 dtimage = New Bitmap(ms)
  23.             End Using
  24.  
  25.             adapter.UpdateCommand = New SqlCommand("UPDATE CompanyMaster_tbl SET CompanyName = @CompanyName", con.connect)
  26.             'this code for updating image also..
  27.             adapter.UpdateCommand = New SqlCommand("update DepartmentMaster_tbl set dtName = @dtName,dtPhone = @dtPhone,dtEmail = @dtEmail,empimage=@dtimage  where dtId=@dtid", con.connect)
  28.             adapter.UpdateCommand.Parameters.AddWithValue("@Cid", cid)
  29.             adapter.UpdateCommand.Parameters.AddWithValue("@CompanyName", cmpname)
  30.             adapter.UpdateCommand.Parameters.AddWithValue("@dtId", dtid)
  31.             adapter.UpdateCommand.Parameters.AddWithValue("@dtName", dtname)
  32.             adapter.UpdateCommand.Parameters.AddWithValue("@dtPhone", dtPhone)
  33.             adapter.UpdateCommand.Parameters.AddWithValue("@dtEmail", dtEmail)
  34.  
  35.             Dim md As New MemoryStream()
  36.             dtimage.Save(md, System.Drawing.Imaging.ImageFormat.Gif)
  37.          ' read to end
  38.             Dim bmpBytes As Byte() = md.GetBuffer()
  39.  
  40.             ' Dim image As Byte() = System.IO.File.ReadAllBytes()
  41.             adapter.UpdateCommand.Parameters.AddWithValue("@dtimage", bmpBytes)
  42.             'adapter.UpdateCommand.Parameters.AddWithValue("@dtimage", dtimage)
  43.             adapter.UpdateCommand.ExecuteNonQuery()
after updating my first 4 column of data grid view coming proper,,but image coming something like back color,,like this:



what is wrong with my code?? any help is very appreciable?
Attached Images
File Type: jpg vsave.jpg (32.4 KB, 100 views)
File Type: jpg Black c.jpg (25.0 KB, 180 views)
Jan 26 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.