Linked Questions
23 questions linked to/from Assign null to a SqlParameter
2 votes
2 answers
2k views
Raw SQL insert statement with optional parameter [duplicate]
I'm trying to execute this command but get an exception: cmd.CommandText = @"insert into Foo (Column1, Column2) values (@Parameter1, @Parameter2)"; cmd.Parameters.Add(new ...
1 vote
2 answers
676 views
SqlParameter : Insert a row with a null float value [duplicate]
I have a list of float? objects. List<float?> myList = new List<float?>{1,null,3}; I'm trying to insert this objects in a table. using (SqlCommand oCommand = myConnection.CreateCommand())...
0 votes
1 answer
364 views
SqlCommand parameters, shorter way of adding nullable value? [duplicate]
Writing data layer code against T-SQL, I often have to add a SqlParameter to a SqlCommand. In the case of a nullable type (for example a DateTime?), I write it like this: if (t.StartDate.HasValue) ...
0 votes
4 answers
114 views
Syntax for Stored Procedure Parameter [duplicate]
I have a value which I am trying to add as a parameter to a stored procedure. The value of this parameter could either be a STRING of "1" or "NULL" If it's NULL then I want to add the parameter of ...
0 votes
0 answers
46 views
Assigning DBNull to an integer type throwing an error when using with the ternary operator [duplicate]
I have a this piece of code below where I'm checking if a value in a certain field is blank ("") and if it is I want to set the value of the SqlParameter to NULL otherwise to the value from the field. ...
0 votes
0 answers
30 views
Not able to update database datetime null column value with datepicker null value in c# [duplicate]
I am updating DataTime? column value to null in the database using the Stored procedure and able to update null value in SQL using the same stored procedure. I am using the below code in C#: if(string....
81 votes
14 answers
140k views
How can I force entity framework to insert identity columns?
I want to write some C# code to initialize my database with some seed data. Clearly, this is going to require the ability to be able to set the values of various Identity columns when inserting. I'm ...
110 votes
4 answers
88k views
Exception when AddWithValue parameter is NULL
I have following code for specifying parameters for SQL query. I am getting following exception when I use Code 1; but works fine when I use Code 2. In Code 2 we have a check for null and hence a if.....
43 votes
3 answers
14k views
Why is this code invalid in C#?
The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit ...
18 votes
1 answer
16k views
Call Stored Procedure with a null parameter value with EntityFramework
i've a stored procedure on sqlserver 2008 and one of the parameters accept null values. i don't know how to call that SP with a null value on the parameter. for a little more context i'm using ...
5 votes
2 answers
18k views
How do I pass a null value into a parameter for a SqlCommand
Before anyone comments that this has been answered before in other question I KNOW that..but in spite of the answers I have reviewed at Assign null to a SqlParameter Sending null parameters to Sql ...
4 votes
1 answer
2k views
Passing Empty Sql Parameter Give Error [duplicate]
I am passing array of SQL Parameters to my wrapper function like SqlParameter[] sqlParams = new SqlParameter[] { new SqlParameter("@UserName",userName), new SqlParameter("@...
3 votes
2 answers
1k views
SqlParameter "disappears" when assigned a value of null?
I stumbled across the following issue recently: Assigning a null to the Value property of an SqlParameter appears to cause the parameter to be omitted from the query rather than passing NULL. This ...
1 vote
3 answers
2k views
Must declare the scalar variable "@employeeid" in stored procedure insert in ASP.NET MVC 5
I want to insert data into a database using a stored procedure, Entity Framework in ASP.NET MVC 5. It works in SQL Server, but when I execute the procedure but in Visual Studio while inserting, I get ...
1 vote
2 answers
1k views
How to potentially pass null in as a parameter in ADO.Net
I'm trying to pass a parameter into a stored proc using ADO.NET. This paramater is of type int but the field in the database is nullable. Here's my code: AddInParameter(cmd, "@...