Problem with SP output parameter

I have the following code which works fine upto retreiving the output parameter. I know the Stored Procedure is fine. Yet the error I get is 'Object cannot be cast from DBNull to other types.' so the problem is the output parameter isn't being stored. Can anybody help me?

System.Data.OleDb.OleDbConnection Con = new System.Data.OleDb.OleDbConnection(ConfigurationSettings.AppSettings["OLEConnectionString"]);
System.Data.OleDb.OleDbCommand OleDbCmdObj = new System.Data.OleDb.OleDbCommand("Web__InsertImage ?,?,?,?,?", Con);

OleDbCmdObj.Parameters.Add("@image", System.Data.OleDb.OleDbType.Binary, FileLength).Value = FileByteArray;
OleDbCmdObj.Parameters.Add("@imageDescription", System.Data.OleDb.OleDbType.VarChar,50).Value = imageDescription.Text;
OleDbCmdObj.Parameters.Add("@contentType", System.Data.OleDb.OleDbType.VarChar,50).Value = image.ContentType;
OleDbCmdObj.Parameters.Add("@byteSize", System.Data.OleDb.OleDbType.VarChar,100).Value = image.ContentLength;
OleDbCmdObj.Parameters.Add("@imageID", System.Data.OleDb.OleDbType.Integer, 4);

OleDbCmdObj.Parameters[4].Direction = ParameterDirection.Output;


int imageID = System.Convert.ToInt32(OleDbCmdObj.Parameters[4].Value);
10/23/2002 11:58:53 PM 29906 articles. 0 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 57

Are you really sure the sp is correct? I ask this because the

OleDbCmdObj.Parameter[4] has a NULL value ('undefinded'). You can't convert this to an Int.
this code looks OK to me, so I think you don't assign a value to the output parameter in the sp. Maybe show the sp?
and - as a side remark - if you work with SQL server, you should use the SqlConnection instead of the OleDbConnection (in System.Data.SqlClient)
10/24/2002 5:11:21 AM
if OleDbCmdObj.Parameters[4].Value return NULL value you can't convert it into Integer. So, you can write function like DBNullToNothing, which will change NULL value into "". I think it will solve your problem

Good Luck!
With best regards,
Julia Pavlova
Rapid Internet Development
AlarIT Inc.
10/24/2002 5:17:23 AM
I thought you had to use OleDbConnection when dealing with images. Is this the case?
10/24/2002 7:54:45 AM
Don't ya love it when the problem is so simple. For all that are interested.

System.Data.OleDb.OleDbCommand OleDbCmdObj = new System.Data.OleDb.OleDbCommand("Web__InsertImage ?,?,?,?,?", Con);
System.Data.OleDb.OleDbCommand OleDbCmdObj = new System.Data.OleDb.OleDbCommand("Web__InsertImage ?,?,?,?,? output", Con);
output keyword in the sql string arghhh :)
10/24/2002 8:22:14 AM
I tried this and got:

Format of the initialization string does not conform to specification starting at index 63.
Is there a SQL version?
11/15/2003 7:35:41 PM

Web resources about - Problem with SP output parameter -

Parameter - Wikipedia, the free encyclopedia
A parameter (from the Ancient Greek παρά , "para", meaning "beside, subsidiary" and μέτρον , "metron", meaning "measure"), in its common meaning, ...

fb_source Parameter - Facebook-Entwickler
This document lists the complete list of values for the fb_source parameter that is returned with th...

Google Webmaster Tools Parameter Handling - Flickr - Photo Sharing!
You aren't signed in Sign In Help Home The Tour Sign Up Explore Explore Home Last 7 Days Interesting Popular Tags Calendar Most Recent Uploads ...

Cat 8: TIA performance standards committee chairman previews parameters of forthcoming twisted-pair cable ...
Category 8, the next-generation twisted-pair cabling specification, is still in development, but the outlook looks quite positive that it will ...

Philippine president asks for 'parameters' of Mayon Volcano tours
Philippine president asks for 'parameters' of Mayon Volcano tours People's Daily Online ... 26, 27, 28, 29, 30, 31. Jan, Feb, Mar, Apr, May, ...

Alternatives to boolean parameters
... ) { PrintLine ( item . Name ); if ( printDetails ) { PrintLine ( item . Description ); } } } Aha! Now can see that the boolean parameter ...

Exporting Error (Error in Parameter...: Apple Support Communities
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability ...

Apple tech monitors device aging, adjusts operating parameters to ensure optimal performance
... describing a method of monitoring the aging of a device's electronics, comparing the data with a global average and modifying operating parameters ...

Is it poor programming practice to pass parameters as objects?
... a free, community-powered network of 100+ Q&A sites . Risser asked: So, we've got a guy who likes to write methods that take Objects as parameters, ...

Parameter Short Film Competition sponsored by Grolsch
Grolsch presents Parameter The A.V. Club Short Film Competition Please Enter Your Date of Birth Ineligible entry Enter For people over the age ...

Resources last updated: 12/17/2015 4:51:14 AM