Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

747 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

12 Experts available now in Live!

Get 1:1 Help Now