Solved

C# casting error = when casting from number the value must be a number

Posted on 2013-11-27
3
525 Views
Last Modified: 2013-12-01
when I excute my application, , the error - 'when casting from number the value must be a number'
I am not sure if it's blowing up on this statment

retVal = (int)cmd.Parameters["@pIntErrDescOut"].Value;    
"@pIntErrDescOut    is a value passed from the stored procedure and its data type is int.

 
Also I am inserting a value into the database by setting this parameter cmd.Parameters["@PerLevel"].Value = PerLevel.Text.Trim();  --- The value data type is int in the database.  How can I convert to numeric before inserting.

I am using SQL Server 2005
0
Comment
Question by:cookiejar
  • 2
3 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39682123
How are you defining the ouput parameter? The Direction is cirtical to make it work.

//Your code setups up correctly the SQL connection, command and input parameter
SqlParameter retval = sqlcomm.Parameters.Add("@pIntErrDescOut", SqlDbType.Int);
retval.Direction = ParameterDirection.ReturnValue;
sqlcomm.ExecuteNonQuery(); 
int retunvalue = retval.Value;

Open in new window

where sqlComm is your SqlCommand instance
and retval contains your parameter.
0
 

Author Comment

by:cookiejar
ID: 39682326
I tired the snippet of code you gave me and I get this error:

Cannot implicitly convert type object to int
An explicit conversion exist. Are you missing a cast?

Sqlcon.Open();
                        cmd.Connection = Sqlcon;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "Inventory.AdminPermissLevel_sp";
                        cmd.Parameters.Add(new SqlParameter("@pvchAction", SqlDbType.VarChar, 50));
                        cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar, 50));
                        cmd.Parameters.Add(new SqlParameter("@PerLevel", SqlDbType.Int));
                        cmd.Parameters["@pvchAction"].Value = "insert";
                        cmd.Parameters["@UserName"].Value = UserName.Text.Trim();
                        cmd.Parameters["@PerLevel"].Value = PerLevel.Text.Trim();
                        SqlParameter retval = cmd.Parameters.Add("@pIntErrDescOut", SqlDbType.Int);
                        retval.Direction = ParameterDirection.ReturnValue;
                        cmd.ExecuteNonQuery();
                        int returnValue = retval.Value;
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 39682557
Apologies, retval.Value is of type object not int, please replace last line of your code snippet with:
int returnValue = (int)retval.Value; 

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now