1

I am working on vb.net windows application..i am populating my DataGridView like this.i wrote code in my form load event like this:

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 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
        Dim img As New DataGridViewImageColumn
        img.HeaderText = "image"
        gv.Columns.Insert(6, img)

then in cell content click i wrote code like this for uploading image:

If e.ColumnIndex = 6 Then
            Dim OFDLogo As New OpenFileDialog()
            OFDLogo.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp"
            If OFDLogo.ShowDialog() = DialogResult.OK Then
                gv.Rows(e.RowIndex).Cells(6).Value = Image.FromFile(OFDLogo.FileName)
            End If
        End If

in save button i am saving my department details like this:

For i As Integer = 0 To gv.RowCount - 2
    sqlInsertT2 = "Insert Into DepartmentMaster_tbl(dtname,dtphone,dtEmail,Cid) Values ('" + myTI.ToTitleCase(gv.Rows(i).Cells(3).Value) + "','" + gv.Rows(i).Cells(4).Value + "','" + gv.Rows(i).Cells(5).Value + "'," & Ccid & ");"
Next

i have one more field in Department master table..field Name: empimage and datatype image ..i want to save corresponding image to this table.how i can save image from my data grid view image column to database.
my DataGridView look like this:enter image description here

ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
user3106114
  • 183
  • 2
  • 11
  • 31
  • Google found me this --> http://www.codeproject.com/Articles/437937/Save-and-Retrieve-Image-from-a-SQL-Server-Database – ɐsɹǝʌ ǝɔıʌ Jan 20 '14 at 10:31
  • in that article i already checked..in that not mentioned how save image from datagrid view – user3106114 Jan 20 '14 at 10:32
  • Really? What's about the `Else` of `Button2_Click`? you can replace `p.Value = data` with your own image in this way `p.Value = gv.Rows(e.RowIndex).Cells(6).Value` – ɐsɹǝʌ ǝɔıʌ Jan 20 '14 at 10:39
  • sir,,i am not using any PictureBox1 right? so instead of this line:PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat) what i have write?? – user3106114 Jan 20 '14 at 10:45
  • Nothing. Remove these lines `Dim ms As New MemoryStream()`, `PictureBox1.BackgroundImage.Save(ms,PictureBox1.BackgroundImage.RawFormat)`, `Dim data As Byte() = ms.GetBuffer()`. They are not necessary for your purpose. The only one you need is `p.Value = gv.Rows(e.RowIndex).Cells(6).Value` – ɐsɹǝʌ ǝɔıʌ Jan 20 '14 at 10:50
  • oke sir,,i will check and let u know – user3106114 Jan 20 '14 at 10:52
  • under save button i cant give this right: e.RowIndex showing error:row index is not member of System.EventArgs – user3106114 Jan 20 '14 at 10:54
  • instead of that i given like this: p.Value = gv.Rows(i).Cells(6).Value but i am getting error in this line:'cmd.ExecuteNonQuery()' Error:Failed to convert parameter value from a Bitmap to a Byte[]. – user3106114 Jan 20 '14 at 11:04
  • I reply you on an answer – ɐsɹǝʌ ǝɔıʌ Jan 20 '14 at 11:09
  • possible duplicate of [show image in proper column of data gridview in windows application](http://stackoverflow.com/questions/21251186/show-image-in-proper-column-of-data-gridview-in-windows-application) – andy Jan 21 '14 at 07:37

1 Answers1

0

Try something like this

    Dim sql As String = "INSERT INTO Information VALUES(@name,@photo)"
    Dim cmd As New SqlCommand(sql, con)
    cmd.Parameters.AddWithValue("@name", "DepartmentName")
    Dim ms As New MemoryStream()
    Dim imgCon As New ImageConverter
    ms.Read(imgCon.ConvertTo(DataGridView1.Rows(0).Cells(4).Value, GetType(Byte())), 0, 1024)
    Dim data As Byte() = ms.GetBuffer()
    Dim p As New SqlParameter("@photo", SqlDbType.Image)
    p.Value = data
    cmd.Parameters.Add(p)
    cmd.ExecuteNonQuery()
    MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK)
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56