No More Solutions

You can find a lot of debugging and deploying problems while developing applications in .NET and Visual Basic here. There are also some querying tips in SQL and typical source codes which might be useful shared here.

Error Converting Data Type Int to Nvarchar

Error: Error converting data type int to nvarchar

This error occurs when a stored procedure which returns a value is used.

e.g
CREATE PROCEDURE [dbo].[spr_SP1]
@ColA INT,
@AutoID INT OUTPUT
AS
BEGIN
 INSERT INTO TableA ( ColA )
 VALUES ( @ColA )

 SELECT @AutoID = SCOPE_IDENTITY();
END

When adding the parameter to the command on the code, usually the code used is:
command.Parameters.AddWithValue("@AutoID", System.DBNull.Value)
command.Parameters("@AutoID").Size = 4
command.Parameters("@AutoID").Direction = ParameterDirection.Output

However, this portion of code would possibly cause error during deployment. To avoid the error, use the code below instead:

Dim param = New System.Data.SqlClient.SqlParameter()
param.ParameterName = "@AutoID"
param.Direction = ParameterDirection.Output
param.Size = 8
command.Parameters.Add(param)
Bookmark and Share

2 comments:

  1. Mobile App Developers said...
     

    Great article, Thanks for your great information, the content is quiet interesting. I will be waiting for your next post.

  2. UI/UX Studio Gurgaon said...
     

    I am extremely impressed along with your writing abilities and also with the format in your blog.
    Stay up to the excellent high quality writing, it's rare to find a nice weblog like this one these
    days.

Post a Comment