C# .NET - Working and giving error "Unable to cast object of type 'System.Byt...

Asked By Gokhan Metepe on 23-Aug-11 06:27 AM

In my project i am uploading a file to SQL,  but after my upload i am getting error;

"Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.Couldn't store <System.Byte[]> in FileDateTime Column. Expected type is DateTime."

My uploaded file is being showed after i reopened program. But i dont want user to get en error during work.

There is my code;

01.if (openFileDialog1.ShowDialog() == DialogResult.OK)
02.      {
03.  
04.        FileInfo fi = new FileInfo(openFileDialog1.FileName);
05.        byte[] data = new byte[fi.Length];
06.        FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
07.        fs.Position = 0;
08.        fs.Read(data, 0, Convert.ToInt32(fi.Length));
09.        SqlConnection conn = new SqlConnection(string.Format(@"Data Source =...")
10.            );
11.        conn.Open();
12.        SqlCommand cmd = new SqlCommand("INSERT INTO Supplementare ([FileName],[Data]) "
13.          + string.Format("values( '{0}',@Data )"
14.            , fi.Name
15.            )
16.            , conn);
17.        SqlParameter pdata = new SqlParameter("@Data", SqlDbType.Image);
18.        pdata.Direction = ParameterDirection.Input;
19.        pdata.SqlValue = data;
20.        cmd.Parameters.Add(pdata);
21.        cmd.ExecuteNonQuery();
22.        conn.Close();
23.        dataSet1.Tables[0].Rows.Add(fi.Name, data);
24.        dataSet1.Tables[0].AcceptChanges();
25.      }
 

Reena Jain replied to Gokhan Metepe on 23-Aug-11 06:34 AM
Hi,

This because you parameter sequence in not matching with query field sequence replace query with this

SqlCommand cmd = new SqlCommand("INSERT INTO Supplementare ([FileName],[Data]) "
+ string.Format("values('" + OpenFileDialog1.FileName + "',@data )"



Gokhan Metepe replied to Reena Jain on 23-Aug-11 06:37 AM

I have tried it now. But am still getting that error ;

Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.Couldn't store <System.Byte[]> in FileDateTime Column. Expected type is DateTime.

Web Star replied to Gokhan Metepe on 23-Aug-11 06:38 AM
you are trying to insert byte data into datetime column that why getting this error so please check first your table's column in sql server and than check the correct column in you sql query for byte datatype to insert image binary data into table.
Anoop S replied to Gokhan Metepe on 23-Aug-11 06:40 AM
This problem occurs looks like only for return from query values, which were included
into some expressions inside of query. So “SELECT MAX(DateTimeField) AS MaxDT FROM
SampleTable” will produce such problem, but for “SELECT DateTimeField FROM
SampleTable” it’ll be ok.

This problem can be handled around, when DateTime will be forced to string type inside of
query, for example: SELECT CONVERT(MIN(LastTS), CHAR) AS TS FROM SampleTable. In this case
execution of 
"DateTime dt = Convert.ToDateTime(ESProc.adpProdLoc.SelectCommand.ExecuteScalar());"
will be without exception.
Reena Jain replied to Gokhan Metepe on 23-Aug-11 06:43 AM
Hi,

Just make sure that file name field in table should be of nvarchar/varchar type and data field should be of byte type in database table, Because its seems you are saving different type of data in different datatype field.
Gokhan Metepe replied to Reena Jain on 23-Aug-11 06:47 AM

Here is my data types;

FileName nvarchar(100)

FileDateTime datetime

Data image

Gokhan Metepe replied to Anoop S on 23-Aug-11 07:02 AM
Where will i use that code?
Working and giving error "Unable to cast object of type 'System.Byt...
Gokhan Metepe replied to Gokhan Metepe on 23-Aug-11 07:10 AM
Still i could not solve the problem. I have tried all of your suggestions. But still i can not do that..
Gokhan Metepe replied to Web Star on 23-Aug-11 08:00 AM
I have done it. Thanks.