Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > cannot use image from picturebox with sqlserver 2000 database and disaplay image in datagrideview
 

cannot use image from picturebox with sqlserver 2000 database and disaplay image in datagrideview

1-I want insert & update and read from oledbconnection from sqlserver 2000 data base example :
Private Sub BtSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSave.Click
        Dim SqlSt As String
        If NewRec Then
            SqlSt = "INSERT INTO category (category_code,category_name,category_paper_kind_and_weight,category_color,category_cut_measurement,category_original,category_copy,category_id1,category_id2,category_picture,category_notes) VALUES (" _
           & txtcode.Text & ",'" & txtcategory_name.Text & "','" & txtcategory_paper_kind_and_weight.Text & "','" & txtcategory_color.Text & "','" & txtcategory_cut_measurement.Text & "','" & _
           txtcategory_original.Text & "','" & txtcategory_copy.Text & "','" & txtcategory_at.Text & "','" & txtcategory_to.Text & "','" & Piccategory.ImageLocation & "','" & txtcategory_notes.Text & "')"
        Else
            SqlSt = "Update category set category_code=" & txtcode.Text & ",category_name='" & txtcategory_name.Text & "' ,category_paper_kind_and_weight='" & txtcategory_paper_kind_and_weight.Text & "' ,category_color='" & txtcategory_color.Text & _
            "' ,category_cut_measurement='" & txtcategory_cut_measurement.Text & "' ,category_original='" & txtcategory_original.Text & "' ,category_copy='" & txtcategory_copy.Text & "' ,category_id1='" & txtcategory_at.Text & "' ,category_id2='" & txtcategory_to.Text & "' ,category_picture='" & Piccategory.ImageLocation & "',category_notes='" & txtcategory_notes.Text & "'where category_id=" & txtcode.Tag
        End If
        OleDbm.OleDbx.ExStatment(SqlSt, SubCon)
        LBCount.Text = OleDbm.OleDbx.ExStatment("select count(category_id) from category", SubCon)
        BtnMode(True, False)
        Me.CategoryTableAdapter.Fill(Me.Dstprinthouse.category)
    End Sub
In this Example i make on picture bold and italc how to write picturebox.what
2- how to view the picture in datagrideview but i am file data in datagrideview withe dataset
ENG.Abdelrhman_fathy  Wednesday, August 12, 2009 3:32 AM

Hi ENG.Abdelrhman_fathy,

These are my replies to the questions:

1. How to update image data to database.

We need to create a command with parameters. Then add parameters to the command. This is a code snippet to update the data source of a DataGridView to database:

'Get the data source of the DataGridView, it is a DataTable.
Dim dt As DataTable = DirectCast(DirectCast(Me.DataGridView1.DataSource, BindingSource).DataSource, DataTable)

'Create a connection.
Dim conStr As String = My.MySettings.Default.MyDBConnectionString
Dim con As SqlConnection = New SqlConnection(conStr)
'Open the connection.
con.Open()
For Each row As DataRow In dt.Rows
    'Create the update command
    'command text with parameters.
    Dim cmdTxt As String = "insert into picture(name,pic) values(@name,@pic)"
    Dim cmd As SqlCommand = New SqlCommand(cmdTxt, con)
    'name parameter.
    Dim name As SqlParameter = New SqlParameter("@name", SqlDbType.VarChar)
    name.Value = row(0)
    cmd.Parameters.Add(name)
    'image parameter.
    Dim img As SqlParameter = New SqlParameter("@pic", SqlDbType.Image)
    img.Value = row(1)
    cmd.Parameters.Add(img)
    'Execute the query.
    cmd.ExecuteNonQuery()
Next
'Close the connection.
con.Close()

This is the table structure in database:

name varchar(50) null

pic image not null

2. How to get image from database and display it in DataGridView.

These are the steps:

1) Add some columns to the DataGridView and set their DataPropertyName to the related column name in the DataTable. The column displays images needs to be of type DataGridViewImageColumn.

2) Create a DataTable and use DataAdapter to fill it.

3) Create a BindingSource and set its data source to the DataTable.

4) Set the data source of the DataGridView to this BindingSource.

This is the code snippet shows how to get data and bind it to the DataGridView:

'Create a connection.
Dim conStr As String = My.MySettings.Default.MyDBConnectionString
Dim con As SqlConnection = New SqlConnection(conStr)
'Create a DataAdapter with a select sql.
Dim adapter As SqlDataAdapter = New SqlDataAdapter("select * from picture", conStr)
'Create a table.
Dim dt As DataTable = New DataTable()
'Fill the table.
adapter.Fill(dt)
'Create a BindingSource.
Dim bindSrc As BindingSource = New BindingSource()
'Set the DataSource of the BindingSource to the DataTable.
bindSrc.DataSource = dt
'Set the DataSource of the DataGridView to the BindingSource.
Me.DataGridView1.DataSource = bindSrc


This is a similar thread:
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/3feb4bd8-dbc9-4f1e-9898-6cef7b23d7e4/.

This is the detail information about DataGridViewImageColumn:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.aspx.

Let me know if this helps.
Aland Li



Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 14, 2009 3:21 AM

Hi ENG.Abdelrhman_fathy,

These are my replies to the questions:

1. How to update image data to database.

We need to create a command with parameters. Then add parameters to the command. This is a code snippet to update the data source of a DataGridView to database:

'Get the data source of the DataGridView, it is a DataTable.
Dim dt As DataTable = DirectCast(DirectCast(Me.DataGridView1.DataSource, BindingSource).DataSource, DataTable)

'Create a connection.
Dim conStr As String = My.MySettings.Default.MyDBConnectionString
Dim con As SqlConnection = New SqlConnection(conStr)
'Open the connection.
con.Open()
For Each row As DataRow In dt.Rows
    'Create the update command
    'command text with parameters.
    Dim cmdTxt As String = "insert into picture(name,pic) values(@name,@pic)"
    Dim cmd As SqlCommand = New SqlCommand(cmdTxt, con)
    'name parameter.
    Dim name As SqlParameter = New SqlParameter("@name", SqlDbType.VarChar)
    name.Value = row(0)
    cmd.Parameters.Add(name)
    'image parameter.
    Dim img As SqlParameter = New SqlParameter("@pic", SqlDbType.Image)
    img.Value = row(1)
    cmd.Parameters.Add(img)
    'Execute the query.
    cmd.ExecuteNonQuery()
Next
'Close the connection.
con.Close()

This is the table structure in database:

name varchar(50) null

pic image not null

2. How to get image from database and display it in DataGridView.

These are the steps:

1) Add some columns to the DataGridView and set their DataPropertyName to the related column name in the DataTable. The column displays images needs to be of type DataGridViewImageColumn.

2) Create a DataTable and use DataAdapter to fill it.

3) Create a BindingSource and set its data source to the DataTable.

4) Set the data source of the DataGridView to this BindingSource.

This is the code snippet shows how to get data and bind it to the DataGridView:

'Create a connection.
Dim conStr As String = My.MySettings.Default.MyDBConnectionString
Dim con As SqlConnection = New SqlConnection(conStr)
'Create a DataAdapter with a select sql.
Dim adapter As SqlDataAdapter = New SqlDataAdapter("select * from picture", conStr)
'Create a table.
Dim dt As DataTable = New DataTable()
'Fill the table.
adapter.Fill(dt)
'Create a BindingSource.
Dim bindSrc As BindingSource = New BindingSource()
'Set the DataSource of the BindingSource to the DataTable.
bindSrc.DataSource = dt
'Set the DataSource of the DataGridView to the BindingSource.
Me.DataGridView1.DataSource = bindSrc


This is a similar thread:
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/3feb4bd8-dbc9-4f1e-9898-6cef7b23d7e4/.

This is the detail information about DataGridViewImageColumn:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.aspx.

Let me know if this helps.
Aland Li



Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 14, 2009 3:21 AM

You can use google to search for other answers

Custom Search

More Threads

• problem saving data from datagirdview
• How to export data grid data to Excel?
• Windows.form.MonthCalendar
• Visual Studio 2005 and Access 2000 query
• Getting revenge on my teacher,HELP!
• Master\Detail Form with Variation Help Required!
• 'System.Windows.Forms.DataGridViewComboBoxCell'
• Lookup fields update
• DataGridView SelectionMode
• Datagridview in a groupbox