dbnull

is DBNull vs. DBNull.Value.Equals()

主宰稳场 提交于 2021-02-08 13:18:17
问题 I am curious what are the pros and cons of using if(some_value is DBNull) versus if(DBNull.Value.Equals(some_value)) . Personally i prefer if(some_value is DBNull) because i find it more readable. I know Microsoft recommends using if(DBNull.Value.Equals(some_value)) according to https://msdn.microsoft.com/en-us/library/system.dbnull%28v=vs.110%29.aspx. 回答1: I would go for the DBNull.Value.Equals way. Why? Beacuse is will check the type against equality. It has to look up the left hand type

How to determine if a parameter value was passed to a stored procedure

别说谁变了你拦得住时间么 提交于 2021-01-27 05:24:35
问题 I want to create a stored procedure (in SQL Server 2008 R2) that will update a record in a table based on the table's PK. The stored proc will have, for example, four parameters: @ID int, @Name nvarchar(50), @Email nvarchar(80), @Phone nvarchar(20) How can I determine if the caller of the stored proc passes a NULL value for one (or more) of the parameters vs. if the caller didn't pass anything for one (or more) of the parameters? C# caller example: Caller specifies NULL for @Phone : using

How to determine if a parameter value was passed to a stored procedure

二次信任 提交于 2021-01-27 05:24:29
问题 I want to create a stored procedure (in SQL Server 2008 R2) that will update a record in a table based on the table's PK. The stored proc will have, for example, four parameters: @ID int, @Name nvarchar(50), @Email nvarchar(80), @Phone nvarchar(20) How can I determine if the caller of the stored proc passes a NULL value for one (or more) of the parameters vs. if the caller didn't pass anything for one (or more) of the parameters? C# caller example: Caller specifies NULL for @Phone : using

How to manage parsing an null object for DateTime to be used with ADO.NET as DBNULL

不羁岁月 提交于 2020-02-01 16:40:46
问题 I have two DateTime objects, BirthDate and HireDate. They are correctly formatted as a string and when I pass them through to my data access layer, they need to be parsed into a DateTime object. DateTime hD = DateTime.Parse(hire); DateTime bD = DateTime.Parse(birth); //incase of a datestring being passed through dateStringPassed = "7/2/1969"; But sometimes, the strings hire and birth are null or empty "" , if the code is run like this, I get a FormatException error from Parsing a empty string

System.IndexOutOfRangeException on SQLDataReader Value Using C#

烂漫一生 提交于 2020-01-24 10:03:35
问题 I have a SQLDataReader that returns three integers. However, there are occasions when two of the integers will return null values. To get round this I wrote the following: int shoppingCartHeadID = 0; int billID = 0; int delID = 0; conn.Open(); reader = comm.ExecuteReader(); if (reader.Read()) { shoppingCartHeadID = Convert.ToInt32(reader["shoppingCartHeadID"]); if (!reader.IsDBNull(billID)) { billID = Convert.ToInt32(reader["billID"]); } if (!reader.IsDBNull(delID)) { delID = Convert.ToInt32

Object cannot be cast from DBNull to other types in constructor

回眸只為那壹抹淺笑 提交于 2020-01-15 19:14:06
问题 I need to add into the list one object CompanyDetails, so I get data from my database, and load it into constructor. result.Add(new CompanyDetails() { Name = dr["name"].ToString(), City = dr["city"].ToString(), StreetName = dr["streetName"].ToString(), StreetNr = Convert.ToInt32(dr["apartmentNr"]), Tax = int.Parse(dr["TAX"].ToString() )}); StreetNr and Tax can have null value. And when I'm trying to run it i get the error: Object cannot be cast from DBNull to other types How can I fix it? I

DBNull cast to (object) returns different value [closed]

…衆ロ難τιáo~ 提交于 2020-01-07 09:54:33
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am trying to write DBNull.Value using Parameters.AddWithVallue when an optional parameter (a string ) of a C# method is null . public static void Abc(string distrito, string municipio = null) command.Parameters.AddWithValue("@Municipio", municipio ?? (object)DBNull.Value); command.Parameters.AddWithValue("

DBNull cast to (object) returns different value [closed]

二次信任 提交于 2020-01-07 09:52:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am trying to write DBNull.Value using Parameters.AddWithVallue when an optional parameter (a string ) of a C# method is null . public static void Abc(string distrito, string municipio = null) command.Parameters.AddWithValue("@Municipio", municipio ?? (object)DBNull.Value); command.Parameters.AddWithValue("

VB.NET: How do I use coalesce with db column values and nullable types? Or is there a better solution?

时光怂恿深爱的人放手 提交于 2020-01-04 06:00:38
问题 I'm trying to do something similar to what's described here, but with nullable types. http://www.csharp-station.com/Tutorials/Lesson23.aspx int availableUnits = unitsInStock ?? 0; In VB, it would be this: Dim availableUnits As Int32 = If(unitsInStock, 0) However I'm working with db columns, which could be DbNull, and nullable types, which can be Nothing (which is different to DbNull). If a column is DbNull, I want to return Nothing, otherwise return the value. For eg: Dim availableUnits As

Seeding method is inserting additional Entities with NULL values

房东的猫 提交于 2019-12-31 05:14:54
问题 I am having this strange behavior all of a sudden (i have compared my files in version control (tfs) to be sure i did not change anything and i didn't found anything different). I am seeding my database with some metadata and i see that it has a very strange behavior i never saw before. I am inserting a Entity "Product" and it inserts this entity 2 times , first insert is correct and has everything it should have, the other one has NULL properties (string values) but some (like datetimes)