Results 1 to 6 of 6

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

  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

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Code is not working properly - please help

    I think you're telling it to limit the size. Here is the overload you are using to create a new SqlParameter object:

    http://msdn.microsoft.com/library/de...ctortopic4.asp

    And here is your code:

    objSQLCommand.Parameters.Add(New SqlParameter("@docs", SqlDbType.Image, 16))

    Perhaps you should eliminate the 16 there, as it may cause your file to truncate, and the other overload of the constructor takes only the parameter name and the data type, and leaves size undefined..

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    59

    Re: Code is not working properly - please help

    I have it set to 16 in the database also!

    I have done something similar to this before but with images. But the images were not stored directly in the database! there got stored somewhere else on the server and only interested a reference in the database.

    That was using the blob datatype.

  4. #4
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Code is not working properly - please help

    16 what though, bytes? That number is pretty close to 15...

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    59

    Re: Code is not working properly - please help

    Yes that is what I was thinking, but when I did a similar program with images, i used the same 16 and it allowed me to upload and download huge image files because it did not store them directly in the database. it only stored a reference in the database using blob.

    How do I do a similar thing here?

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2004
    Posts
    59

    Re: Code is not working properly - please help

    conipto - thanks, it was as simple as removing 16 from the code...

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