deanlee17
asked on
How to assign null to a sqlparameter?
Hi guys, my code is....
I tried
But got the error 'Operator '??' cannot be applied to operands of type 'string' and 'System.
Thanks,
Dean
try
{
SqlConnection sqlConn = new SqlConnection("XXX");
SqlCommand sqlComm = new SqlCommand();
sqlComm = sqlConn.CreateCommand();
sqlComm.CommandText = @"INSERT INTO BomDataUpload (RFQ_Number, ManPartNo, Manufacturer, CustPartNo ) VALUES (@RFQ_Number, @ManPartNo, @Manufacturer, @CustPartNo )";
sqlComm.Parameters.Add("@ManPartNo", SqlDbType.VarChar);
sqlComm.Parameters.Add("@Manufacturer", SqlDbType.VarChar);
sqlComm.Parameters.Add("@RFQ_Number", SqlDbType.VarChar);
sqlComm.Parameters.Add("@CustPartNo", SqlDbType.VarChar);
sqlConn.Open();
for (int i = 0; i < 2; i++)
{
sqlComm.Parameters["@Manufacturer"].Value = gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.Manufacturer);
sqlComm.Parameters["@ManPartNo"].Value = gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.ManPartNo);
sqlComm.Parameters["@RFQ_Number"].Value = gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.RFQ_Number);
sqlComm.Parameters["@CustPartNo"].Value = gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.CustPartNo);
sqlComm.ExecuteNonQuery();
}
sqlConn.Close();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString());
}
I tried
sqlComm.Parameters["@CustPartNo"].Value = gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.CustPartNo) ?? DBNull.Value;
But got the error 'Operator '??' cannot be applied to operands of type 'string' and 'System.
Thanks,
Dean
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@ Sedgwick....
Invalid expression term 'object'
Invalid expression term 'object'
ASKER
@ VipulKadia
The name 'NULL' does not exist in the current context
The name 'NULL' does not exist in the current context
try null(lowercase) instead of NULL.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The things you coalesce must be of the same type. You need to cast as sedgwick demonstrates--although, I believe the DBNull.Value will need to be cast as object as well.
Open in new window