Solved

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

Posted on 2013-11-27
3
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 36

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 36

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

728 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