Looks like regno is a nvarchar data type in your table and you have passed an int via your your procedure, either use a cast and convert @regno to an nvarchar or change the regno data type to an integer in the table.

DECLARE @regnocast NVARCHAR(15)

SET @regnocast = CAST(@regno AS NVARCHAR)

Then in your SELECT, INSERT and WHERE clauses use @regnocast rather than @regno

Answer from Darren on Stack Overflow
🌐
SQLTeam
forums.sqlteam.com › transact-sql
Error Converting Date Type nvarchar to Numeric - Transact-SQL - SQLTeam.com Forums
June 29, 2021 - Hello. I have a pretty simple Subquery that is giving me heartburn. (Select Max(Case When ah.TransactionType = 'DCA' OR ah.TransactionType = 'DPA' Then ah.TransactionType Else '' End) From AccountHistory ah Where ah.MemberNumber = aud.MemberNumber AND ah.PrincipalAmount = aud.DebitAmount) ah.PrincipalAmount is Numeric .. however, aud.DebitAmount isn't always numeric.
Discussions

.net - "Error converting data type nvarchar to int" when executing Stored Procedure and reading return value - Stack Overflow
I have a clr stored procedure which has an output value I am calling the sp from a vb.net app with the code below. I am getting an error that says 'Error converting data type nvarchar to int. 1125... More on stackoverflow.com
🌐 stackoverflow.com
Converting nvarchar to int
Hello, I need help writing a sql statement that converts nvarchar values (@pricemin,@pricemax) to int. Right now if I try to the condition below I get the error: Conversion failed when converting the nvarchar value to data type int. More on experts-exchange.com
🌐 experts-exchange.com
June 28, 2012
sql - Conversion failed when converting the nvarchar value ... to data type int - Stack Overflow
I got this error when I used a where clause which looked at a nvarchar field but didn't use single quotes. ... This didn't work since the Number column had the data type nvarchar. It wasn't an int as I first thought. More on stackoverflow.com
🌐 stackoverflow.com
Error converting data type nvarchar to int
I am getting this error when I'm attempting to use the engine object to run a stored proc in MSSQL: ProgrammingError at /helpdesk/submit (pyodbc.ProgrammingError) ('42000', '[42000]... More on github.com
🌐 github.com
1
1
June 28, 2024
🌐
Warewolf
community.warewolf.io › communities › 1 › topics › 532-sql-error-error-converting-data-type-nvarchar-to-int
SQL Error: Error converting data type nvarchar to int / Community / Warewolf
Running the same example of the SQL video Connector in debugging mark data type conversion error, the stored procedure has an integer type parameter, in the query analyzer the stored procedure works well
🌐
Laserfiche Answers
answers.laserfiche.com › questions › 72136 › Error-converting-data-type-nvarchar-to-int
Error converting data type nvarchar to int - Laserfiche Answers
February 25, 2015 - Is the expected value in the procedure supposed to be of type int? Your values, after dividing the form value by 100 are in the 0.05 to 1 range, so none but the last one are integers. ... Yes, the expected value is of int. I thought that too, but trying to pass the the Form field without dividing by 100 results in the same error.
🌐
Stack Overflow
stackoverflow.com › questions › 50530270 › error-converting-data-type-nvarchar-to-int-when-executing-stored-procedure-and › 50532349
.net - "Error converting data type nvarchar to int" when executing Stored Procedure and reading return value - Stack Overflow
The problem is that SqlDbType.NVarChar is an enum yet is being used incorrectly here as the "value" to imply the datatype from. The datatype is, by default, int. This is why you are getting an error converting the stored procedure's Result parameter into the int that was declared for the SqlCommand parameter via the line shown above.
🌐
Experts Exchange
experts-exchange.com › questions › 27773882 › Converting-nvarchar-to-int.html
Solved: Converting nvarchar to int | Experts Exchange
June 28, 2012 - In that case you'll need a numeric data type, as integer values do not have decimal places. Also you'll want to handly any scenario where the value has a $ char in it, as you'd have to strip that out before converting to numeric. >Select * From Products >Where AND price>100 AND price <10000 Remove the first AND. >Conversion failed when converting the nvarchar value to data type int.
Find elsewhere
🌐
GitHub
github.com › sqlalchemy › sqlalchemy › discussions › 11543
Error converting data type nvarchar to int · sqlalchemy/sqlalchemy · Discussion #11543
June 28, 2024 - ProgrammingError at /helpdesk/submit (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Error converting data type nvarchar to int. (8114) (SQLExecDirectW)') [SQL: EXEC [dbo].[sp_HelpdeskInitializeTicketUpdate] empid_it_assigned, importance_id] Some details: The issue has to do with the last parameter, importance_id.
Author   sqlalchemy
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 648029 › how-to-solve-error-conversion-failed-when-converti
how to solve error Conversion failed when converting the nvarchar value '24VAC/DC' to data type int ? - Microsoft Q&A
December 1, 2021 - Download Microsoft Edge More info about Internet Explorer and Microsoft Edge ... Conversion failed when converting the nvarchar value '24VAC/DC' to data type int.
🌐
SQLTeam
forums.sqlteam.com › t › conversion-failed-when-converting-the-nvarchar-value-26-19-to-data-type-int › 19464
Conversion failed when converting the nvarchar value '26.19' to data type int - SQLTeam.com Forums
May 17, 2021 - Hello. I what I have: Debit_Amount = SUM(Case When aud.DebitAmount Is Not Null Then aud.DebitAmount Else 0 End) And I keep on getting the referenced error. I have tried: Cast, Convert, nvarchar, varchar .... Any as…
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 403522 › conversion-failed-when-converting-the-nvarchar-val
Conversion failed when converting the nvarchar value '62523.286' to data type int. - Microsoft Q&A
So Pending_Qty>0, needs to be CAST(Pending_Qty as INT)>0. 2 comments Show comments for this answer Report a concern ... Pending_qty column does not have decimal value,but Pending_weight has Decimal value and which in an nvarchar ,now i want to convert it to Decimal cast(Pending_Weight as Decimal(10,0)), But below error is coming Msg 8114, Level 16, State 5, Line 1 Error converting data type ...
Top answer
1 of 4
6

My best guess would be that your problem is in this line:

dm.AddParameters(4, "@boxtype", Convert.ToString(BoxType(lines.GetValue(I).ToString())));

in the procedure @boxtype is declared as an int but I believe this overload of AddParameters will make your parameter of type varchar. I don't know what your data is but one or other seems wrong. I suspect the above line probably should be

dm.AddParameters(4, "@boxtype", Convert.ToInt32(BoxType(lines.GetValue(I).ToString())));

It depends on exactly what BoxType(lines.GetValue(I).ToString())) returns. If it is a string representation of an int then use the above. If it is an int then you don't need the convert at all:

dm.AddParameters(4, "@boxtype", BoxType(lines.GetValue(I).ToString()));

Why I came to this conclusion

I thought I'd add a note on how I debugged this error so that you can understand for future use.

The error message "Error converting data type nvarchar to int" tells us that somewhere is expecting an int but receiving an nvarchar. Nvarchars are only a type in sql so we know that there must be either something in the sql or something in the call to the database.

If it was a problem in the procedure itself you would have noticed it when you were testing the procedure away from the rest of the app. This means it must have been down to the way the procedure is being called.

At this point we look at the parameter list and find that there is one parameter that is an int. We then check that against the code and hey presto, there's our suspicious looking line...

2 of 4
0

It looks to me like your stored procedure expects Boxtype to be an int. You are, however, setting up the SqlCommand with a @BoxType parameter that will be a string - ADO.NET will interpret that the @BoxType parameter is type string. When it runs the query, it will convert @BoxType to an nvarchar. SQL Server will in turn attempt to convert that nvarchar to an int when executing the stored procedure. Since SQL server can't do this, that's causing your error.

Basically, change this line:

dm.AddParameters(4, "@boxtype",
       Convert.ToString(BoxType(lines.GetValue(I).ToString())));

to

dm.AddParameters(4, "@boxtype", 
       Convert.ToInt32(BoxType(lines.GetValue(I).ToString())));

Being sure, of course, that the value in question is always convertible to an int. :)

🌐
Phocas user group
pug.phocassoftware.com › t › convert-nvarchar-to-data-type-numeric-error › 2168
Convert nvarchar to data type numeric error - Phocas user group
April 28, 2022 - I’m trying to create a calculated measure that takes a Sales Quantity * 1.05. The measure itself is now built. But when I try to create the transformed column to do the calculation [Quantity] *1.05, I get an error mess…
Top answer
1 of 2
2

as @bejger said it is because you are sending string instead of int to the contact and depid parameters.

but i think sending the String/VARCHAR value for INT columns is acceptable and it does not throw the exception if it is a valid integer.

it throws Exception when provided String can not be converted into Integer. hence i would suggest you to use Int32.TryParse() method to perform the parsing before sending the value to the parameter.

Int32.TryParse() method returns true if the parsing is successfull otherwise returns false.

Complete Code:

if(!Int32.TryParse(TextBox2.Text,out var contact))
{
  //Error here you can return or display some warning
  return;
}

if(!Int32.TryParse(TextBox4.Text,out var depid))
{
  //Error here you can return or display some warning
  return;
}

SqlCommand commandForSP = new SqlCommand("donateBloodProc",DALobj.openConnection());
commandForSP.CommandType = System.Data.CommandType.StoredProcedure;

commandForSP.Parameters.AddWithValue("@donorName", TextBox1.Text);
commandForSP.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedValue);
commandForSP.Parameters.AddWithValue("@email", TextBox5.Text);
commandForSP.Parameters.AddWithValue("@bloodGroup", DropDownList1.SelectedValue);
commandForSP.Parameters.AddWithValue("@contact_number",contact);
commandForSP.Parameters.AddWithValue("@L_D_D", TextBox3.Text);
commandForSP.Parameters.AddWithValue("@city", DropDownList2.SelectedValue);
commandForSP.Parameters.AddWithValue("@arid_number", TextBox1.Text);
commandForSP.Parameters.AddWithValue("@DepId", depid);

commandForSP.ExecuteNonQuery();
2 of 2
0

The problem is with these lines:

commandForSP.Parameters.AddWithValue("@contact_number", TextBox2.Text);
commandForSP.Parameters.AddWithValue("@DepId", TextBox4.Text);

Because you try to pass string values, whereas your columns are of integer type. You should try to do this in such a way:

commandForSP.Parameters.AddWithValue("@contact_number", int.Parse(TextBox2.Text));
commandForSP.Parameters.AddWithValue("@DepId", int.Parse(TextBox4.Text));

Although be aware that this is not fully error-proof (so if there is no value in the textbox it will throw an exception).

To summarize, your code will look like this:

SqlCommand commandForSP = new SqlCommand("donateBloodProc",DALobj.openConnection());
commandForSP.CommandType = System.Data.CommandType.StoredProcedure;
commandForSP.Parameters.AddWithValue("@donorName",TextBox1.Text);
commandForSP.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedValue);
commandForSP.Parameters.AddWithValue("@email", TextBox5.Text);
commandForSP.Parameters.AddWithValue("@bloodGroup",DropDownList1.SelectedValue);
commandForSP.Parameters.AddWithValue("@contact_number", int.Parse(TextBox2.Text));
commandForSP.Parameters.AddWithValue("@L_D_D",TextBox3.Text);
commandForSP.Parameters.AddWithValue("@city", DropDownList2.SelectedValue);
commandForSP.Parameters.AddWithValue("@arid_number", TextBox1.Text);
commandForSP.Parameters.AddWithValue("@DepId", int.Parse(TextBox4.Text));
commandForSP.ExecuteNonQuery();
🌐
Stack Exchange
dba.stackexchange.com › questions › 249192 › conversion-failed-when-converting-the-nvarchar-value-value-to-int
sql server - Conversion failed when converting the nvarchar value 'value' to int - Database Administrators Stack Exchange
September 19, 2019 - Since #Proc_Concat and #Proc50 only have two columns in play here, PatientID and ProcedureCodes, one of them is throwing the error. Guessing not ProcedureCodes as it's pipe-delimeted so must be a varchar. That leaves PatientId. Numeric data types can't handle the space character '1111111 11100', so that's the first place I'd look. Another possibility is within your subquery, P1.PatientID = P2.LbPatientID, in case one is a varchar and one is an int, and implicit conversion won't allow the varchar value to be converted to an int to test the equality.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 91443 › error-converting-data-type-nvarchar-to-numeric
Error converting data type nvarchar to numeric - Microsoft Q&A
As Tom said,you need to convert the data type of null to the data type of the corresponding column. I did a test and found that it can be executed successfully after changing the data type of null, please refer to: create TABLE #orders (QtyOrdered VARCHAR(50)) INSERT #orders SELECT '19' UNION ALL SELECT 'Why?' UNION ALL SELECT '1.84736378483933' UNION ALL SELECT '1e3' UNION ALL SELECT '$1.40' select NULL as name, Null as id, Null as batch into test from #orders update test set name='Amy'