Monday, February 20, 2012

Null parameter in uniqueidentifier field

I have the following database structure:

The table aspnet_users, which we all know

Another table with an uniqueidentifier which references the user iD

I want to set the value of the iD in one of the entries in the second table to null.

I tried this:
string assigned is null, i am passing this as a method parameter
............
string queryString = "UPDATE [table2] SET AssignedUserId=@.assigned WHERE ProblemId = @.id";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
...............
System.Data.IDataParameter dbParam_au_pr = new System.Data.SqlClient.SqlParameter();
dbParam_au_pr.ParameterName = "@.assigned";
if (assigned == null)
{
dbParam_au_pr.Value = null;
}
else
{
dbParam_au_pr.Value = assigned;
}
dbParam_au_pr.DbType = System.Data.DbType.String ;
dbCommand.Parameters.Add(dbParam_au_pr);

I also tried using "" instead of null, or not using that "if" statement at all.
I get an error that says:

Parameterized Query '(@.id nvarchar(1),@.assignednvarchar(4000))UPDATE [tracker_Probl' expects parameter @.assigned,which was not supplied.

Please help with this as soon as possible.ThanksTry using DBNull.Value instead of null:
dbParam_au_pr.Value = DBNull.Value;

No comments:

Post a Comment