I know this question has been asked several times and I read those with no luck 🙁 So I am asking it again with my code.
I have created a stored procedure to update a database table.
Stored procedure:
CREATE PROCEDURE usp_PettyCash_EditExpenseInfo
(
@ExpenseID bigint,
@ExpenseName varchar(100),
@SAPCode varchar(50),
@MaxLimit decimal,
@ExpenseType varchar(50)
)
AS
BEGIN
UPDATE t_ExpenseInfo
SET ExpenseName = @ExpenseName,
SAPCode = @SAPCode,
MaxLimit = @MaxLimit,
ExpenseType = @ExpenseType
WHERE
ExpenseID = @ExpenseID
END
GO
But when I call this from code behind with below code, it gives the exception similar to
Error in converting from nvarchar to bigint
Code behind:
oOleDbCommand.Parameters.AddWithValue("@ExpenseName", oInputExpense.ExpenseName.ToString());
oOleDbCommand.Parameters.AddWithValue("@SAPCode", oInputExpense.SAPCode.ToString());
oOleDbCommand.Parameters.AddWithValue("@MaxLimit", Convert.ToDecimal(oInputExpense.MaxLimit));
oOleDbCommand.Parameters.AddWithValue("@ExpenseType", oInputExpense.ExpenseType.ToString());
oOleDbCommand.Parameters.AddWithValue("@ExpenseID", Convert.ToDouble(oInputExpense.ExpenseID.ToString()));
I also tried this:
oOleDbCommand.CommandText = "UPDATE t_ExpenseInfo SET ExpenseName='" + oInputExpense.ExpenseName.ToString() + "', SAPCode='" + oInputExpense.SAPCode.ToString() + "', MaxLimit=" + oInputExpense.MaxLimit + ", ExpenseType='" + oInputExpense.ExpenseType.ToString() + "' WHERE ExpenseID=" + oInputExpense.ExpenseID + "";
this DIRECT command runs OK from SQL Query Analyzer from from code behind this also give an “Access Violation” error.
I am really confused about what to do? Please help !
is there any particular reason why you’re using
OleDbCommandinstead of the “native” SQL ServerSqlCommand??I would try this:
Basically, I prefer using the native
SqlConnection/SqlCommandover the old, legacy OleDb stuff, and I also prefer to explicitly specify what type my parameters are – don’t rely on ADO.NET or some other part figuring it out automagically – when it has to guess, it can get it wrong, and error like the one you see might occur. If you define it yourself, you’re in control!