Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSAS Store Forecasting data in the cube 1 17
SQL Dump exec output to table 3 21
Parse this column 6 27
C# Linq Select From List 3 10
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

856 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