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

10
  • Google found me this --> codeproject.com/Articles/437937/… Commented Jan 20, 2014 at 10:31
  • in that article i already checked..in that not mentioned how save image from datagrid view Commented Jan 20, 2014 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 Commented Jan 20, 2014 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?? Commented Jan 20, 2014 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 Commented Jan 20, 2014 at 10:50

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

Error is coming in this line:ms.Read(gv.Rows(0).Cells(6).Value, 0, 1024) Error is:Unable to cast object of type 'System.Drawing.Bitmap' to type 'System.Byte[]'.
sir i will check and let u know
You can convert the image to Byte array using something like this: stackoverflow.com/questions/12961144/…
sir,,what happened with your function ..if i do that i wont get answer?
earlier your wrote some function name :imageToByteArray..i cant do that way?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.