Click here to Skip to main content
15,392,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a program where i have to browse and save the image. everything is going fine but while running the program i dint get the image in the picturebox and i am not able to save it. the error occurred in "@imagedata".
here's my code...plz assist me.......
-----------------------CODE--------------------------------
VB
Imports System
Imports System.Net
Imports System.Drawing
Imports System.Collections
Imports System.Componentmodel
Imports System.windows.Forms

Imports System.Data
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Imports System.IO

Imports System.Data.SqlClient
Imports System.IO.Directory
Imports System.Threading
Public Class scan_salary
    Dim con As New SqlConnection(str)
    Private Sub btn_browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_browse.Click
        Try
            OFD1.Filter = "Jpg Files(*.Jpg)|*.Jpg" & "|Jpeg Files(*.Jpeg)|*.Jpeg" & "|Gif Files(*.Gif)|*.Gif"
            If OFD1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Label4.Text = OFD1.FileName
            Else
                Label4.Text = ""
            End If

            If Label4.Text = "" Then
                PictureBox1.Visible = False
            Else
                lbl_path.Text = Label4.Text
                Dim imgdata() As Byte = readfile(lbl_path.Text)
                Dim ms As MemoryStream = New MemoryStream(imgdata, 0, imgdata.Length)
                ms.Write(imgdata, 0, imgdata.Length)
                Dim img As Image = Image.FromStream(ms, True)
                PictureBox1.Image = img
            End If
            'PictureBox1.Image = Image.FromFile()

            lbl_path.Text = Label4.Text
            Label4.Text = Label4.Text.Remove(Label4.Text.LastIndexOf("\"))

        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub

    Public Function readfile(ByVal FPath As String) As Byte()
        Dim data() As Byte
        Try
            Dim FI As New FileInfo(FPath)
            Dim numbytes As Long = FI.Length
            Dim fstream As FileStream = New FileStream(FPath, FileMode.Open, FileAccess.Read)
            Dim br As BinaryReader = New BinaryReader(fstream)
            data = br.ReadBytes(Convert.ToInt64(numbytes))
            fstream.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

        Return data
    End Function


    Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
        If con.State <> ConnectionState.Open Then
            con.Open()


            Dim insdate, effdate As String
            insdate = Now().Date
            effdate = dtp1.Value.ToString


            Dim emp_id As String = Class2.Empid
            Dim sal As String = Class2.salary

            Dim imgdata() As Byte = readfile(lbl_path.Text)
            Dim cmd As New SqlCommand("insert into emp.dbo.scan_salary ( emp_id,salary, imagedata, insert_date, effect_date)" & _
           "values ( '" & emp_id & "', '" & sal & "',  '" & insdate & "','" & effdate & "',@imagedata", con)
            cmd.Parameters.Add(New SqlParameter("@imagedata", CType(imgdata, Object)))
            'cmd.Parameters.Add(New SqlParameter("@insert_date", DateTime.Now.ToString("yyyy-MM-dd")))
            'cmd.Parameters.Add(New SqlParameter("@effect_date", dtp1.Value.ToString("yyyy-MM-dd")))
            'cmd.Parameters.Add(New SqlParameter("@emp_id", emp_id.ToString))
            'cmd.Parameters.Add(New SqlParameter("@salary", sal.ToString.Trim))
            'cmd1.Parameters.Add(New SqlParameter("@Docimage", CType(imgdata, Object)))
            cmd.ExecuteNonQuery()
            cmd.CommandTimeout = 0

        End If
        MsgBox("document uploaded")
    End Sub
End Class
Posted
Updated 28-Mar-12 2:15am
v2

1 solution

I don't think you need the New when you are using the cmd.Parameters.Add method. You may want to research the SqlParameter[^] class a bit more.

Also, the next time you post a question, please include the exact error message that you are getting along with your code.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900