I have the following code in c# for sql insert:
using (DM dbManager = new DM())
{
dbManager.Command.CommandText = @"Insert into TransactionLog(TransactionNumber,Amount
,studentID) Values (@trnumber,@amount,@studentID) ; SELECT SCOPE_IDENTITY() ;";
dbManager.Command.Parameters.AddWithValue("@trnumber", txnref);
dbManager.Command.Parameters.AddWithValue("@amount", amount);
dbManager.Command.Parameters.AddWithValue("@studentID",
Membership.GetUser().ProviderUserKey.ToString());
id = Convert.ToInt32(dbManager.Command.ExecuteScalar());
}
But failing to understand why am i getting this error:
System.Data.SqlClient.SqlException: The parameterized query '(@trnumber
nvarchar(4000),@amount nvarchar(5),@studentID nvarcha' expects the
parameter '@trnumber', which was not supplied.
Because the parameter is clearly in the query i tried changing the name of parameter same issue.
Any suggestions?
DMexactly? Can you please show it's structure as well?txnrefis null, then theAddWithValuewill mean that theSqlValueproperty of the parameter is null, and it is as if the parameter is not passed (remembernullin c# andNULLin the DB are not the same thing). I would personally use the more strongly type method -dbManager.Command.Parameters.Add("@trnumber", SqlDbType.NVarChar, 4000).Value = txnref ?? string.EmptyDBNullrather than an empty string -dbManager.Command.Parameters.Add("@trnumber", SqlDbType.NVarChar, 4000).Value = string.IsNullOrEmpty(txnref) ? (object)DBNull.Value : (object)txnref;