dbnull

Handling a DateTime DBNull

…衆ロ難τιáo~ 提交于 2019-12-01 16:23:32
I have seen many, many versions of this on SO, but none of them seem to quite work for my needs. My data comes from a vendor database that allows null for DateTime fields. First I pull my data into a DataTable. using (SqlCommand cmd = new SqlCommand(sb.ToString(), conn)) using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); } I am converting a DataTable to a List<> for processing. var equipment = from i in dt.AsEnumerable() select new Equipment() { Id = i.Field<string>("ID"), BeginDate = i.Field<DateTime>("BeginDate"), EndDate = i.Field<DateTime>("EndDate"), EstimatedLife = i

Handling a DateTime DBNull

做~自己de王妃 提交于 2019-12-01 15:57:48
问题 I have seen many, many versions of this on SO, but none of them seem to quite work for my needs. My data comes from a vendor database that allows null for DateTime fields. First I pull my data into a DataTable. using (SqlCommand cmd = new SqlCommand(sb.ToString(), conn)) using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); } I am converting a DataTable to a List<> for processing. var equipment = from i in dt.AsEnumerable() select new Equipment() { Id = i.Field<string>("ID"),

Operand type clash: nvarchar is incompatible with image

岁酱吖の 提交于 2019-12-01 14:36:31
问题 I'm using a SQL Server 2008 stored procedure to create a new record with this syntax: cmd.Parameters.Add("@photo", DBNull.Value) cmd.ExecuteNonQuery() but the result is a: Operand type clash: nvarchar is incompatible with image Photo is not the only parameter but is the only image one, I am not passing a nvarchar but a null value, am I missing something? 回答1: If you pass in DBNull.Value as the value, ADO.NET can't figure out what type the parameter should be. If you specify a string, or an

Why isn't my DbNull a singleton when I deserialise it using XmlSerialiser?

烈酒焚心 提交于 2019-12-01 04:58:34
I've always assumed that DbNull.value was a singleton. And thus you could do things like this: VB.NET: If someObject Is DbNull.Value Then ... End if C#: If (someObject == DbNull.Value) { ... } But recently, I serialised a DbNull instance using the XmlSerialiser and suddenly it wasn't a singleton any more. Type comparison operations (like C#'s (obj is DBNull)) work OK though. Code follows: [Serializable, System.Xml.Serialization.XmlInclude(typeof(DBNull))] public class SerialiseMe { public SerialiseMe() { } public SerialiseMe(object value) { this.ICanBeDbNull = value; } public Object

Passing DBNull.Value and Empty textbox value to database [duplicate]

☆樱花仙子☆ 提交于 2019-12-01 03:29:53
This question already has an answer here: How do I Parameterize a null string with DBNull.Value clearly and quickly 8 answers I have some textboxes on my page which can be empty because they are optional and I have this DAL code parameters.Add(new SqlParameter("@FirstName", FirstName)); parameters.Add(new SqlParameter("@LastName", LastName)); parameters.Add(new SqlParameter("@DisplayName", DisplayName)); parameters.Add(new SqlParameter("@BirthDate", BirthDate)); parameters.Add(new SqlParameter("@Gender", Gender)); Any of those fields can be empty. The problem is when they are empty I receive

Why isn't my DbNull a singleton when I deserialise it using XmlSerialiser?

霸气de小男生 提交于 2019-12-01 02:36:04
问题 I've always assumed that DbNull.value was a singleton. And thus you could do things like this: VB.NET: If someObject Is DbNull.Value Then ... End if C#: If (someObject == DbNull.Value) { ... } But recently, I serialised a DbNull instance using the XmlSerialiser and suddenly it wasn't a singleton any more. Type comparison operations (like C#'s (obj is DBNull)) work OK though. Code follows: [Serializable, System.Xml.Serialization.XmlInclude(typeof(DBNull))] public class SerialiseMe { public

Passing DBNull.Value and Empty textbox value to database [duplicate]

南楼画角 提交于 2019-11-30 23:13:38
问题 This question already has answers here : How do I Parameterize a null string with DBNull.Value clearly and quickly (8 answers) Closed 5 years ago . I have some textboxes on my page which can be empty because they are optional and I have this DAL code parameters.Add(new SqlParameter("@FirstName", FirstName)); parameters.Add(new SqlParameter("@LastName", LastName)); parameters.Add(new SqlParameter("@DisplayName", DisplayName)); parameters.Add(new SqlParameter("@BirthDate", BirthDate));

Cannot implicitly convert type 'decimal?' to 'decimal'.

∥☆過路亽.° 提交于 2019-11-30 18:44:30
sdr is my sqldatareader and I want to check that the curPrice value which is of type decimal is null. inrec.curPrice = sdr.IsDBNull(7) ? (decimal?)null : sdr.GetDecimal(7); This is the error message I am getting: Cannot implicitly convert type 'decimal?' to 'decimal'. An explicit conversion exists (are you missing a cast?) Where am I going wrong, please someone tell me. either convert curPrice to nullable, or use .Value property of nullable types. If curPrice is a property of a class then public decimal? curPrice { get; set; } decimal? indicates that it's a nullable decimal; you have to use

Finding null value in Dataset - DataRow.IsNull method vs ==DbNull.Value - c#

与世无争的帅哥 提交于 2019-11-30 18:02:27
What are the benefits of using the c# method DataRow.IsNull to determine a null value over checking if the row equals DbNull.value? if(ds.Tables[0].Rows[0].IsNull("ROWNAME")) {do stuff} vs if(ds.Tables[0].Rows[0]["ROWNAME"] == DbNull.value) {do stuff} There is no real practical benefit. Use whichever one seems more readable to you. As to the particular differences between them, the basic answer is that IsNull queries the null state for a particular record within a column. Using == DBNull.Value actually retrieves the value and does substitution in the case that it's actually null. In other

Cannot implicitly convert type 'decimal?' to 'decimal'.

会有一股神秘感。 提交于 2019-11-30 16:53:43
问题 sdr is my sqldatareader and I want to check that the curPrice value which is of type decimal is null. inrec.curPrice = sdr.IsDBNull(7) ? (decimal?)null : sdr.GetDecimal(7); This is the error message I am getting: Cannot implicitly convert type 'decimal?' to 'decimal'. An explicit conversion exists (are you missing a cast?) Where am I going wrong, please someone tell me. 回答1: either convert curPrice to nullable, or use .Value property of nullable types. If curPrice is a property of a class