Skip to main content

VB.Net : Save Image to Sql Server Database in VB.Net



Store Image into SQL Database using VB.Net

Here is the steps to save image into database.I'm using picture box to show saved image in database.
Before all this create one database, and one table with the name tbl_Employee and the  following feilds
Field Name
Data Type
Empcode
int
Address
Varchar(500)
Password
Varchar(50)
Photo
image



Here is interface which is used in pick image from local system. Having picture box and two button Add Photo, Remove Photo.


Here is code for Add Photo button.


Private Sub cmdAddPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddPhoto.Click
        If dlgOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then
            pic.Image = Image.FromFile(dlgOpen.FileName)
            picFlag = "edit"
        End If
    End Sub



and code for Remove Button.

Private Sub cmdRemPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemPhoto.Click

        If MsgBox("Are you sure to remove agent photo", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm?") = MsgBoxResult.No Then Exit Sub

        pic.Image = My.Resources.noimage

        picFlag = "edit"

    End Sub

After coding all this we are going to code for save Button Event as Follow.This procedure takes image from PictureBox control named "pic" as a sql parameter.Here i use a function "ConvertImageToByte" for converting image to bytes.

            Dim sqlConn As New SqlClient.SqlConnection()
sqlConn.ConnectionString = "Password=123;Persist Security Info=True;User ID=sa;Data Source=.;initial catalog=dbEmp"

Dim SqlCmd As New SqlClient.SqlCommand

                  SqlCmd.Connection = sqlConn

Sql = "insert into tbl_Employee(Empcode,Address,Password,Photo) Values(" & _
                  "'" & CStr(EmpCode.Text) & "'," & _
                  "'" & CStr(Address.Text) & "'," & _
                  "'" & CStr(Password.Text) & "'," & _
                  "'" & Format(Now, "MM/dd/yyyy") & "'," & _
                  "@pics)"

       
        SqlCmd.CommandText = Sql

        Dim picphoto As New SqlClient.SqlParameter("@pics", SqlDbType.Image)
        picphoto.Value = ConvertImageToByte(pic.Image)
        SqlCmd.Parameters.Add(picphoto)

        DimAs Integer = SqlCmd.ExecuteNonQuery()

        If c <> 0 Then
            MsgBox("Record Saved Successfully.", MsgBoxStyle.Information,    "Message")
        End If

After successfully saving image into database it will give message for confirmation.

Comments

Popular posts from this blog

UP TET 2011 Exam Application,UPTET 2011 Result | UPTET 2011 Revised Result

UPTET 2011 Result | UPTET 2011 Revised Result | UPTET 2011 Updated Result |Board of High School and Intermediate Education Uttar Pradesh, Allahabad UP TET Advertisement 2011 and Exam Detail/Admit Card/Call Letter Download UP TET 2011 FORM & DETAILS  GET LATEST DETAIL ABOUT 72825 Primary Teacher Merit List UP TET 2011 Application form submission last date is 18-Oct-2011.Forms will be distributed from PNB bank all over UP.According to latest news Appearing B.Ed student also eligible for TET exam 2011 so they can also submit their forms to concern District.There is no requirement for Rojgar Registration No and Bank receipt within the application form.UP TET 2011 exam results will be avail on http://www.uptet2011.com . Now you can easily get Application from from the P.N.B bank Branches. UPTET 2011 Result | UPTET 2011 Revised Result | UPTET 2011 Updated Result |  Board of High School and Intermediate Education Uttar Pradesh, Al...

DataView Rowfilter With Like % in C#.Net

DataView RowFilter Syntax in C#,ASP.Net Example This example describes syntax of DataView.RowFilter expression. It shows how to correctly build expression string (without "SQL injection" ) using methods to escape values. Column names If a column name contains any of these special characters ~ ( ) # \ / = > < + – * % & | ^ ' " [ ], you must enclose the column name within square brackets [ ]. If a column namecontains right bracket ] or backslash \, escape it with backslash (\] or \\). [C#] dataView.RowFilter = "id = 10″; // no special character in column name " id " dataView.RowFilter = "$id = 10″; // no special character in column name " $id " dataView.RowFilter = "[#id] = 10″; // special character " #" in column name "#id" dataView.RowFilter = "[[id\]] = 10″; // special characters in column name " [id] " Literals String values are enclosed within single qu...

Print Div ,HTML or Table Content in Asp.net Using C#

Following is the code for printing  Div ,HTML or Table to printer using C# in Asp.net < script type ="text/javascript" language ="javascript">     function print()     {     var printFriendly = document.getElementById( "div1" )     var printWin = window.open( "about:blank" , "Voucher" , "menubar=no;status=no;toolbar=no;" );     printWin.document.write( "<html><head><title>Voucher Report</title></head><body><h1>Agent Wise Report</h1>" + printFriendly.innerHTML + "</body></html>" );     printWin.document.close();     printWin.window.print();        printWin.close();     }         </ script > add this script lines in your header section of given page. < div id ="div1"> < table ...