dbnull

Checking for DBNull throws a StrongTypingException

南楼画角 提交于 2019-12-10 02:21:11
问题 I am using a dataset to pull data from a DB. One of the fields in a row is NULL . I know this. However, the following vb.net code throws a StrongTypingException (in the autogenerated get_SomeField() method in the dataset designer): If Not IsDBNull(aRow.SomeField) Then 'do something End If According to documentation and this question it should be fine. edit: If aRow.SomeField is DBNull.Value Then also returns the same error. Argh. 回答1: The difference is that in the related question it is

Null safe way to get values from an IDataReader

旧街凉风 提交于 2019-12-10 01:25:31
问题 (LocalVariable)ABC.string(Name)= (Idatareader)datareader.GetString(0); this name value is coming from database.. what happening here is if this name value is null while reading it's throwing an exception? I am manually doing some if condition here. I don't want to write a manual condition to check all my variables.. I am doing something like this now.. string abc = (Idatareader)datareader.GetValue(0); if(abc = null) //assiging null else assiging abc value is there something like can we write

How to safely cast nullable result from sqlreader to int?

不想你离开。 提交于 2019-12-08 19:42:14
问题 I have a table which contains null values and I need to get data from the table using SqlDataReader. I can't figure out how I can safely cast DBNull to int. I'm doing it in this way at the moment: ... reader = command.ExecuteReader(); while (reader.Read()) { int y = (reader["PublicationYear"] != null) ? Convert.ToInt32(reader["PublicationYear"]) : 0; ... } ... but getting a Object cannot be cast from DBNull to other types. when PublicationYear is null. How can I get the value safely? Thanks.

Cast a null into something?

六月ゝ 毕业季﹏ 提交于 2019-12-07 06:09:00
问题 I had this interesting discussion today with a colleague. We were debating two pieces of code in C#. Code Snippet 1: if(!reader.IsDBNull(2)) { long? variable1 = reader.GetInt64(2) } Code Snippet 2: long variable1 = reader.IsDBNull(2) ? (long?) null : reader.GetInt64(2) Question is: is it a good practice to cast null into a nullable long? Or would you rather use the traditional if statement to avoid casting null to nullable long. 回答1: The expressions (type?)null , default(type?) and new

Cast a null into something?

江枫思渺然 提交于 2019-12-05 08:35:21
I had this interesting discussion today with a colleague. We were debating two pieces of code in C#. Code Snippet 1: if(!reader.IsDBNull(2)) { long? variable1 = reader.GetInt64(2) } Code Snippet 2: long variable1 = reader.IsDBNull(2) ? (long?) null : reader.GetInt64(2) Question is: is it a good practice to cast null into a nullable long? Or would you rather use the traditional if statement to avoid casting null to nullable long. The expressions (type?)null , default(type?) and new Nullable<type>() end up being compiled into the same opcodes: long? x = (long?)null; long? y = default(long?);

How can DBNull not equal DBNull

橙三吉。 提交于 2019-12-05 05:44:30
I have the following line of code if (DBNull.Value.Equals(o) || o != null) where o is object o in row.ItemArray I keep getting an error of --> Xml type "List of xdt:untypedAtomic" does not support a conversion from Clr type "DBNull" to Clr type "String". What I don't understand is that when I step through my code this if should be catching this and performing my alternate action but it does not? Can someone please shed some light for me. Thank you! Try using Convert.IsDBNull method. I think you problem is that in fact DBNull.Value == null //is always false The DBNull is a special class for

Checking for DBNull throws a StrongTypingException

隐身守侯 提交于 2019-12-05 03:36:21
I am using a dataset to pull data from a DB. One of the fields in a row is NULL . I know this. However, the following vb.net code throws a StrongTypingException (in the autogenerated get_SomeField() method in the dataset designer): If Not IsDBNull(aRow.SomeField) Then 'do something End If According to documentation and this question it should be fine. edit: If aRow.SomeField is DBNull.Value Then also returns the same error. Argh. The difference is that in the related question it is talking about an untyped value (i.e. object ) via an indexer. When you go via .SomeField , the type is already

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

笑着哭i 提交于 2019-12-05 01:37:49
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. How can I manage empty parses and allow the DateTime, if empty or null, be accepted as DBNull.Value ?

Null safe way to get values from an IDataReader

你。 提交于 2019-12-05 01:32:17
(LocalVariable)ABC.string(Name)= (Idatareader)datareader.GetString(0); this name value is coming from database.. what happening here is if this name value is null while reading it's throwing an exception? I am manually doing some if condition here. I don't want to write a manual condition to check all my variables.. I am doing something like this now.. string abc = (Idatareader)datareader.GetValue(0); if(abc = null) //assiging null else assiging abc value is there something like can we write extension method for this? thanks Here is a couple extension methods that will nicely wrap up all of

Operator '??' cannot be applied to operands of type 'string' and 'System.DBNull'

妖精的绣舞 提交于 2019-12-04 22:31:46
I have the following C# code: sqlCommand.Parameters.AddWithValue("@Parameter", table.Value ?? DBNull.Value); But it throws the following compilation error: Operator ?? cannot be applied to operands of type string and System.DBNull Why doesn't the compiler allow this syntax? Pavel Urbančík Both operands need to be object. Use explicit cast: (object)table.Value ?? DBNull.Value; There is no automatic conversion between string and System.DBNull and so you need to specify the type you want explicitly by adding a cast to object : sqlCommandObject.Parameters.AddWithValue("@Parameter", table.Value ??