You Declare Phone No as Numeric Field So You need to Convert Your Text into Integer

 [cPhoneNo] NUMERIC (18)   NOT NULL
 [cFax]     NUMERIC (18)   NOT NULL

Use like this:

com.Parameters.AddWithValue("@phoneno", Convert.ToInt32(contactnumberTB.Text));
com.Parameters.AddWithValue("@fax", Convert.ToInt32(faxTB.Text));

STOP USING ADD WITH VALUES

Answer from Dgan on Stack Overflow
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. :)

🌐
GitHub
github.com › HangfireIO › Hangfire › issues › 1880
SqlException (0x80131904): Error converting data type nvarchar to bigint. · Issue #1880 · HangfireIO/Hangfire
June 15, 2021 - Content root path: E:\vsproject\net5\WebApplication1 fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HM9FPPD1S4MM", Request id "0HM9FPPD1S4MM:0000000D": An unhandled exception was thrown by the application. System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to bigint.
Author   gitlsl
🌐
Stack Overflow
stackoverflow.com › questions › 29391703 › errorsystem-data-sqlclient-sqlexception-0x80131904-conversion-failed-when-co
c# - Error:System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting the nvarchar value - Stack Overflow
Error:System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting the nvarchar value 'ff' to data type int. at System.Data.SqlClient.SqlConnection.OnError(SqlException except...
🌐
Stack Overflow
stackoverflow.com › questions › 36958263 › error-converting-data-type-nvarchar-to-int-using-stored-procedure
c# - Error converting data type nvarchar to int using stored procedure - Stack Overflow
CREATE PROCEDURE AddStudent @password nvarchar(50), @email nvarchar(50), @role_id int, @reset_hash nvarchar(50), @student_id int, @f_name nvarchar(50), @l_name nvarchar(50), @course_id int, @year int, @dob datetime, @parttime_fulltime nvarchar(50), @personal_tutor_id int, @advisor int, @status_id int, @academic_year int, @profile_image nvarchar(max) AS INSERT INTO users (password, email, role_id, reset_hash) VALUES (@password, @email, @role_id, @reset_hash); SET @student_id = SCOPE_IDENTITY() INSERT INTO student_records (user_id, f_name, l_name, course_id, year, dob, parttime_fulltime, personal_tutor_id, advisor, status_id, academic_year, profile_image) VALUES (@student_id, @f_name, @l_name, @course_id, @year, @dob, @parttime_fulltime, @personal_tutor_id, @advisor, @status_id, @academic_year, @profile_image)
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 56044213 › error-system-data-sqlclient-sqlexception-error-converting-data-type-nvarchar-t
sql - Error System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' in c# - Stack Overflow
I'm inserting data from one database table to another with a buttonclick and this error shows up System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' in both tables the data types matches. The data types are id = int, Druh = varchar(50), Nazov = varchar(50), Cena = numeric(18,2).
🌐
GitHub
github.com › zzzprojects › EntityFramework-Plus › issues › 664
Microsoft.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value 'Value1' to data type int.' · Issue #664 · zzzprojects/EntityFramework-Plus
December 24, 2020 - If you are seeing an exception, include the full exceptions details (message and stack trace). Microsoft.Data.SqlClient.SqlException HResult=0x80131904 Message=Conversion failed when converting the nvarchar value 'Value1' to data type int.
Author   t11omas
Top answer
1 of 4
8

Usually it's not a problem to pass a string to a parameter that's numeric, as long as the SQL Server is able to convert the content of the string to a numeric value itself. If that doesn't work, you get this error.

For example: Passing "Hello" to a parameter that's numeric, you get an error. Passing "1234" you don't. Please note that an empty string or a string containing whitespace can not be converted to a numeric value!

However, it should be said that it is not good style to do that. You should make sure that the types you use in your application match the types in the database to avoid problems. Maybe some further detail on why you need to have string types in your application could help.

EDIT 1
To make a parameter optional for the query, the way to go would be the following:

  1. Change your SQL statement to allow optional parameters like WHERE RAUMKLASSE_ID = ISNULL(@Raumklasse_ID, RAUMKLASSE_ID).
  2. Do not add the @Raumklasse_ID parameter if it should be optional or add the value DBNull.Value

You should really consider changing your string properties to nullable types like int?.

EDIT 2
This is how your code could look implementing the changes I suggested in Edit 1:

using (SqlCommand cmd = new SqlCommand(@"SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID FROM RAUM r WHERE RAUMKLASSE_ID = ISNULL(@Raumklasse_ID, RAUMKLASSE_ID) OR STADT_ID = ISNULL(@Stadt_ID, STADT_ID) OR GEBAEUDE_ID = ISNULL(@Gebaeude_ID, GEBAEUDE_ID) OR REGION_ID = ISNULL(@Region_ID, REGION_ID)", con)) 
{
    con.Open();
    if (!String.IsNullOrWhitespace(RAUMKLASSE_ID))
        cmd.Parameters.AddWithValue("@Raumklasse_ID", RAUMKLASSE_ID);
    else
        cmd.Parameters.AddWithValue("@Raumklasse_ID", DBNull.Value);
    if (!String.IsNullOrWhitespace(STADT_ID))
        cmd.Parameters.AddWithValue("@Stadt_ID", STADT_ID);
    else
        cmd.Parameters.AddWithValue("@Stadt_ID", DBNull.Value);
    if (!String.IsNullOrWhitespace(GEBAEUDE_ID))
        cmd.Parameters.AddWithValue("@Gebaeude_ID", GEBAEUDE_ID);
    else
        cmd.Parameters.AddWithValue("@Gebaeude_ID", DBNull.Value);
    if (!String.IsNullOrWhitespace(REGION_ID))
        cmd.Parameters.AddWithValue("@Region_ID", REGION_ID);
    else
        cmd.Parameters.AddWithValue("@Region_ID", DBNull.Value);
    ...
}
2 of 4
0

The problem might be that you're not giving anything to infer the data type from as you're not setting the value of the parameter.

Try setting the parameters using the Add method as follows:

cmd.Parameters.Add("@Raumklasse_ID", SqlDbType.Int).Value = RAUMKLASSE_ID;

That way if you don't want to set the value, you can still define the parameter.

See here for more: http://msdn.microsoft.com/en-us/library/wbys3e9s

🌐
GitHub
github.com › sqlalchemy › sqlalchemy › discussions › 11543
Error converting data type nvarchar to int · sqlalchemy/sqlalchemy · Discussion #11543
June 28, 2024 - import sqlalchemy as sa engine = sa.create_engine("mssql+pyodbc://scott:tiger^5HHH@msodbcsql18_199") with engine.begin() as conn: conn.exec_driver_sql("DROP PROCEDURE IF EXISTS sproc1") conn.exec_driver_sql("""\ CREATE PROCEDURE sproc1 @some_text nvarchar(50), @importance_id int AS BEGIN SET NOCOUNT ON; -- this sp doesn't actually do anything END """) with engine.begin() as conn: stmt = sa.text("EXEC sproc1 :some_text, :importance_id") conn.execute(stmt, {"some_text": "val1", "importance_id": 123}) # no error conn.execute(stmt, {"some_text": "val1", "importance_id": "123"}) # also no error conn.execute(stmt, {"some_text": "val1", "importance_id": "1,000"}) # Error converting data type nvarchar to int.
Author   sqlalchemy