hdaccess 0 Newbie Poster

Good day! This is the code I've done in saving and retrieving an image to and from a database...I have encountered an error in retrieving the image.....how can i solve this?
can u help me..thanks...hope you reply soon...

Imports System.Data
Imports System.IO
Imports MySql.Data.MySqlClient

Public Class frmMain
    Dim cnString As String = "Server=localhost;Database=picture; User=root;Password=rootwdp;"
    Dim CN As New MySqlConnection(cnString)
    Dim imgName As String
    Dim daImage As MySqlDataAdapter
    Dim dsImage As DataSet

    Private Sub FillCombo()
        Try


            CN.Open()
            daImage = New MySqlDataAdapter()
            daImage.SelectCommand = New MySqlCommand("SELECT * FROM pix", CN)
            dsImage = New DataSet("dsImage")

            daImage.Fill(dsImage)

            Dim dtable As DataTable

            dtable = dsImage.Tables(0)
            cboImageID.Items.Clear()

            For Each drow As DataRow In dtable.Rows
                cboImageID.Items.Add(drow(0).ToString())
                cboImageID.SelectedIndex = 0
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Try
            Dim dlgImage As FileDialog = New OpenFileDialog()

            dlgImage.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"

            If dlgImage.ShowDialog() = DialogResult.OK Then
                imgName = dlgImage.FileName

                Dim newimg As New Bitmap(imgName)

                imgSave.SizeMode = PictureBoxSizeMode.StretchImage
                imgSave.Image = DirectCast(newimg, Image)
            End If

            dlgImage = Nothing
        Catch ae As System.ArgumentException
            imgName = " "

            MessageBox.Show(ae.Message.ToString())
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If imgName <> "" Then
                Dim fs As FileStream

                fs = New FileStream(imgName, FileMode.Open, FileAccess.Read)

                Dim picByte As Byte() = New Byte(fs.Length - 1) {}

                fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))

                fs.Close()

                Dim CN As New MySqlConnection(cnString)

                CN.Open()

                Dim strSQL As String

                strSQL = "INSERT INTO pix(Image) values (@Img)"

                Dim imgParam As New MySqlParameter()

                imgParam.MySqlDbType = MySqlDbType.Binary
                imgParam.ParameterName = "Img"
                imgParam.Value = picByte

                Dim cmd As New MySqlCommand(strSQL, CN)

                cmd.Parameters.Add(imgParam)
                cmd.ExecuteNonQuery()

                MessageBox.Show("Image successfully saved.")

                cmd.Dispose()
                CN.Close()
                CN.Dispose()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        FillCombo()
    End Sub

    Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click
        Dim dataTable As DataTable = dsImage.Tables(0)

        If imgRetrieve.Image IsNot Nothing Then

            imgRetrieve.Image.Dispose()
        End If

        Dim fsImage As New FileStream("Image.jpg", FileMode.Create)

        For Each dataRow As DataRow In dataTable.Rows
            If dataRow(0).ToString() = cboImageID.SelectedItem.ToString() Then

               - Dim blob As Byte() = DirectCast(dataRow(1), Byte())----error enters here---' it says: CANNOT FIND COLUMN 1.

                fsImage.Write(blob, 0, blob.Length)
                fsImage.Close()
                fsImage = Nothing

                imgRetrieve.Image = Image.FromFile("Image.jpg")
                imgRetrieve.SizeMode = PictureBoxSizeMode.StretchImage
                imgRetrieve.Refresh()
            End If
        Next


    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillCombo()
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start("http://www.sourcecodester.com")
    End Sub

    Private Sub cboImageID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboImageID.SelectedIndexChanged

    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.