Error Description:
I have a table of Database, one item string "a" of this table can be set as null. Now I want to insert a row to this database, and use 'null' to assign variable "a". the codes can be :
string a=null;
.......
new SqlParameter("@a", a),
.......
Unluckly, I got an error: "expects parameter '@a' , which was not supplied
Solution:
the reason of it is because the difference of C# datatype and SQLserver datatype
we must use the null formate of SQLserver and not C#, so the code should be:
new SqlParameter("@a", ObjString(a))
which ObjString function is:
private object ObjString(string param)
{
return param == null ? (object)DBNull.Value : (object)param;
}
Reference:
[1]. http://forums.asp.net/t/1047321.aspx