Results 1 to 6 of 6

Thread: [RESOLVED] Code is not working properly - please help

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    59

    Resolved [RESOLVED] Code is not working properly - please help

    Hi

    I have the following code which for some reason does not do what it should be doing. What it should do is, on the first button click it should upload to a database a selected file.

    The second button should download the file again.

    But for some reason when I upload a file which is 23,040 bytes, when i clikc the download button it will only return a file which is 15 bytes !!!

    Can anyone see why this is happening?

    THE CODE

    VB Code:
    1. Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    2.         WriteDocumentToDB2("c:\test.doc")
    3.     End Sub
    4.  
    5.     Private Sub WriteDocumentToDB2(ByVal sInDoc As String)
    6.         Dim objDataSet As DataSet
    7.         Dim objSQLCommand As SqlCommand
    8.         Dim objSQLConnection As New SqlConnection("connection string")
    9.         Dim strInsertCommand As String = "insert into table (name, phone, docs) values (@name, @phone, @docs)"
    10.  
    11.         Dim objFileStream As New FileStream(sInDoc, FileMode.OpenOrCreate, FileAccess.Read)
    12.         Dim objDocBytes(objFileStream.Length) As Byte
    13.  
    14.         objFileStream.Read(objDocBytes, 0, objFileStream.Length)
    15.         objFileStream.Close()
    16.  
    17.         objSQLCommand = New SqlCommand(strInsertCommand, objSQLConnection)
    18.  
    19.         objSQLCommand.Parameters.Add(New SqlParameter("@name", SqlDbType.NVarChar, 50))
    20.         objSQLCommand.Parameters("@name").Value = "Some Value Here"
    21.  
    22.         objSQLCommand.Parameters.Add(New SqlParameter("@phone", SqlDbType.NVarChar, 50))
    23.         objSQLCommand.Parameters("@phone").Value = "Some Value Here"
    24.  
    25.         objSQLCommand.Parameters.Add(New SqlParameter("@docs", SqlDbType.Image, 16))
    26.         objSQLCommand.Parameters("@docs").Value = objDocBytes
    27.  
    28.         objSQLCommand.Connection.Open()
    29.         objSQLCommand.ExecuteNonQuery()
    30.         objSQLCommand.Connection.Close()
    31.     End Sub
    32.  
    33.     Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
    34.         ReadDocumentFromDB2("c:\a\test.doc")
    35.     End Sub
    36.  
    37.  
    38.     Public Sub ReadDocumentFromDB2(ByVal sOutDoc As String)
    39.         Dim objDataSet As DataSet
    40.         Dim objSQLConnection As New SqlConnection("connection string")
    41.         Dim objSQLDataAdapter As SqlDataAdapter
    42.  
    43.         Dim objSelectCommand As String = "select docs from table1 where id = @id"
    44.  
    45.         objSQLDataAdapter = New SqlDataAdapter(objSelectCommand, objSQLConnection)
    46.  
    47.         objSQLDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@id", SqlDbType.Int, 4))
    48.         objSQLDataAdapter.SelectCommand.Parameters("@id").Value = "0"
    49.  
    50.         objDataSet = New DataSet
    51.         objSQLDataAdapter.Fill(objDataSet)
    52.  
    53.         Dim objDataRow As DataRow
    54.         objDataRow = objDataSet.Tables(0).Rows(0)
    55.  
    56.         Dim objDocBytes() As Byte
    57.         objDocBytes = objDataRow("docs")
    58.  
    59.         Dim K As Long
    60.         K = UBound(objDocBytes)
    61.  
    62.         Dim objFileStream As New FileStream(sOutDoc, FileMode.OpenOrCreate, FileAccess.Write)
    63.         objFileStream.Write(objDocBytes, 0, K)
    64.         objFileStream.Close()
    65.         objFileStream = Nothing
    66.     End Sub
    Last edited by Hack; Mar 31st, 2006 at 09:41 AM. Reason: Added Green Resolved Checkmark Edited by narmi2 : Today at 08:55 AM. Reason: RESOLVED BY conipto

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width