"Object must implement IConvertible." Error when Executing Stored Procedure

G

Guest

Hi Folks I got a strange Error when executing a Stored Procedure, can Somebody help me

This is my Error Message

Server Error in '/OMAS' Application
-------------------------------------------------------------------------------

Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Object must implement IConvertible

Source Error:

Line 333:
Line 334: // Finally, execute the comman
Line 335: int retval = cmd.ExecuteNonQuery()
Line 336:
Line 337: // Detach the SqlParameters from the command object, so they can be used agai


Source File: d:\inetpub\wwwroot\OMAS\SQLHelper.cs Line: 335

Stack Trace:

[InvalidCastException: Object must implement IConvertible.
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream
System.Data.SqlClient.SqlCommand.ExecuteNonQuery(
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters) in d:\inetpub\wwwroot\OMAS\SQLHelper.cs:33
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) in d:\inetpub\wwwroot\OMAS\SQLHelper.cs:25
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(String connectionString, String spName, Object[] parameterValues) in d:\inetpub\wwwroot\OMAS\SQLHelper.cs:28
OMAS.IO.Button1_ServerClick(Object sender, EventArgs e) in d:\inetpub\wwwroot\omas\io.aspx.cs:10
System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e
System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData
System.Web.UI.Page.ProcessRequestMain(

-------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

And this is my Code:

Guid FileName = Guid.NewGuid()
String GuidS = "{" + FileName.ToString() +"}"
String SaveAs = "E:\\Stacks\\" + FileName.ToString()
File.SaveAs(SaveAs)

FileIO myIO = new Dickenbestimmung.FileIO()
int[] resolution
float[] length
resolution = new int[] {0,0,0,0}
length = new float[] {0,0,0,0 }

int canFloat = myIO.dbl_read(SaveAs,ref resolution,ref length)

double[] bits = (double[]) myIO.data

//Works fine! The Data is in the Array like it should be

//Create procedure paramete
object[] obj = new object[5];
obj[0] = bits
obj[1] = Name.Text
obj[2] = SaveAs
obj[3] = GuidS
obj[4] = resolution[0]

SqlHelper.ExecuteNonQuery(connectionString,"sp_add_stack",obj);

What is wrong here? Some Problem with the "image" Data Type

Ah, this is the Code of my Procedure sp_add_stack

CREATE PROCEDURE sp_add_stac

@Data image
@name nvarchar(50)
@FileName nvarchar(50)
@StackID uniqueidentifier
@XRes in
) A
insert into StackData (Data,stackname,StackID,XRes) values(@Data,@name,@StackID,@XRes
G
 
N

Nicholas Paldino [.NET/C# MVP]

Chucker,

It could be. I would think that for the image data type, you need to
pass in an array of bytes, not an array of doubles. I would try converting
it to bytes, and then trying it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chucker said:
Hi Folks I got a strange Error when executing a Stored Procedure, can Somebody help me?

This is my Error Message:

Server Error in '/OMAS' Application.
-------------------------------------------------------------------------- ------

Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object must implement IConvertible.

Source Error:


Line 333:
Line 334: // Finally, execute the command
Line 335: int retval = cmd.ExecuteNonQuery();
Line 336:
Line 337: // Detach the SqlParameters from the command object, so they can be used again


Source File: d:\inetpub\wwwroot\OMAS\SQLHelper.cs Line: 335

Stack Trace:


[InvalidCastException: Object must implement IConvertible.]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(SqlConnection
connection, CommandType commandType, String commandText, SqlParameter[]
commandParameters) in d:\inetpub\wwwroot\OMAS\SQLHelper.cs:335
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(String
connectionString, CommandType commandType, String commandText,
SqlParameter[] commandParameters) in
d:\inetpub\wwwroot\OMAS\SQLHelper.cs:252
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(String
connectionString, String spName, Object[] parameterValues) in
d:\inetpub\wwwroot\OMAS\SQLHelper.cs:286
OMAS.IO.Button1_ServerClick(Object sender, EventArgs e) in d:\inetpub\wwwroot\omas\io.aspx.cs:103
System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e)
System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandl
er.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()

-------------------------------------------------------------------------- ------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

And this is my Code:

Guid FileName = Guid.NewGuid();
String GuidS = "{" + FileName.ToString() +"}";
String SaveAs = "E:\\Stacks\\" + FileName.ToString();
File.SaveAs(SaveAs);

FileIO myIO = new Dickenbestimmung.FileIO();
int[] resolution;
float[] length;
resolution = new int[] {0,0,0,0};
length = new float[] {0,0,0,0 };

int canFloat = myIO.dbl_read(SaveAs,ref resolution,ref length);

double[] bits = (double[]) myIO.data;

//Works fine! The Data is in the Array like it should be!

//Create procedure parameter
object[] obj = new object[5];
obj[0] = bits;
obj[1] = Name.Text;
obj[2] = SaveAs;
obj[3] = GuidS;
obj[4] = resolution[0];

SqlHelper.ExecuteNonQuery(connectionString,"sp_add_stack",obj);

What is wrong here? Some Problem with the "image" Data Type?

Ah, this is the Code of my Procedure sp_add_stack:

CREATE PROCEDURE sp_add_stack
(
@Data image,
@name nvarchar(50),
@FileName nvarchar(50),
@StackID uniqueidentifier,
@XRes int
) AS
insert into StackData (Data,stackname,StackID,XRes) values(@Data,@name,@StackID,@XRes)
GO
 
G

Guest

Thanks for your help. I think you are right. But how can I convert fro
Double[] to Byte[]? I tried the following, but it does not work

ByteConverter myconv = new ByteConverter()

object[] obj = new object[5];
obj[0] = myconv.ConvertFrom(myIO.data); (myIO.data is double[] but I need byte[] for the Parameter
obj[1] = Name.Text
obj[2] = SaveAs
obj[3] = GuidS
obj[4] = resolution[0]

SqlHelper.ExecuteNonQuery(connectionString,"sp_add_stack",obj)

There has to be some class in the Framework to do this but wich

Cheer

Martin
 
G

Guest

I found a way but I don´t like it

There has to be an easier way to do this


Guid FileName = Guid.NewGuid()
String SaveAs = "E:\\Stacks\\" + FileName.ToString()

File.SaveAs(SaveAs)

FileIO myIO = new Dickenbestimmung.FileIO()
int[] resolution
float[] length
resolution = new int[] {0,0,0,0}
length = new float[] {0,0,0,0 }

int canFloat = myIO.dbl_read(SaveAs,ref resolution,ref length)
int maxindex = resolution[0]*resolution[1]*resolution[2]*resolution[3]


byte[] ByteData = new byte[8*maxindex]
byte[] actDbl = new byte[8]
for (int i = 0; i<maxindex;i++

actDbl = BitConverter.GetBytes(myIO.data)
for (int p = 0; p<8;p++) ByteData[(8*i)+p]=actDbl[p]


object[] obj = new object[5];
obj[0] = ByteData
obj[1] = Name.Text
obj[2] = SaveAs
obj[3] = FileName
obj[4] = resolution[0]

SqlHelper.ExecuteNonQuery(connectionString,"sp_add_stack",obj);
 
N

Nicholas Paldino [.NET/C# MVP]

Chucker,

What you need to do is create an array of bytes that has 8 times the
elements in the double array. Then, you have to convert each double into an
array of eight bytes, and copy those bytes array into your new byte array at
the appropriate indexes (0, 8, 16, etc, etc).
 
G

Guest

I did that and it works. I just think it is complicated. I did a nee post with the Subject Convert double[] to byte[
where I go into Detail. Thanks for your efforts! Marti
 
N

Nicholas Paldino [.NET/C# MVP]

Chucker,

You might also want to check out the static BlockCopy method on the
Buffer class. It can perform the copy without having to loop through the
elements (it will perform the copy for you).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chucker said:
I did that and it works. I just think it is complicated. I did a nee post
with the Subject Convert double[] to byte[]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top