Click here to Skip to main content
15,886,199 members
Articles / Web Development / ASP.NET

Handling Image and Employee Records and Show in Datagrid and Image Control

Rate me:
Please Sign up or sign in to vote.
2.50/5 (9 votes)
14 Oct 2010CPOL 36.8K   745   20   3
Handling Image With Employee Records and Show in Datagrid and Image Control
Sample screenshot

Introduction

In this article, I create two classes, one is EmployeeInformation and another is EmployeeManager. EmployeeInformation class structure looks like this:

VB.NET
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:

VB.NET
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:

VB.NET
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:

VB.NET
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.NET
<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.

VB.NET
Image1.ImageUrl=FormateUrl(txtEmpid.Text)

To Clear All TextBox In Form, I write this sub.

VB.NET
Sub ClearField()
Dim ctl As Control
For Each ctl In Me.FindControl("Form1").Controls
If (Not ctl Is System.DBNull.Value) Then
'Reset TextBox On The Form
If (ctl.GetType.ToString.ToLower.IndexOf("textbox") > 0) Then
CType(ctl, TextBox).Text = String.Empty
End If
End If
Next
End Sub

License

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


Written By
Web Developer
India India
~ns

Comments and Discussions

 
QuestionHandling Image and Employee Records and Show in Datagrid and Image Control Pin
priom222-Jun-13 4:08
priom222-Jun-13 4:08 
QuestionHow to implement same to insert multiple images for a single user? Pin
zakzapakzak2-May-08 1:35
zakzapakzak2-May-08 1:35 
GeneralExcellent Contribution [modified] Pin
cgilf2-Aug-06 10:39
cgilf2-Aug-06 10:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.