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.net - "Error converting data type nvarchar to int" when executing Stored Procedure and reading return value - Stack Overflow
Converting nvarchar to int
sql - Conversion failed when converting the nvarchar value ... to data type int - Stack Overflow
Error converting data type nvarchar to int
Videos
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...
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. :)
@Gwashoppa, thx for taking time to answer my question. To my understanding return values are not to be mixed with OUTPUT parameters. But in any case I found the solution - data type and length needs to be set explicitly:
cmd.Parameters["@mobile"].SqlDbType = SqlDbType.NVarChar;
cmd.Parameters["@mobile"].Size = 50;
I thought that happened in the AddWithValue function, but apparently not.
Your C# code looks fine... The fact that the line cmd.ExecuteNonQuery(); throws an exception, signifies that the error must lie with some parameter used by the SP.
You will need to isolate your SP in steps to identify the problem area...
For instance, in this statement does the ResetHistory table have the same column types used in your select?
INSERT INTO ResetHistory ([User_ID], MobilePhone, Company_ID, [System], [Status], ResetIP)
SELECT rq.[User_ID], u.MobilePhone, u.Company_ID, rq.[System], '3', rq.ResetIP
FROM ResetQueue rq
...
In this statement, does the mobilephone column return a Varchar value?
SELECT u.[MobilePhone]
FROM [user] u
WHERE u.ObjectGuid = @user
In this statement, does the system column return a Varchar value?
SELECT rq.[system]
FROM ResetQueue rq
WHERE rq.[User_ID] = @user
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();
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();
Assuming your first table is named A with a column named MyIntColumn of type int and your second table is named B with a column named MyNVarcharColumn of type nvarchar, you could do
From A
Inner Join B On A,MyIntColumn = Try_Cast(B.MyNVarcharColumn As int)
Tom
Hi All,
I faced the same issue whenever a maintenance job was done by Azure Managed Service. The strange thing is that I got this exception from my services, and it was only resolved after a restart.
What happened in between?