#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Location
    London, UK
    Posts
    13
    Rep Power
    0

    Question [VB.NET]Object must implement IConvertible?


    Hello All,

    Could anyone help me out with a VB.NET error I am having..

    I am making a form that when executed calls a Sproc and enters the information into the database..

    I'm new to .NET so if any of the below code is not that good then please take it easy on me! :P

    I am getting the error - Object must implement IConvertible

    Code:
    Dim con = New SqlConnection("XXXX")
            Dim cmd = New SqlCommand("Add_Bug", con)
            cmd.CommandType = CommandType.StoredProcedure
    
            cmd.Parameters.Add("BugTitle", SqlDbType.NVarChar).Value = txtBugTitle.Text
            cmd.Parameters.Add("BugDescription", SqlDbType.NVarChar).Value = txtEnquiry.Text
            cmd.Parameters.Add("BugRank", SqlDbType.NVarChar).Value = ddlBugRank.Text
            cmd.Parameters.Add("Name", SqlDbType.NVarChar).Value = txtName.Text
            cmd.Parameters.Add("Email", SqlDbType.NVarChar).Value = txtEmail.Text
            cmd.Parameters.Add("Screenshot", SqlDbType.Image).Value = file1.PostedFile.FileName
    
            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Location
    London, UK
    Posts
    13
    Rep Power
    0
    Ok so after doing some more looking around I have found that the issue lies within the Adding of the parameter shown below:

    Code:
    cmd.Parameters.Add("@Screenshot", SqlDbType.Image).Value = file1.PostedFile.FileName
    Now I'm assuming that I can't just grab the uploaded file from the asp:fileupload control?

    Can any one instruct me on how to go about getting the file
  4. #3
  5. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    First, some general improvements to the code:
    Code:
    Using con As New SqlConnection("XXXX"), _
          cmd As New SqlCommand("Add_Bug", con)
          
        cmd.CommandType = CommandType.StoredProcedure
    
        cmd.Parameters.Add("BugTitle", SqlDbType.NVarChar).Value = txtBugTitle.Text
        cmd.Parameters.Add("BugDescription", SqlDbType.NVarChar).Value = txtEnquiry.Text
        cmd.Parameters.Add("BugRank", SqlDbType.NVarChar).Value = ddlBugRank.Text
        cmd.Parameters.Add("Name", SqlDbType.NVarChar).Value = txtName.Text
        cmd.Parameters.Add("Email", SqlDbType.NVarChar).Value = txtEmail.Text
    
        cmd.Parameters.Add("Screenshot", SqlDbType.Image).Value = file1.PostedFile.FileName
    
        con.Open()
        cmd.ExecuteNonQuery()
    End Using
    I've done two things here:
    1) Made your connection and command objects strongly typed.
    2) Guaranteed your connection will be closed and the command is disposed, even if an exception is thrown.

    Now let's fix your image parameter. You were assigning the name of the file to the parameter value, and expecting it to fill in the file's contents. It just doesn't work that way. You need to get an actual byte array holding your files contents and use that for the parameter value.

    Unfortunately, I have not yet needed to use the fileupload control myself, so I couldn't tell you how it handles transferring the bytes. But I expect it's pretty simple: probably just referencing the correct property or calling the correct method.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn

IMN logo majestic logo threadwatch logo seochat tools logo