Hi..
In Previous article i explained about How to upload image to database and How to validate that image,For your refernce i mention the article url below
http://vbdotnetaddict.blogspot.com/2011/02/upload-file-in-aspnet-using-vb-coding.html
http://vbdotnetaddict.blogspot.com/2011/06/how-to-validate-uploaded-images-in.html
Now i am going to explain about to retrieve the uploaded images from the database and View through Data Grid View..
Before in SQL you have to create a table like this..
GO
CREATE TABLE [dbo].[upload](
[fileid] [int] IDENTITY(1,1) NOT NULL,
[ImageName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CS_AS NULL,
[Image] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Coding:
If FileUpload1.HasFile Then
Dim intlength As Integer
Dim bytes1() As Byte = pstFile(FileUpload1) 'Function Used to read bytes from the upload image
' Create SQL Connection
Dim con As New SqlConnection()
con.ConnectionString = ConfigurationManager.ConnectionStrings("phone").ConnectionString
intlength = Convert.ToInt32(FileUpload1.PostedFile.InputStream.Length)
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO upload(ImageName,Image) VALUES (@ImageName,@Image)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
Dim ImageName As New SqlParameter("@ImageName", SqlDbType.VarChar, 50)
ImageName.Value = TextBox1.Text.ToString
cmd.Parameters.Add(ImageName)
Dim imagelenth As New SqlParameter("@Image", SqlDbType.Image)
imagelenth.Value = bytes1
cmd.Parameters.Add(imagelenth)
con.Open()
Dim result As Integer = cmd.ExecuteNonQuery()
con.Close()
loadgrid() 'Used To store image value in grid
Else
Label1.Text = "please select the file to uplaod"
End If
'Function Used to read bytes from the upload image
Function pstFile(ByVal passfile As FileUpload) As Byte()
Dim fs As Stream
Dim bytes1 As Byte()
Dim postfile As Byte()
fs = passfile.PostedFile.InputStream
Dim br1 As New BinaryReader(fs)
bytes1 = br1.ReadBytes(passfile.FileContent.Length)
postfile = bytes1
Return postfile
End Function
'Function Used to store image value in gridview
Sub loadgrid()
Dim fselect As String
Dim adp As New SqlDataAdapter
Dim ds As New DataSet
fselect = "select fileid,ImageName from upload"
Dim con As New SqlConnection()
con.ConnectionString = ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim cmd As New SqlCommand()
cmd = New SqlCommand(fselect, con)
con.Open()
adp = New SqlDataAdapter(cmd)
ds = New DataSet
adp.Fill(ds)
dt = ds.Tables(0)
Session("dataimage") = dt
imggrid.DataSource = dt
imggrid.DataBind()
End Sub
'client Side Coding for create grid view
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
Enter the File name to Upload <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
click here to upload the file <asp:Button ID="Button1" runat="server"
Text="submit" style="height: 26px" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<table cellpadding ="0" cellspacing ="0" width: "100%">
<tr>
<td height="300">
<asp:GridView ID="imggrid" runat="server" AutoGenerateColumns ="false"
Width="97%" >
<Columns >
<asp:TemplateField HeaderText ="Name">
<ItemTemplate >
<%#Eval("imagename")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Image">
<ItemTemplate >
<asp:Image ID="Image1" runat="server" ImageUrl ="Image.aspx?id=0" /> 'This image File is called form the page image.aspx
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
For view an image we have to create one another page like image.aspx, and write the following coding in the server side.....
image.aspx page is called from the Grid View..
By the same way you can get the imagename from the query string ,By using image id get the image from the database and show in grid view.........
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con As New SqlConnection
Dim i As Integer
i = CInt(Request.QueryString("ID").ToString)
con.ConnectionString = ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim sqlcmd As New SqlCommand
Dim sqldr As SqlDataReader
sqlcmd = New SqlCommand("select image from upload where ='" & i & "'", con)
con.Open()
sqldr = sqlcmd.ExecuteReader()
con.Close()
If sqldr.HasRows Then
Response.BinaryWrite(sqldr("image"))
End If
End Sub
End Sub
I hope this one really helpful to you........
0 comments:
Post a Comment