Solved

How to assign null to a sqlparameter?

Posted on 2013-06-04
7
378 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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