Storing and Retriving any document in SQL Server 2000 using ASP.Net

M

Manish Naik

Hi,

Using ASP.Net, I want to store and retrive documents (Word, excel,
etc) from SQL Server 2000 database. I have tried image type data
field, but could not succed. Can any one help me please.

Regards,

Manish Naik
 
M

Manish Naik

Edward said:
list your code here, maybe we can give the answer to you.


_____________________________________________________________________

Hi Edward,

Thank you for your response.

I am sending the code that we tried out at listed below.

Your help is highly appreciated.

Regards,

Manish Naik

__________________________________________________________________________

The code below allows the user to save data in the ImageGallery Table
While page loads, if there exists any data in the table it displays it
on the page below file text box control.
Clicking on the image will display the image in new explorer.
If the stored type is other then picture...like word, excel, the it
will not be displyed

Try to save data in ImageGallery tabel by clicking on the browse
button
and selecting the documenet u want to save. then click on the upload
button.
Save all types of document like .jpeg, .txt, .xls etc.

It will display the .jpeg file but will show error for other types.




//########### cOMMENT
THIS IS ASPX PAGE

###################//


<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="WebApplication4.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server"
enctype="multipart/form-data">
<p align="center"><font face="Comic Sans MS" size="7"><strong>
<asp:LinkButton id="LinkButton1" style="Z-INDEX: 101; LEFT:
-8px; POSITION: absolute; TOP: 216px"
runat="server">LinkButton</asp:LinkButton>
My Photo Gallary</strong> </font>
</p>
<p align="center"><input id="UploadImage" type="file"
name="UploadImage" runat="server">&nbsp;
<asp:button id="btnUpload" Runat="server"
Text="Upload"></asp:button></p>
<p align="center"></p>
<p align="center">
<asp:DataList ID="DataList1" Runat="server"
RepeatDirection="Horizontal" RepeatColumns="2">
<ItemTemplate>
<table>
<tr>
<td height="125px">
<a href='WebForm2.aspx?ID=<%#container.dataitem("ImageID")
%>&amp;source=DB'
target=_blank ><img
src='WebForm2.aspx?ID=<%#container.dataitem("ImageID")
%>&amp;source=DB&amp;width=125'
border=0> </a>
</td>
</tr>
<tr>
<td>
<a href='WebForm2.aspx?ID=<%#container.dataitem("ImageID")
%>&amp;source=DB'
target=_blank >
<%# container.dataitem("ImageName") %>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</p>
</form>
</body>
</HTML>





//####################cOMMENT

cODE BEHIND FILE FOR THE ABOVE ASPX PAGE
###########################3//

Imports System.IO
Imports System.IO.Stream
Imports System.Data.SqlClient

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
Protected WithEvents btnUpload As System.Web.UI.WebControls.Button
Protected WithEvents UploadImage As
System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents DataList1 As
System.Web.UI.WebControls.DataList
Protected WithEvents LinkButton1 As
System.Web.UI.WebControls.LinkButton
Protected WithEvents Calendar1 As
System.Web.UI.WebControls.Calendar

#End Region
Dim pubsConn As SqlConnection = New SqlConnection("Data
Source=mm;Initial Catalog=pubs;uid=sa;pwd=dotnet2;")

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
binddata()
End If

End Sub
Public Sub binddata()
Dim sqlconnect As String
sqlconnect = "initial catalog=pubs;data
source=MM;uid=sa;pwd=dotnet2;"
Dim cn As New SqlConnection(sqlconnect)
Dim sqlcmd As String
sqlcmd = "Select ImageId,ImageName from ImageGallery"
Dim cmd As New SqlCommand(sqlcmd, cn)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable("ImageGallery")
da.Fill(dt)
DataList1.DataSource = dt
DataBind()

End Sub

Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnUpload.Click

If UploadImage.PostedFile Is Nothing Then
Response.Write("No File Upload. <br>")
Return
End If

Dim imagestream As Stream = UploadImage.PostedFile.InputStream

Dim imagelength As Integer =
UploadImage.PostedFile.ContentLength
Dim imagetype As String = UploadImage.PostedFile.ContentType

Dim imagename As String
imagename = Path.GetFileName(UploadImage.PostedFile.FileName)

Dim mstream As New MemoryStream()
Dim imagedata(1024) As Byte
Dim count As Integer = imagedata.Length

count = imagestream.Read(imagedata, 0, imagedata.Length)

Do While count > 0
mstream.Write(imagedata, 0, count)
count = imagestream.Read(imagedata, 0, imagedata.Length)
Loop

Dim sqlcmd As String

sqlcmd = "insert into
ImageGallery(imageName,imageType,imageData)"
sqlcmd &= " values ( @imageName,@imageType,@imageData )"

Dim cmd As New SqlCommand(sqlcmd, pubsConn)
cmd.Parameters.Add("@imageName", imagename)
cmd.Parameters.Add("@imageType", imagetype)
cmd.Parameters.Add("@imageData", mstream.GetBuffer())

pubsConn.Open()
cmd.ExecuteNonQuery()
pubsConn.Close()
DataBind()
End Sub

Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles LinkButton1.Click
Response.Redirect("d:/keyhook.txt")

End Sub
End Class




//###############################################

TABLE IS STORED IN pubs database
table name: ImageGallery



ImageId ---- Numeric --- 9
ImageName ---- varchar --- 255
ImageType ---- varchar --- 255
ImageData ---- Image --- 16







############################////////////
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top