January 23rd, 2009, 07:32 AM
-
[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()
January 23rd, 2009, 07:48 AM
-
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
January 23rd, 2009, 08:18 AM
-
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.