<%@ Page Language="vb" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Import Namespace="System.IO" %>

<HTML>

  <HEAD>

    <title>Inserting Image to a SqlServer</title>

   

    <script runat=server>

                        Public Sub AddPerson(sender As Object, e As EventArgs)

 

                                    Dim intImageSize As Int64

                                    Dim strImageType As String

                                    Dim ImageStream As Stream

 

                                    ' Gets the Size of the Image

                                    intImageSize = PersonImage.PostedFile.ContentLength

                                   

                                    ' Gets the Image Type

                                    strImageType = PersonImage.PostedFile.ContentType

                                   

                                    ' Reads the Image

                                    ImageStream = PersonImage.PostedFile.InputStream

 

                                    Dim ImageContent(intImageSize) As Byte

                                    Dim intStatus As Integer

                                    intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

 

                                    ' Create Instance of Connection and Command Object

                                    Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

                                    Dim myCommand As New SqlCommand("sp_person_isp", myConnection)

 

                                    ' Mark the Command as a SPROC

                                    myCommand.CommandType = CommandType.StoredProcedure

 

                                    ' Add Parameters to SPROC

                                    Dim prmEmail As New SqlParameter("@PersonEmail", SqlDbType.VarChar, 255)

                                    prmEmail.Value = txtPersonEmail.Text

                                    myCommand.Parameters.Add(prmEmail)

 

                                    Dim prmName As New SqlParameter("@PersonName", SqlDbType.VarChar, 255)

                                    prmName.Value = txtPersonName.Text

                                    myCommand.Parameters.Add(prmName)

 

                                    Dim prmSex As New SqlParameter("@PersonSex", SqlDbType.Char, 1)

                                    If sexMale.Checked Then

                                                prmSex.Value = "M"

                                    Else

                                                prmSex.Value = "F"

                                    End If

                                    myCommand.Parameters.Add(prmSex)

 

                                    Dim prmPersonDOB As New SqlParameter("@PersonDOB", SqlDbType.DateTime)

                                    prmPersonDOB.Value = txtPersonDob.Text

                                    myCommand.Parameters.Add(prmPersonDOB)

 

                                    Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)

                                    prmPersonImage.Value = ImageContent

                                    myCommand.Parameters.Add(prmPersonImage)

 

                                    Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)

                                    prmPersonImageType.Value = strImageType

                                    myCommand.Parameters.Add(prmPersonImageType)

 

                                    Try

                                                myConnection.Open()

                                                myCommand.ExecuteNonQuery()

                                                myConnection.Close()

                                                Response.Write("New person successfully added!")

                                    Catch SQLexc As SqlException

                                                Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())

                                    End Try

                        End Sub   

   

    </script>

   

  </HEAD>

  <body style="font: 10pt verdana">

 

    <form enctype="multipart/form-data" runat="server">

            <asp:Table Runat=server Width=50% BorderWidth=1 BackColor=Beige>

                        <asp:TableRow>

                                    <asp:TableCell ColumnSpan=2 BackColor="#ff0000">

                                    <asp:Label Font-Name="verdana" Font-size="12px" ForeColor="#ffffff" font-bold="True" Runat=server Text="Add New Person" />

                                    </asp:TableCell>

                        </asp:TableRow>

                        <asp:TableRow>

                                    <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Name" /></asp:TableCell>

                                    <asp:TableCell><asp:TextBox id=txtPersonName Runat=server /></asp:TableCell>

                        </asp:TableRow>

                        <asp:TableRow>

                                    <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Email" /></asp:TableCell>

                                    <asp:TableCell><asp:TextBox id="txtPersonEmail" Runat=server /></asp:TableCell>

                        </asp:TableRow>

                        <asp:TableRow>

                                    <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Sex" /></asp:TableCell>

                                    <asp:TableCell>

                                                <asp:RadioButton GroupName="sex" Font-Name="Verdana" Font-Size="12px" Text="Male" ID="sexMale" Runat=server />

                                                <asp:RadioButton GroupName="sex" Font-Name="Verdana" Font-Size="12px" Text="FeMale" ID="sexFeMale" Runat=server />

                                    </asp:TableCell>

                        </asp:TableRow>

                        <asp:TableRow>

                                    <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Date Of Birth" /></asp:TableCell>

                                    <asp:TableCell><asp:TextBox id="txtPersonDOB" Runat=server /></asp:TableCell>

                        </asp:TableRow>

                        <asp:TableRow>

                                    <asp:TableCell HorizontalAlign="Right"><asp:Label Font-Name="verdana" Font-size="12px"  Runat=server Text="Image" /></asp:TableCell>

                                    <asp:TableCell><input type="file" id="PersonImage" runat=server /></asp:TableCell>

                        </asp:TableRow>

                        <asp:TableRow>

                                    <asp:TableCell ColumnSpan=2 HorizontalAlign=Center>

                                    <asp:Button Text="Add Person" OnClick="AddPerson" Runat=server />

                                    </asp:TableCell>

                        </asp:TableRow>

            </asp:Table>

    </form>

 

  </body>

</HTML>

Send your comments to das@aspalliance.com        Back to Article list