Solved

How to assign null to a sqlparameter?

Posted on 2013-06-04
7
376 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 75

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 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

17 Experts available now in Live!

Get 1:1 Help Now