|
Hi, I get the following error message when I run my application in the browser.
"Invalid cast from System.String to System.Byte[ ]"
I have changed cmdInsert.Parameters.Add("@docContent",OdbcType.Text) to cmdInsert.Parameters. Add("@docContent",OdbcType.Binary) like someone had suggested, but it still does not work. I have checked other variables for type mismatch but didn't find any. If you see anything at all wrong with my code please point it out. I would really be grateful if you can show me what is causing VS2003 to throw the exception. Thank you in advance for your help. The following are my code and database to help you better understand my situation.
---------------------------------------------------------------------------------------------
| Field | Type | Null | Key |default | Extra |
--------------------------------------------------------------------------------------------------
| docID | int(11) | NO | PRI | NULL | AUTO_INCREMENT |
|
| docTitle | varchar(30) | NO | | NULL | |
|
| docContent | MediumBlob | NO | | NULL | |
|
| docDate | timestamp | NO | | CURRENT_TIMESTAMP | |
|
| docType | varchar(15) | NO | | NULL | |
----------------------------------------------------------------------------------------------------
Imports System.IO
Imports System.Data.Odbc
Public Class Upload
Inherits System.Web.UI.Page
Private Sub Submit1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.Click
Dim strFileExtension As String
Dim StrFileType As String
Dim intFileLen As Integer
Dim objStream As Stream
Dim strInsert As String
Dim myConnection As OdbcConnection
Dim cmdInsert As OdbcCommand
If Not IsNothing(txtFileContents.PostedFile) Then
'Determines File Type
strFileExtension = Right(txtFileContents.PostedFile.FileName, 4)
Select Case strFileExtension.ToLower
Case ".doc"
StrFileType = "doc"
Case ".ppt"
StrFileType = "ppt"
Case ".htm"
StrFileType = "htm"
Case ".html"
StrFileType = "html"
Case ".txt"
StrFileType = "txt"
Case Else
StrFileType = "jpg"
End Select
'Grab the content of uploaded file
intFileLen = txtFileContents.PostedFile.ContentLength
Dim arrFile(intFileLen) As Byte
objStream = txtFileContents.PostedFile.InputStream
objStream.Read(arrFile, 0, intFileLen)
'Add UpLoaded file to a database
myConnection = New OdbcConnection("Provider=MySQLProv; Data Source=Data;User Id=myIDassword=myPWD")
strInsert = "INSERT into QUsers (docTitle,docContent,docDate,docType) values (?,?,?,?,?)"
cmdInsert = new odbcCommand(strInsert,myConnection)
cmdInsert.Parameters.Add("@docID",OdbcType.int,30)
cmdInsert.Parameters["@docID"].Values =""
cmdInsert.Parameters.Add("@docDate", OdbcType.TimeStame,14)
cmdInsert.Parameters["@docDate"].Values = DateTime.Now.ToShortDateString()
cmdInsert.Parameters.Add("@docTitle", OdbcType.Varchar,30 )
cmdInsert.Parameters["@docTitle"].Values = txtFileTitle.Text
cmdInsert.Parameters.Add("@docType", OdbcType.Varchar,30)
cmdInsert.Parameters["@docType"].Values =StrFileType
cmdInsert.Parameters.Add("@docContent",OdbcType.Binary)
cmdInsert.Parameters["@docContent"].Values =arrFile
myConnection.Open()
cmdInsert.ExecuteNonQuery()
myConnection.Close()
End If
-- modified at 15:16 Friday 8th June, 2007
|
|
|
|
|
On what line does this exception show up?
|
|
|
|
|
It shows up on line 92 where it says cmdInsert.ExecuteNonQuery(). Thank you so much for responding. I just don't know what else to do.
|
|
|
|
|
OK, this usually means you have mismatched one of your parameters. But, the code sample that you gave is NOT what you have running. This code wouldn't even compile, let alone run! Next time, COPY AND PASTE your code, do NOT retype what you THINK it is! How can I tell? The property name is Value , not "Values".
cmdInsert.Parameters.Add("@docID",OdbcType.int,30)
cmdInsert.Parameters["@docID"].Values =""
cmdInsert.Parameters.Add("@docDate", OdbcType.TimeStame,14)
cmdInsert.Parameters["@docDate"].Values = DateTime.Now.ToShortDateString()
cmdInsert.Parameters.Add("@docTitle", OdbcType.Varchar,30 )
cmdInsert.Parameters["@docTitle"].Values = txtFileTitle.Text
cmdInsert.Parameters.Add("@docType", OdbcType.Varchar,30)
cmdInsert.Parameters["@docType"].Values =StrFileType
cmdInsert.Parameters.Add("@docContent",OdbcType.Binary)
cmdInsert.Parameters["@docContent"].Values =arrFile
I can see several problems with creating the parameter objects. In the overload of Add that you're using, you're supplying a parameter name, an ODBCType, and a size for that type. You do NOT have to specify a size for basic types like ODBCType.Int because the size is always constant. For Text and Binary types, you do specify a size. So, changing the code a bit:
' Int's are always 4 bytes long, not 30.
cmdInsert.Parameters.Add("@docID",OdbcType.int).Value = String.Empty
' This is where your error occured. You used the TimeStamp type instead of
' Date. TimeStamp requires an array of bytes and you supplied a string of characters.
cmdInsert.Parameters.Add("@docDate", OdbcType.Date).Value = DateTime.Now
' This is a disaster waiting to happen! You don't do any validation of user input
' before you plug what the user typed directly into an SQL parmater! VERY BAD IDEA!
cmdInsert.Parameters.Add("@docTitle", OdbcType.Varchar, 30).Value = txtFileTitle.Text
' So far, this one looks good. Assuming, of course, that 30 is the
' actual size of the field in your database!
cmdInsert.Parameters.Add("@docType", OdbcType.Varchar, 30).Value = StrFileType
' I'd be willing to bet that this OdbcType doesn't match the field type in your database.
cmdInsert.Parameters.Add("@docContent",OdbcType.Binary, size is missing!).Value = arrFile
|
|
|
|
|
Thank you so much, you solved my problem. What you had said about the cause of the problem is exactly right, it works. I have a question however about what you said about Int always being 4 byte. I have seen people use 11 as the size of the Int type. Would you please explain? Also if I wanted to use a blob what would a good size be for it? Thank you so much for your help.
|
|
|
|
|
ASPnoob wrote: I have a question however about what you said about Int always being 4 byte. I have seen people use 11 as the size of the Int type.
It doesn't matter what they put for the size, an Int is a 32-bit value, 4 bytes. Whatever they put will simply be ignored.
You don't put a size for a blob in MySql.
|
|
|
|
|
i am using two queries in the stored procedure.
first is the insert statement and then select statement
the stored procedure is
CREATE PROCEDURE [dbo].[AdminValidation] <br />
@uname as varchar(10),<br />
@pword as varchar(10)<br />
as<br />
insert into admin(u_name,p_word)values(@uname,@pword)<br />
select * from admin<br />
GO
Is it right?
|
|
|
|
|
I haven't done it in a stored procedure before, but the syntax seems to be ok.
Except not having an apostrophe ( ' ) around the @uname and @pword fields may give you trouble.
ie.
insert into admin(u_name,p_word)values('@uname','@pword')
select * from admin
GO
for any values that aren't numbers...you need the apostrophes around them to the best of my knowledge. Hope that helps a bit.
|
|
|
|
|
I agree the syntax seems fine.
reegan41 wrote: not having an apostrophe ( ' ) around the @uname and @pword fields may give you trouble
The apostrophe is not required
Steve Jowett
-------------------------
Sometimes a man who deserves to be looked down upon because he is a fool, is only despised only because he is an 'I.T. Consultant'
|
|
|
|
|
Steven J Jowett wrote: The apostrophe is not required
Is that just for inside a stored structure? Just so I know.
|
|
|
|
|
If your have defined the variable/parameter as a text type field, e.g. varchar, nvarchar etc. then SQL Server knows it is a text value and will handle the value accordingly. So this would apply to stored procedures and functions.
Steve Jowett
-------------------------
Sometimes a man who deserves to be looked down upon because he is a fool, is only despised only because he is an 'I.T. Consultant'
|
|
|
|
|
oh I see, thanks man. Ya, normally I define them as strings, which makes sense now.
|
|
|
|
|
Does it work?
CleaKO
"Now, a man would have opened both gates, driven through and not bothered to close either gate." - Marc Clifton (The Lounge)
|
|
|
|
|
Add set nocount on to prevent the insert from returning a result.
create procedure AdminValidation
@UName varchar(10),
@PWord varchar(10)
as
set nocount on
insert into admin (u_name, p_word)
values (@UName, @PWord)
select u_name, p_word
from admin
Avoid using * in a select. Specify the fields that you want returned, that way the result of existing queries doesn't change just because you add a field to the table.
---
single minded; short sighted; long gone;
|
|
|
|
|
In a form in MS Access, I'm going to have numerous checkboxes...that when clicked on, populate a text body. Each checkbox points to a table that contains numerous paragraphs. So ideally, when each box is checked, its corresponding paragraph populates a text body. After all of the desired boxes are checked, the text body should have numerous paragraphs inside it. I want to be able to export this text body to a Word document, and save it as a specified file name. What would be the best way to go about this? I have a pretty good idea how to populate the text body using the database, but am alittle befuddled on how to export the data to Word. Thanks.
|
|
|
|
|
hello to ALL,
how can i create a user group,user in active directory using vb 6.0 OR VB.Net
any code ???
Is there any supported links ?????
plzzz help
-koolprasad2003
Be a good listener...Because Opprtunity knoughts softly...N-Joy
|
|
|
|
|
The key words in your question are "Active Directory", "Create User", and "VB.NET". Let's see what happens when you type them into Google. Click[^]
|
|
|
|
|
i was using stoed procedure and getting the following eror.
invalid object name adminvalidation
the code is as follows
Dim con As New SqlConnection(connectionstring)
con.Open()
Dim cmd As New SqlCommand("AdminValidation", con)
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Parameters.Add("@uname", Data.SqlDbType.VarChar).Value = TextBox1.Text
cmd.Parameters.Add("@pword", Data.SqlDbType.VarChar).Value = TextBox2.Text
cmd.ExecuteNonQuery()
adminvalidation is stored procedure name.
i have gone though This Article But in vain
-- modified at 8:30 Friday 8th June, 2007
|
|
|
|
|
And does the stored procedure actually exist in the database to which you've connected?
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
* Reading: Developer Day 5
Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton
My website
|
|
|
|
|
Problem is solved.
Sorry sir, there was an error in writing the corect table name into stored procedure.
Thanks a lot.
-- modified at 8:34 Friday 8th June, 2007
|
|
|
|
|
Can I or how can I populate a TreeView control using records in two table? Let say Parent Nodes comes from Table1 and each Parent Node has a child comming from Table2.
+--- Record 1 from Table 1
| |
| + Child 1 from Table 2 with reference to Record 1 in Table 1
| |
| + Child 2 from Table 2 with reference to Record 1 in Table 1
|
+--- Record 2 from Table 1
| |
| + Child 1 from Table 2 with reference to Record 2 in Table 1
|
+--- Record 3 from Table 1
| |
| + Child 1 from Table 2 with reference to Record 3 in Table 1
| |
| + Child 2 from Table 2 with reference to Record 3 in Table 1
| |
| + Child 3 from Table 2 with reference to Record 3 in Table 1
|
+--- Record 4 from Table 1
|
+ Child 1 from Table 2 with reference to Record 4 in Table 1
|
+ Child 2 from Table 2 with reference to Record 4 in Table 1
Hope that one will clear my question.
Thanks in advance.
qhYzme
|
|
|
|
|
What's the meaning of
Child 1 from Table 2 with reference to Record 1 in Table
|
|
|
|
|
Sorry for that. what i mean is "Child 1" came from Table 2 and had a reference (let say an ID) from a "Record 1" in Table 1. which means all child node are came from Table 2 and all parent node are came from table 1. just like in table/drawing i included in my post.
+--- Record 1 from Table 1 (TABLE 1 ID = '001')
| |
| + Child 1 from Table 2 with reference to Record 1 in Table 1 (TABLE 2 ID = '001')
| |
| + Child 2 from Table 2 with reference to Record 1 in Table 1 (TABLE 2 ID = '001')
|
|+--- Record 2 from Table 1 (TABLE 1 ID = '002')
| |
| + Child 1 from Table 2 with reference to Record 2 in Table 1 (TABLE 2 ID = '002')
|
|+--- Record 3 from Table 1 (TABLE 1 ID = '003')
| |
| + Child 1 from Table 2 with reference to Record 3 in Table 1 (TABLE 2 ID = '003')
| |
| + Child 2 from Table 2 with reference to Record 3 in Table 1 (TABLE 2 ID = '003')
| |
| + Child 3 from Table 2 with reference to Record 3 in Table 1 (TABLE 2 ID = '003')
|
|+--- Record 4 from Table 1 (TABLE 1 ID = '003')
|
| + Child 1 from Table 2 with reference to Record 4 in Table 1 (TABLE 2 ID = '003')
| + Child 2 from Table 2 with reference to Record 4 in Table 1 (TABLE 2 ID = '003')
as you can see, in Table 1 there are 3 records and Table 2 have 3 record in it. and for each record in Table 1 have a corresponding child (link record / relation to) in Table 2
anyway, thanks for the reply.
|
|
|
|
|
thanks for all you guys. i got it, there something wrong with the flow and records in my table.
|
|
|
|
|
Hi,
I am using VB.NET 1.1 and I am having difficulty in finding a starting point in my array. My array is filled with Product objects. Lets say I want to start at row 301 and display all the products till row 600, how do I do this?
I have the following, but I don't know what to do further:
ByRef productArray As ArrayList (this is a parameter to this function.
Dim p As New Product
Dim i As Integer
For i = intStart To intEnd
For Each p In productArray
Response.Write("SKU: " & p.ID)
Next
Next
Please can someone help me??
Regards
ma se
|
|
|
|
|