|
Hi, I have tried to upload a file to a MySQL database for a week now and still have problems. Would you please check to see what is causing the exception "Invalid cast from System.String to System.Byte[]." The following are my code for uploading a file to the database. I would really like to know why its giving me that error message, thank you in advance for your help.
----------------------------------------------------------------------------------------------------------------------------------------
| 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 strFileEx 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
strFileEx = Right(txtFileContents.PostedFile.FileName, 4)
Select Case strFileEx.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=myID;Password=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.Text)
cmdInsert.Parameters["@docContent"].Values =arrFile
myConnection.Open()
cmdInsert.ExecuteNonQuery()
myConnection.Close()
End If
End Sub
End Class
|
|
|
|
|
Hi,
Your parameter "@docContent" its datatype should be OdbcType.Binary or OdbcType.VarBinary.
Coz these two datatypes are mapped to an array of Byte.
I hope this will help you.
Thanks and Regards,
Chetan Ranpariya
|
|
|
|
|
I have changed that error you have pointed out but I still get the same error message. The change from Odbctype.Text to OdbcType.Binary had been made for @docContent and still VS2003 throws the same exception. I don't know what else to do, all of the other variables have been checked for invalid type
casting and I don't see any. Please help me spot the error. Thank you in advance for your help.
-- modified at 23:29 Thursday 7th June, 2007
|
|
|
|
|
Hi,
R u getting the same error at the same line as previous one?
Thanks and Regards,
Chetan Ranpariya
|
|
|
|
|
Yes, I get the same error message as before. I even tried changing Binary to VarBinary and that didn't help either. Please take at look at my code for me to see where the problem is. VS2003 says it's the line that says cmdInsert.ExecuteNonQuery() that is causing the problem and it marked that line red. Is there anyway that you can run my code to see where the problem is? Please help me solve this problem, I ran out of ideas. Thanks again for your time.
-- modified at 11:42 Friday 8th June, 2007
|
|
|
|
|
|
my cookie object is deleted when i run application at second time. why? though i gave expiry days.
|
|
|
|
|
are you overwriting some blank data at second time?
Regards,
Sylvester G
sylvester_g_m@yahoo.com
|
|
|
|
|
Hi ,
I would like to add item in web control RadioButtonList using java script .
Any help is appriciated.
Regards
Rajesh
rajesh
|
|
|
|
|
function RadioChecked(param)
{
{
var frm = document.forms[0];
// Take all elements of the form
for (i=0; i < frm.length; i++)
{
// itinerate the elements searching "RadioButtons"
if (frm.elements[i].type == "radio")
{
// Unchecked if the RadioButton is != param
if (param != frm.elements[i].id )
{
frm.elements[i].checked = false;
}
}
}
}
}
Regards,
Satips.
|
|
|
|
|
Thanks Satips,
But my question is how to add item in RadioButtonList .
Like I write a code in code behind .cs file
RadioButtonList.item.add(a);
RadioButtonList.item.add(b);
RadioButtonList.item.add(c);
RadioButtonList.item.add(d);
RadioButtonList.item.add(e);
Similarly , i want to add item in RadioButtonList using java script.
How can i do it?
Regards
Rajesh
rajesh
|
|
|
|
|
What method do you think is better when there are many threads?
Regards,
Analizzatore Cloro
|
|
|
|
|
You can't do that. The RadionButtonList control doesn't exist in the browser, it only exists on the server while the page is created.
As what you are asking for is impossible, what is it that you want to accomplish?
---
single minded; short sighted; long gone;
|
|
|
|
|
You cannot do as how you have stated for the Radio Button list.
Since the Radio buttonlist control exist on the server side while the page is creatred this method is impossible.
so go with the method as how i have stated before.
Regards,
Satips.
|
|
|
|
|
hi all, i want to make a simple project, i want to put a dropdown list to make a list of the countries's times as the one you can see on the time zoon on the windows.
-- modified at 7:44 Thursday 7th June, 2007
Thanks alot
Hamody
|
|
|
|
|
One option is to enumerate the subkeys of
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
Regards,
Sylvester G
sylvester_g_m@yahoo.com
|
|
|
|
|
hi, thanks a lot for urhelp but mayu give me a code sample cause i didnt understand how 2 do it.
Thanks alot
Hamody
|
|
|
|
|
how to fix the size and position of text box or other control that size would not be changed?
|
|
|
|
|
|
just give some values the its properties according to your requirements
|
|
|
|
|
I have the master page having middle part as content template and on left and right
on master page I am displaying images which are changing on each refreshment.
In IE if I refresh the page it comes to master page pageload event (Postback=false)
and executes the functions (for displaying the images right and left )in condition postback==false
In IE it comes ones on this page(master page) BUT IN MOZZILLA if I refresh the page it comes two times on pageload and execute that functions two times on page load of master page....and changed images that i bind to repeater to display is not diplayed ...
sjs
|
|
|
|
|
I am displaying multiple rows and columns in a table dynamically i want to keep page index or next previous link to the table with out using grid, but the functionality must be like page indexer in grid view
|
|
|
|
|
Hi,
Your idea of using table instead of Gridview and Datagrid is not good.
But to implement the functionality u want to, you should put linkbuttons for Prev and Next functionality and should write your own logic in their click event.
The scenario is similar to implementing Custom Paging in Gridview/datagrid.
Do search on CustomPaging you will get idea about that.
Thanks and Regards,
Chetan Ranpariya
|
|
|
|
|
I created a file called Mypageaspx.aspx.
I stuck a load of code in it before i realised that i got the name wrong so i changed it to Mypage.aspx.
When i looked in the aspx file the inherits="MynameSpace.Mypageaspx" I changed this to Mynamespace.Mypage and searched for Mypageaspx in the project and nothing found. Now the code won't compile because "The name 'ControlName' does not exist in the current context". Changing this back makes the project compile again.
Every time i rename a page in asp.net i have problems and i suspect that the inherits tag value has something to do with it. If it doesn't actually relate to any other part of my project I don't understand what it does.
Can anyone help, I'd be especially grateful if someone could show me how to rename a file without the project blowing up every time.
Thanks
Russell
|
|
|
|
|
In the ASP.NET code-behind (or code-beside for 2.0) model an aspx page and the code file for it are connected in two ways. The CodeBehind (or CodeFile for 2.0) atribute in the Page directive contains the physical filename (like Mypage.aspx.cs) while the Inherits contains the class (or partial class for 2.0) for the page. Renaming the page changes the filenames, and hence the CodeBehind/CodeFile attribute's value. However it does not need to, and therefore also doesn't, rename the class in the codefile. Inherits should only be changed if the name of the class is also changed manually.
|
|
|
|