Link to home
Start Free TrialLog in
Avatar of deanlee17
deanlee17

asked on

How to assign null to a sqlparameter?

Hi guys, my code is....

  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());
            }

Open in new window


I tried

sqlComm.Parameters["@CustPartNo"].Value = gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.CustPartNo) ?? DBNull.Value; 

Open in new window


But got the error 'Operator '??' cannot be applied to operands of type 'string' and 'System.

Thanks,
Dean
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

sqlComm.Parameters["@CustPartNo"].Value = CType(gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.CustPartNo), object) ?? DBNull.Value; 

Open in new window

SOLUTION
Avatar of VipulKadia
VipulKadia
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deanlee17
deanlee17

ASKER

@ Sedgwick....

Invalid expression term 'object'
@ VipulKadia

The name 'NULL' does not exist in the current context
try null(lowercase) instead of NULL.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.