Download file from sql database.

G

Guest

Hi,

I have trouble in downloading the excel file from sql database.
After downloading, i try to open it, the error msg prompt:"The file in not
in a recognizable format"

Any ideas? i already out of ideas.

Here is my download excel file code:
Function GetFile()

Dim PROD_CONNEC As SqlConnection = New
SqlConnection("Server=gl;uid=LBPCOS;pwd=new1;database=DB")
Dim PROD_CMD As SqlCommand = New SqlCommand("wla_getfile",
PROD_CONNEC)
Dim PROD_DR As SqlDataReader

PROD_CMD.CommandType = CommandType.StoredProcedure

Dim PRM_CP As New SqlParameter("@Attachment_ID", SqlDbType.Int)
PRM_CP.Value = CInt("2")
PROD_CMD.Parameters.Add(PRM_CP)

PROD_CONNEC.Open()
PROD_DR = PROD_CMD.ExecuteReader


If PROD_DR.Read Then
Response.ContentType = PROD_DR("Contenttype").ToString
Response.OutputStream.Write(CType(PROD_DR("Filedata"), Byte()),
0, CInt(PROD_DR("Filesize")))
Response.AddHeader("Content-Disposition", "attachment;filename="
+ PROD_DR("Filename").ToString())
Else
Response.Write("File Not Found.")
End If

PROD_CONNEC.Close()

End Function


Below is my upload file code:
Function UploadFile()

Dim PROD_CONNEC As SqlConnection = New
SqlConnection("Server=gl;uid=LBPCOS;pwd=new1;database=DB")
Dim PROD_CMD As SqlCommand = New SqlCommand("wla_fileupload",
PROD_CONNEC)
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length,
Integer)

If iLength = 0 Then Exit Function 'not a valid file
Dim sContentType As String = File1.PostedFile.ContentType
Dim sFileName As String, i As Integer
Dim bytContent As Byte()
ReDim bytContent(iLength) 'byte array, set to file size

'strip the path off the filename
i = InStrRev(File1.PostedFile.FileName.Trim, "\")
If i = 0 Then
sFileName = File1.PostedFile.FileName.Trim
Else
sFileName = Right(File1.PostedFile.FileName.Trim,
Len(File1.PostedFile.FileName.Trim) - i)
End If



PROD_CMD.CommandType = CommandType.StoredProcedure

Dim PRM_CP As New SqlParameter("@FileName", SqlDbType.NVarChar)
PRM_CP.Value = sFileName
PROD_CMD.Parameters.Add(PRM_CP)

Dim PRM_CP2 As New SqlParameter("@FileSize", SqlDbType.Real)
PRM_CP2.Value = iLength
PROD_CMD.Parameters.Add(PRM_CP2)

Dim PRM_CP3 As New SqlParameter("@FileData", SqlDbType.Image)
PRM_CP3.Value = bytContent
PROD_CMD.Parameters.Add(PRM_CP3)

Dim PRM_CP4 As New SqlParameter("@ContentType", SqlDbType.NVarChar)
PRM_CP4.Value = sContentType
PROD_CMD.Parameters.Add(PRM_CP4)

PROD_CONNEC.Open()
PROD_CMD.ExecuteNonQuery()
PROD_CONNEC.Close()


End Function

Your help will be appreciated.

Best Regards,
Daniel
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top