Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error converting nvarchar to int 1

Status
Not open for further replies.

MJV57

Programmer
Apr 18, 2009
87
CA
I have the following code that executes a stored procedure and the parameter @customer is in the database as a varchar. Can anyone tell me why im getting the above stated error in this code. Thanks in advance.


SqlConnection conn = new SqlConnection("Data Source=server1;Initial Catalog=estimator;Integrated Security=True");
SqlCommand cmd = new SqlCommand("QuoteCreate", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@estnum", estno);
cmd.Parameters.AddWithValue("@quotenum", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue("@customer", SqlDbType.VarChar).Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue("@stdrfq", SqlDbType.Int).Direction = ParameterDirection.Output;
 
this is the not problem. the exception stack trace will tell you where the error originated from (assuming you do not swallow errors.) if the exception is stating nvarchar as a problem, then the problem may be the stored proc. execute the code on the db server and see if any errors are thrown.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The stored procedure works fine on when executed from the DB
 
post the stack trace and the code that uses the output parameters.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Code:
AddWithValue("@customer", SqlDbType.VarChar)

You're passing an integer (the value of the SqlDbType enum) to your database, which is expecting a string value. Try something like this:

Code:
AddWithValue("@customer", "Bill Gates")

MSDN docs:

Note second parameter is the value, not the column type.

Chip H.



____________________________________________________________________
www.chipholland.com
 
Note second parameter is the value, not the column type.
this is why the error is occurring. the SqlDbType is an Enum which is an integer.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top