C# int? parameter- Data is Null. This method or property cannot be called on Null values The Next CEO of Stack OverflowHow do you give a C# Auto-Property a default value?In C#, what happens when you call an extension method on a null object?Get int value from enum in C#Get property value from string using reflection in C#Pass Method as Parameter using C#How to call asynchronous method from synchronous method in C#?Data is Null. This method or property cannot be called on null value after joining two tables in C#.netSqlDataReader - This method or property cannot be called on Null valuesData is Null. This method or property cannot be called on Null value (mysql to textbox)Data is Null. This method or property cannot be called on Null values error in c#

Read/write a pipe-delimited file line by line with some simple text manipulation

Is a linearly independent set whose span is dense a Schauder basis?

"Eavesdropping" vs "Listen in on"

Why did the Drakh emissary look so blurred in S04:E11 "Lines of Communication"?

Raspberry pi 3 B with Ubuntu 18.04 server arm64: what pi version

Prodigo = pro + ago?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

Creating a script with console commands

Is it OK to decorate a log book cover?

Does int main() need a declaration on C++?

How can I separate the number from the unit in argument?

Can you teleport closer to a creature you are Frightened of?

Is it reasonable to ask other researchers to send me their previous grant applications?

How to unfasten electrical subpanel attached with ramset

Finitely generated matrix groups whose eigenvalues are all algebraic

Create custom note boxes

Mathematica command that allows it to read my intentions

Man transported from Alternate World into ours by a Neutrino Detector

Upgrading From a 9 Speed Sora Derailleur?

Variance of Monte Carlo integration with importance sampling

Why do we say “un seul M” and not “une seule M” even though M is a “consonne”?

MT "will strike" & LXX "will watch carefully" (Gen 3:15)?

What happens if you break a law in another country outside of that country?

Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico



C# int? parameter- Data is Null. This method or property cannot be called on Null values



The Next CEO of Stack OverflowHow do you give a C# Auto-Property a default value?In C#, what happens when you call an extension method on a null object?Get int value from enum in C#Get property value from string using reflection in C#Pass Method as Parameter using C#How to call asynchronous method from synchronous method in C#?Data is Null. This method or property cannot be called on null value after joining two tables in C#.netSqlDataReader - This method or property cannot be called on Null valuesData is Null. This method or property cannot be called on Null value (mysql to textbox)Data is Null. This method or property cannot be called on Null values error in c#










0















I am trying to call a stored procedure which has an INT parameter (BusinessID) and allows NULL. From my C# code, I can't figure out if data is null, how to identify and pass. I have listed three techniques I have tried numbered #1,#2,#3 and the errors I get. Please guide
Here is my sample code:



private static DataRow ValidateRecord (string ProjectNum, int? BusinessID)

DataRow returnRow = null;
using (SqlConnection connection = Connection.GetConnection())

if (connection.State != ConnectionState.Open)

connection.Open();


try

using (SqlCommand command = new SqlCommand(
"[dbo].[mySQLProc]", connection))

command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 90;

command.Parameters.Add("@ProjectNum", SqlDbType.VarChar, 17).Value = ProjectNum;

// #1 – error: "Data is Null. This method or property cannot be called on Null values."
//if (!BusinessID.HasValue)
//
// BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null;
//

//#2 - "Data is Null. This method or property cannot be called on Null values."
if (BusinessID == null)

BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null;

//#3 - error on compile time – Type of conditional expression cannot be determined because
// there is no implicit converstion between ‘System.DBNull’ and ‘int?’
// BusinessID = BusinessID == null ? DBNull.Value : BusinessID;
command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID;
command.ExecuteNonQuery();
connection.Close();
returnRow = CreateDataRow(ProjectNum, BusinessID);


catch (Exception ex)

throw ex;

return returnRow;











share|improve this question






















  • The line BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null; - try (int?) also whether BusinessID is null or not you're still passing it to the stored procedure I'm not sure why you're checking for null if the SP can handle it. Not sure what version of .net you're using but maybe change command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID; to command.Parameters.AddWithValue("@BusinessID", BusinessID); - Hope this helps

    – Andrew
    Mar 8 at 19:41












  • It might be to do with you including the '@' symbol in the name - whenever I create SqlParameters, I drop the '@' e.g. new SqlParameter("BusinessID", BusinessID)

    – Andrew Williamson
    Mar 8 at 19:41











  • same error with (int?)System.Data.SqlTypes.SqlInt32.Null;

    – SilverFish
    Mar 8 at 19:51











  • if I do command.Parameters.AddWithValue("@BusinessID", BusinessID); and pass null, then the stored procedure doesn't even recognize it as if @BusinessID is missing

    – SilverFish
    Mar 8 at 19:59











  • Worked!!! command.Parameters.AddWithValue("@BusinessID", (Object)BusinessID ?? DBNull.Value);

    – SilverFish
    Mar 8 at 20:32















0















I am trying to call a stored procedure which has an INT parameter (BusinessID) and allows NULL. From my C# code, I can't figure out if data is null, how to identify and pass. I have listed three techniques I have tried numbered #1,#2,#3 and the errors I get. Please guide
Here is my sample code:



private static DataRow ValidateRecord (string ProjectNum, int? BusinessID)

DataRow returnRow = null;
using (SqlConnection connection = Connection.GetConnection())

if (connection.State != ConnectionState.Open)

connection.Open();


try

using (SqlCommand command = new SqlCommand(
"[dbo].[mySQLProc]", connection))

command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 90;

command.Parameters.Add("@ProjectNum", SqlDbType.VarChar, 17).Value = ProjectNum;

// #1 – error: "Data is Null. This method or property cannot be called on Null values."
//if (!BusinessID.HasValue)
//
// BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null;
//

//#2 - "Data is Null. This method or property cannot be called on Null values."
if (BusinessID == null)

BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null;

//#3 - error on compile time – Type of conditional expression cannot be determined because
// there is no implicit converstion between ‘System.DBNull’ and ‘int?’
// BusinessID = BusinessID == null ? DBNull.Value : BusinessID;
command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID;
command.ExecuteNonQuery();
connection.Close();
returnRow = CreateDataRow(ProjectNum, BusinessID);


catch (Exception ex)

throw ex;

return returnRow;











share|improve this question






















  • The line BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null; - try (int?) also whether BusinessID is null or not you're still passing it to the stored procedure I'm not sure why you're checking for null if the SP can handle it. Not sure what version of .net you're using but maybe change command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID; to command.Parameters.AddWithValue("@BusinessID", BusinessID); - Hope this helps

    – Andrew
    Mar 8 at 19:41












  • It might be to do with you including the '@' symbol in the name - whenever I create SqlParameters, I drop the '@' e.g. new SqlParameter("BusinessID", BusinessID)

    – Andrew Williamson
    Mar 8 at 19:41











  • same error with (int?)System.Data.SqlTypes.SqlInt32.Null;

    – SilverFish
    Mar 8 at 19:51











  • if I do command.Parameters.AddWithValue("@BusinessID", BusinessID); and pass null, then the stored procedure doesn't even recognize it as if @BusinessID is missing

    – SilverFish
    Mar 8 at 19:59











  • Worked!!! command.Parameters.AddWithValue("@BusinessID", (Object)BusinessID ?? DBNull.Value);

    – SilverFish
    Mar 8 at 20:32













0












0








0








I am trying to call a stored procedure which has an INT parameter (BusinessID) and allows NULL. From my C# code, I can't figure out if data is null, how to identify and pass. I have listed three techniques I have tried numbered #1,#2,#3 and the errors I get. Please guide
Here is my sample code:



private static DataRow ValidateRecord (string ProjectNum, int? BusinessID)

DataRow returnRow = null;
using (SqlConnection connection = Connection.GetConnection())

if (connection.State != ConnectionState.Open)

connection.Open();


try

using (SqlCommand command = new SqlCommand(
"[dbo].[mySQLProc]", connection))

command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 90;

command.Parameters.Add("@ProjectNum", SqlDbType.VarChar, 17).Value = ProjectNum;

// #1 – error: "Data is Null. This method or property cannot be called on Null values."
//if (!BusinessID.HasValue)
//
// BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null;
//

//#2 - "Data is Null. This method or property cannot be called on Null values."
if (BusinessID == null)

BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null;

//#3 - error on compile time – Type of conditional expression cannot be determined because
// there is no implicit converstion between ‘System.DBNull’ and ‘int?’
// BusinessID = BusinessID == null ? DBNull.Value : BusinessID;
command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID;
command.ExecuteNonQuery();
connection.Close();
returnRow = CreateDataRow(ProjectNum, BusinessID);


catch (Exception ex)

throw ex;

return returnRow;











share|improve this question














I am trying to call a stored procedure which has an INT parameter (BusinessID) and allows NULL. From my C# code, I can't figure out if data is null, how to identify and pass. I have listed three techniques I have tried numbered #1,#2,#3 and the errors I get. Please guide
Here is my sample code:



private static DataRow ValidateRecord (string ProjectNum, int? BusinessID)

DataRow returnRow = null;
using (SqlConnection connection = Connection.GetConnection())

if (connection.State != ConnectionState.Open)

connection.Open();


try

using (SqlCommand command = new SqlCommand(
"[dbo].[mySQLProc]", connection))

command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 90;

command.Parameters.Add("@ProjectNum", SqlDbType.VarChar, 17).Value = ProjectNum;

// #1 – error: "Data is Null. This method or property cannot be called on Null values."
//if (!BusinessID.HasValue)
//
// BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null;
//

//#2 - "Data is Null. This method or property cannot be called on Null values."
if (BusinessID == null)

BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null;

//#3 - error on compile time – Type of conditional expression cannot be determined because
// there is no implicit converstion between ‘System.DBNull’ and ‘int?’
// BusinessID = BusinessID == null ? DBNull.Value : BusinessID;
command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID;
command.ExecuteNonQuery();
connection.Close();
returnRow = CreateDataRow(ProjectNum, BusinessID);


catch (Exception ex)

throw ex;

return returnRow;








c# stored-procedures null int






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 19:11









SilverFishSilverFish

184216




184216












  • The line BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null; - try (int?) also whether BusinessID is null or not you're still passing it to the stored procedure I'm not sure why you're checking for null if the SP can handle it. Not sure what version of .net you're using but maybe change command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID; to command.Parameters.AddWithValue("@BusinessID", BusinessID); - Hope this helps

    – Andrew
    Mar 8 at 19:41












  • It might be to do with you including the '@' symbol in the name - whenever I create SqlParameters, I drop the '@' e.g. new SqlParameter("BusinessID", BusinessID)

    – Andrew Williamson
    Mar 8 at 19:41











  • same error with (int?)System.Data.SqlTypes.SqlInt32.Null;

    – SilverFish
    Mar 8 at 19:51











  • if I do command.Parameters.AddWithValue("@BusinessID", BusinessID); and pass null, then the stored procedure doesn't even recognize it as if @BusinessID is missing

    – SilverFish
    Mar 8 at 19:59











  • Worked!!! command.Parameters.AddWithValue("@BusinessID", (Object)BusinessID ?? DBNull.Value);

    – SilverFish
    Mar 8 at 20:32

















  • The line BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null; - try (int?) also whether BusinessID is null or not you're still passing it to the stored procedure I'm not sure why you're checking for null if the SP can handle it. Not sure what version of .net you're using but maybe change command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID; to command.Parameters.AddWithValue("@BusinessID", BusinessID); - Hope this helps

    – Andrew
    Mar 8 at 19:41












  • It might be to do with you including the '@' symbol in the name - whenever I create SqlParameters, I drop the '@' e.g. new SqlParameter("BusinessID", BusinessID)

    – Andrew Williamson
    Mar 8 at 19:41











  • same error with (int?)System.Data.SqlTypes.SqlInt32.Null;

    – SilverFish
    Mar 8 at 19:51











  • if I do command.Parameters.AddWithValue("@BusinessID", BusinessID); and pass null, then the stored procedure doesn't even recognize it as if @BusinessID is missing

    – SilverFish
    Mar 8 at 19:59











  • Worked!!! command.Parameters.AddWithValue("@BusinessID", (Object)BusinessID ?? DBNull.Value);

    – SilverFish
    Mar 8 at 20:32
















The line BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null; - try (int?) also whether BusinessID is null or not you're still passing it to the stored procedure I'm not sure why you're checking for null if the SP can handle it. Not sure what version of .net you're using but maybe change command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID; to command.Parameters.AddWithValue("@BusinessID", BusinessID); - Hope this helps

– Andrew
Mar 8 at 19:41






The line BusinessID = (int)System.Data.SqlTypes.SqlInt32.Null; - try (int?) also whether BusinessID is null or not you're still passing it to the stored procedure I'm not sure why you're checking for null if the SP can handle it. Not sure what version of .net you're using but maybe change command.Parameters.Add("@BusinessID", SqlDbType.Int).Value = BusinessID; to command.Parameters.AddWithValue("@BusinessID", BusinessID); - Hope this helps

– Andrew
Mar 8 at 19:41














It might be to do with you including the '@' symbol in the name - whenever I create SqlParameters, I drop the '@' e.g. new SqlParameter("BusinessID", BusinessID)

– Andrew Williamson
Mar 8 at 19:41





It might be to do with you including the '@' symbol in the name - whenever I create SqlParameters, I drop the '@' e.g. new SqlParameter("BusinessID", BusinessID)

– Andrew Williamson
Mar 8 at 19:41













same error with (int?)System.Data.SqlTypes.SqlInt32.Null;

– SilverFish
Mar 8 at 19:51





same error with (int?)System.Data.SqlTypes.SqlInt32.Null;

– SilverFish
Mar 8 at 19:51













if I do command.Parameters.AddWithValue("@BusinessID", BusinessID); and pass null, then the stored procedure doesn't even recognize it as if @BusinessID is missing

– SilverFish
Mar 8 at 19:59





if I do command.Parameters.AddWithValue("@BusinessID", BusinessID); and pass null, then the stored procedure doesn't even recognize it as if @BusinessID is missing

– SilverFish
Mar 8 at 19:59













Worked!!! command.Parameters.AddWithValue("@BusinessID", (Object)BusinessID ?? DBNull.Value);

– SilverFish
Mar 8 at 20:32





Worked!!! command.Parameters.AddWithValue("@BusinessID", (Object)BusinessID ?? DBNull.Value);

– SilverFish
Mar 8 at 20:32












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069553%2fc-sharp-int-parameter-data-is-null-this-method-or-property-cannot-be-called-o%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069553%2fc-sharp-int-parameter-data-is-null-this-method-or-property-cannot-be-called-o%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229