Solved

How to assign null to a sqlparameter?

Posted on 2013-06-04
7
374 Views
Last Modified: 2013-06-05
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
0
Comment
Question by:deanlee17
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 42

Expert Comment

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

Open in new window

0
 
LVL 8

Assisted Solution

by:VipulKadia
VipulKadia earned 250 total points
ID: 39218550
Try this :
sqlComm.Parameters["@CustPartNo"].Value = gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.CustPartNo) == NULL? DBNull.Value :  gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.CustPartNo);
0
 

Author Comment

by:deanlee17
ID: 39218556
@ Sedgwick....

Invalid expression term 'object'
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:deanlee17
ID: 39218564
@ VipulKadia

The name 'NULL' does not exist in the current context
0
 
LVL 8

Expert Comment

by:VipulKadia
ID: 39218601
try null(lowercase) instead of NULL.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 39218634
i thought its vb.net, here in c#:
sqlComm.Parameters["@CustPartNo"].Value = (object)gridControl1.GetCellDisplayText(i, Instance_DropdownColumnNumberMatch.CustPartNo) ?? DBNull.Value; 

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39218974
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now