Introduction
In this article, I create two classes, one is EmployeeInformation
and another is EmployeeManager
. EmployeeInformation
class structure looks like this:
Imports System
Imports System.IO.Stream
Imports System.Data.SqlClient
Public Class EmployeeInformation
Inherits Connect
Public Id As Integer
Public Name As String
Public Address As String
Public ContactNo As Long
Public Designation As String
Public City As String
Public Image() As Byte
Public Shared Function GetImpId() As Integer
Connect.Qry = "Select Max(Id) from EmployeeRecord"
Connect.con.Open()
Connect.cmd.Connection = con
Connect.cmd.CommandText = Qry
GetImpId = Connect.cmd.ExecuteScalar
Connect.cmd.Dispose()
Connect.con.Close()
End Function
End Class
And EmployeeManager
class structure looks like this:
Public Class EmployeeManager
Inherits Connect
Public Shared Sub CreateRecord(ByVal EmpObj As EmployeeInformation)
Connect.Qry = "insert into EmployeeRecord(Name,Designation,Address,ContactNo,City,image)
values (@Name,@Designation,@Address,@ContactNo,@City,@image)"
Connect.con.Open()
Connect.cmd.Connection = con
Connect.cmd.CommandText = Qry
Dim prmName As New SqlParameter("@Name", SqlDbType.VarChar)
prmName.Value = EmpObj.Name
Connect.cmd.Parameters.Add(prmName)
Dim prmDesignation As New SqlParameter("@Designation", SqlDbType.VarChar)
prmDesignation.Value = EmpObj.Designation
Connect.cmd.Parameters.Add(prmDesignation)
Dim prmAddress As New SqlParameter("@Address", SqlDbType.VarChar)
prmAddress.Value = EmpObj.Address
Connect.cmd.Parameters.Add(prmAddress)
Dim prmContactNo As New SqlParameter("@ContactNo", SqlDbType.VarChar)
prmContactNo.Value = EmpObj.ContactNo
Connect.cmd.Parameters.Add(prmContactNo)
Dim prmCity As New SqlParameter("@City", SqlDbType.VarChar)
prmCity.Value = EmpObj.City
Connect.cmd.Parameters.Add(prmCity)
Dim prmImage As New SqlParameter("@image", SqlDbType.Image)
prmImage.Value = EmpObj.Image
Connect.cmd.Parameters.Add(prmImage)
Connect.cmd.ExecuteNonQuery()
Connect.cmd.Dispose()
Connect.con.Close()
End Sub
Public Shared Sub UpdateRecord(ByVal EmpObj As EmployeeInformation)
Connect.Qry = "Update EmployeeRecord set
<a href="mailto:Name=@Name,Designation=@Designation%22%3EName=@Name,Designation=@Designation,Address=@Address,ContactNo=@ContactNo,City=@City,image=@image">mailto:Name=@Name,Designation=@Designation">Name=@Name,Designation=@Designation,
Address=@Address,ContactNo=@ContactNo,City=@City,image=@image</a> where id=" &
EmpObj.Id
Connect.con.Open()
Connect.cmd.Connection = con
Connect.cmd.CommandText = Qry
Dim prmName As New SqlParameter("@Name", SqlDbType.VarChar)
prmName.Value = EmpObj.Name
Connect.cmd.Parameters.Add(prmName)
Dim prmDesignation As New SqlParameter("@Designation", SqlDbType.VarChar)
prmDesignation.Value = EmpObj.Designation
Connect.cmd.Parameters.Add(prmDesignation)
Dim prmAddress As New SqlParameter("@Address", SqlDbType.VarChar)
prmAddress.Value = EmpObj.Address
Connect.cmd.Parameters.Add(prmAddress)
Dim prmContactNo As New SqlParameter("@ContactNo", SqlDbType.VarChar)
prmContactNo.Value = EmpObj.ContactNo
Connect.cmd.Parameters.Add(prmContactNo)
Dim prmCity As New SqlParameter("@City", SqlDbType.VarChar)
prmCity.Value = EmpObj.City
Connect.cmd.Parameters.Add(prmCity)
Dim prmImage As New SqlParameter("@image", SqlDbType.Image)
prmImage.Value = EmpObj.Image
Connect.cmd.Parameters.Add(prmImage)
Connect.cmd.ExecuteNonQuery()
Connect.cmd.Dispose()
Connect.con.Close()
End Sub
Public Shared Sub DeleteRecord(ByVal id As Integer)
Connect.Qry = "Delete from EmployeeRecord where id=" & id
Connect.con.Open()
Connect.cmd.CommandText = Qry
Connect.cmd.Connection = con
Connect.cmd.ExecuteNonQuery()
Connect.cmd.Dispose()
Connect.con.Close()
End Sub
Public Shared Function GetRecords(ByVal id As Integer) As DataTable
Connect.Qry = "select * from EmployeeRecord where id=" & id & " ORDER BY id"
Dim da As New SqlDataAdapter(Qry, con)
Dim dt As New DataTable
da.Fill(dt)
Return dt
End Function
Public Shared Function GetRecords() As DataTable
Connect.Qry = "select * from EmployeeRecord ORDER BY id"
Dim da As New SqlDataAdapter(Qry, con)
Dim dt As New DataTable
da.Fill(dt)
Return dt
End Function
Shared Function FormatUrl(ByVal strArg) As String
Return ("RealImagePage.aspx?id=" & strArg)
End Function
End Class
and another class called Connect
that is used for declaring ConnectionObject
and CommandObject
. That looks like this:
Public Class Connect
Protected Shared con As New SqlConnection(ConfigurationSettings.AppSettings("ConStr"))
Protected Shared cmd As New SqlCommand
Protected Shared Qry As String
End Class
These is the structure of the class. Now for image display in Datagrid
, we use:
Function FormatUrl. i.e. look like this
Shared Function FormatUrl(ByVal strArg) As String
Return ("RealImagePage.aspx?id=" & strArg)
End Function
Here I use RealImagePage.aspx in this page Response.BinaryWriter
write the image, and that image is returned by id on Datagrid
. In Datagrid
, I create Template
column that contains image control.
<asp:Image width=90 Height=100 ImageUrl='<% # FormatUrl(DataBinder.Eval
(Container,"DataItem.Id")%>'/>
This will call the Image in Datagrid
column. The same thing for ImageControl
. I write this for showing image in Image control.
Image1.ImageUrl=FormateUrl(txtEmpid.Text)
To Clear All TextBox In Form, I write this sub
.
Sub ClearField()
Dim ctl As Control
For Each ctl In Me.FindControl("Form1").Controls
If (Not ctl Is System.DBNull.Value) Then
If (ctl.GetType.ToString.ToLower.IndexOf("textbox") > 0) Then
CType(ctl, TextBox).Text = String.Empty
End If
End If
Next
End Sub