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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaseem0712
    New Member
    • Jun 2013
    • 7

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

    I have a data grid view like this:



    i am loading data grid view like this:
    Code:
    Dim cd As SqlCommandBuilder = New SqlCommandBuilder(adapter)
            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)
    
            dt1 = New DataTable
            bSource = New BindingSource
            adapter.Fill(dt1) 'Filling dt with the information from the DB
            bSource.DataSource = dt1
            gv.DataSource = bSource
            gv.Columns("cid").Visible = False
            gv.Columns("dtId").Visible = False
    in update button i wrote code like this:
    Code:
    adapter = New SqlDataAdapter()
          Dim cid As Integer
            Dim dtid As Integer
            Dim cmpname As String
            Dim dtname As String
            Dim dtPhone As String
            Dim dtEmail As String
            Dim dtimage As Image
    
    
            For i As Integer = 0 To gv.RowCount - 2
               Dim rv = DirectCast(gv.Rows(i).DataBoundItem, DataRowView)
                cid = rv.Row.Field(Of Integer)("Cid")
    
                dtid = rv.Row.Field(Of Integer)("dtId")
                cmpname = rv.Row.Field(Of String)("CompanyName")
                dtname = rv.Row.Field(Of String)("Department")
                dtPhone = rv.Row.Field(Of String)("Phone")
                dtEmail = rv.Row.Field(Of String)("Email")
    
                Using ms As New MemoryStream(rv.Row.Field(Of Byte())("empimage"))
                    dtimage = New Bitmap(ms)
                End Using
    
                adapter.UpdateCommand = New SqlCommand("UPDATE CompanyMaster_tbl SET CompanyName = @CompanyName", con.connect)
                'this code for updating image also..
                adapter.UpdateCommand = New SqlCommand("update DepartmentMaster_tbl set dtName = @dtName,dtPhone = @dtPhone,dtEmail = @dtEmail,empimage=@dtimage  where dtId=@dtid", con.connect)
                adapter.UpdateCommand.Parameters.AddWithValue("@Cid", cid)
                adapter.UpdateCommand.Parameters.AddWithValue("@CompanyName", cmpname)
                adapter.UpdateCommand.Parameters.AddWithValue("@dtId", dtid)
                adapter.UpdateCommand.Parameters.AddWithValue("@dtName", dtname)
                adapter.UpdateCommand.Parameters.AddWithValue("@dtPhone", dtPhone)
                adapter.UpdateCommand.Parameters.AddWithValue("@dtEmail", dtEmail)
    
                Dim md As New MemoryStream()
                dtimage.Save(md, System.Drawing.Imaging.ImageFormat.Gif)
             ' read to end
                Dim bmpBytes As Byte() = md.GetBuffer()
    
                ' Dim image As Byte() = System.IO.File.ReadAllBytes()
                adapter.UpdateCommand.Parameters.AddWithValue("@dtimage", bmpBytes)
                'adapter.UpdateCommand.Parameters.AddWithValue("@dtimage", dtimage)
                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 Files
Working...