G
Guest
Dear Experts,
I am having 1 listbox control in my web form and trying to retrieve the
items and load into it. I want to list out those uploaded files into the
database and list out in the listbox control. My code is as follow:
-------------------------------------------
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnUpload.Click
litMsgBox.Text = ""
If FileUpload1.HasFile Then
'Upload Attachment to Server
If Not FileUpload1.FileContent Is Nothing Then
'perform validation logic before updating the database -
i.e. if file size is
With FileUpload1.FileContent
Dim abyContent(CType(.Length, Integer)) As Byte
'allocate buffer for file data
.Read(abyContent, 0, .Length)
'Update the attach filename into database
InsertFile(FileUpload1.FileName, abyContent)
'Update the attach filename into server folder
SaveFile(FileUpload1.FileName)
'btnUpload.Enabled = False
'btnDelete.Visible = True
'strAllAttachFiles = strAllAttachFiles &
FileUpload1.FileName & ","
'txtRequest.Text = strAllAttachFiles
End With
End If
Else
strMsgBox = "<script language='Javascript'>alert(""No File to be
uploaded"")"
strMsgBox = strMsgBox & Chr(60) & "/script>"
litMsgBox.Text = strMsgBox
End If
LoadAttachments(intRequestID)
End Sub
Private Sub InsertFile(ByVal strFilename As String, ByVal abyContent As
Byte())
'Dim oConnection As New OleDbConnection(strConnectionString)
'Dim intRequestID As Integer
Try
DatabaseConnection()
intRequestID = Int(txtReqNo.Text)
Dim strFileUploadQuery As String = "INSERT INTO Attachments
(Attachments, FileContent, RequestID) VALUES (?,?," & intRequestID & ")"
Dim oCommand As New OleDbCommand(strFileUploadQuery, dbConnection)
Dim oParameter As OleDbParameter = Nothing
oParameter = New OleDbParameter("?", OleDbType.VarChar)
oParameter.Value = strFilename
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)
oParameter = New OleDbParameter("?", OleDbType.VarBinary)
oParameter.Value = abyContent
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)
oCommand.CommandTimeout = 120
oCommand.CommandType = CommandType.Text
oCommand.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
DatabaseDisconnection()
LoadAttachments(intRequestID)
End Try
End Sub
Public Sub LoadAttachments(ByVal intReqID As Integer)
Try
DatabaseConnection()
Dim strSQLFileAttach As String = "SELECT * FROM Attachments
WHERE RequestID = " & intReqID
Dim FileAttachCommand As New
Data.OleDb.OleDbCommand(strSQLFileAttach, dbConnection)
Dim FileAttachReader As Data.OleDb.OleDbDataReader =
FileAttachCommand.ExecuteReader()
lstAttachments.Items.Clear()
FileAttachReader.Read()
lstAttachments.DataValueField = "AttachmentID"
lstAttachments.DataTextField = "Attachments"
lstAttachments.DataSource = FileAttachReader
lstAttachments.DataBind()
FileAttachReader.Close()
Catch ex As Exception
TextBox1.Text = ex.Message
Finally
DatabaseDisconnection()
End Try
End Sub
--------------------------------------------------
My problem is when I retrieve the data, some how the 1st row of the data
does not appear. May I know where and how to get this problem solve?
Many thanks in advance.
Regards,
SB
I am having 1 listbox control in my web form and trying to retrieve the
items and load into it. I want to list out those uploaded files into the
database and list out in the listbox control. My code is as follow:
-------------------------------------------
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnUpload.Click
litMsgBox.Text = ""
If FileUpload1.HasFile Then
'Upload Attachment to Server
If Not FileUpload1.FileContent Is Nothing Then
'perform validation logic before updating the database -
i.e. if file size is
With FileUpload1.FileContent
Dim abyContent(CType(.Length, Integer)) As Byte
'allocate buffer for file data
.Read(abyContent, 0, .Length)
'Update the attach filename into database
InsertFile(FileUpload1.FileName, abyContent)
'Update the attach filename into server folder
SaveFile(FileUpload1.FileName)
'btnUpload.Enabled = False
'btnDelete.Visible = True
'strAllAttachFiles = strAllAttachFiles &
FileUpload1.FileName & ","
'txtRequest.Text = strAllAttachFiles
End With
End If
Else
strMsgBox = "<script language='Javascript'>alert(""No File to be
uploaded"")"
strMsgBox = strMsgBox & Chr(60) & "/script>"
litMsgBox.Text = strMsgBox
End If
LoadAttachments(intRequestID)
End Sub
Private Sub InsertFile(ByVal strFilename As String, ByVal abyContent As
Byte())
'Dim oConnection As New OleDbConnection(strConnectionString)
'Dim intRequestID As Integer
Try
DatabaseConnection()
intRequestID = Int(txtReqNo.Text)
Dim strFileUploadQuery As String = "INSERT INTO Attachments
(Attachments, FileContent, RequestID) VALUES (?,?," & intRequestID & ")"
Dim oCommand As New OleDbCommand(strFileUploadQuery, dbConnection)
Dim oParameter As OleDbParameter = Nothing
oParameter = New OleDbParameter("?", OleDbType.VarChar)
oParameter.Value = strFilename
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)
oParameter = New OleDbParameter("?", OleDbType.VarBinary)
oParameter.Value = abyContent
oParameter.Direction = ParameterDirection.Input
oCommand.Parameters.Add(oParameter)
oCommand.CommandTimeout = 120
oCommand.CommandType = CommandType.Text
oCommand.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
DatabaseDisconnection()
LoadAttachments(intRequestID)
End Try
End Sub
Public Sub LoadAttachments(ByVal intReqID As Integer)
Try
DatabaseConnection()
Dim strSQLFileAttach As String = "SELECT * FROM Attachments
WHERE RequestID = " & intReqID
Dim FileAttachCommand As New
Data.OleDb.OleDbCommand(strSQLFileAttach, dbConnection)
Dim FileAttachReader As Data.OleDb.OleDbDataReader =
FileAttachCommand.ExecuteReader()
lstAttachments.Items.Clear()
FileAttachReader.Read()
lstAttachments.DataValueField = "AttachmentID"
lstAttachments.DataTextField = "Attachments"
lstAttachments.DataSource = FileAttachReader
lstAttachments.DataBind()
FileAttachReader.Close()
Catch ex As Exception
TextBox1.Text = ex.Message
Finally
DatabaseDisconnection()
End Try
End Sub
--------------------------------------------------
My problem is when I retrieve the data, some how the 1st row of the data
does not appear. May I know where and how to get this problem solve?
Many thanks in advance.
Regards,
SB