Solved

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

Posted on 2013-11-27
3
531 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Save json data from URL using SSIS 1 32
SQL Syntax 6 32
SQL Server In place upgrade from 2012 to 2014 12 23
Inserting LocalDB Table to SQL Server C# 3 21
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 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