Solved

How to assign null to a sqlparameter?

Posted on 2013-06-04
7
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pdf not loading 2 41
How does this modal work? 3 34
Which is best Image Resizing Web service 11 42
learning MS SSIS 13 27
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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