dbnull

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

主宰稳场 提交于 2019-11-30 01:06:08
问题 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} 回答1: 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

Linq to DataTable - Cannot cast DBNull

我的未来我决定 提交于 2019-11-29 16:04:58
New to Linq, so apologies if this is basic. This query is throwing up the error {"Cannot cast DBNull.Value to type 'System.Int64'. Please use a nullable type."} when I enumerate the results. private void AddLevels(long rootid) { var results = from row in data.AsEnumerable() where row.Field<long>("ParentID") == rootid select row; foreach (DataRow row in results) { //do stuff } } The column ParentID does accept nulls - do I need to handle these separately? EDIT2: Actual solution below that still uses Linq. EDIT: I solved this by scrapping Linq and just using a DataTable.Select statement instead.

Null value in a parameter varbinary datatype

£可爱£侵袭症+ 提交于 2019-11-29 09:35:15
How can I add a null value in a parameter varbinary datatype? When I execute the following code: using (SqlConnection myDatabaseConnection1 = new SqlConnection(myConnectionString.ConnectionString)) { using (SqlCommand mySqlCommand = new SqlCommand("INSERT INTO Employee(EmpName, Image) Values(@EmpName, @Image)", myDatabaseConnection1)) { mySqlCommand.Parameters.AddWithValue("@EmpName", textBoxEmpName.Text); mySqlCommand.Parameters.AddWithValue("@Image", DBNull.Value); myDatabaseConnection1.Open(); mySqlCommand.ExecuteNonQuery(); } } I get the following System.Data.SqlClient.SqlException :

What should I use to compare DBNull ? Using DBNull.Value or ToString().IsNullOrEmpty()

六月ゝ 毕业季﹏ 提交于 2019-11-29 09:08:40
I can check for a DBnull on a data row using any of the methods. Either by using if(dr[0][0]==DBNull.Value) //do somethin or by doing if(dr[0][0].ToString().IsNullOrEmpty()) //do something In Both Cases I will be getting same result. But Which one is conecptually right approach. Which was will use less resources The first way is somewhat correct. However, more accepted way is: if ( dr[0][0] is DBNull ) And the second way is definitely incorrect. If you use the second way, you will get true in two cases: Your value is DBNull Your value is an empty string Conceptually the comparison to DBNull

Conversion from type 'DBNull' to type 'String' is not valid

一曲冷凌霜 提交于 2019-11-29 01:14:25
i am receiving this problem Conversion from type 'DBNull' to type 'String' is not valid. Line 501: hfSupEmail.Value = dt.Rows(0)("SupEmail") i am very new to this, i am not really sure what is the exact problem could someone guide me? Many thanks The quick and dirty fix: hfSupEmail.Value = dt.Rows(0)("SupEmail").ToString() This works very well when your eventual target and the source data are already strings. This is because any extra .ToString() call for something that's already a string will generally be optimized by the jitter into a no-op, and if it's NULL then the resulting DBNull.Value

C# ADO.NET: nulls and DbNull — is there more efficient syntax?

那年仲夏 提交于 2019-11-28 19:05:38
I've got a DateTime? that I'm trying to insert into a field using a DbParameter . I'm creating the parameter like so: DbParameter datePrm = updateStmt.CreateParameter(); datePrm.ParameterName = "@change_date"; And then I want to put the value of the DateTime? into the dataPrm.Value while accounting for null s. I thought initially I'd be clever: datePrm.Value = nullableDate ?? DBNull.Value; but that fails with the error Operator '??' cannot be applied to operands of type 'System.DateTime?' and 'System.DBNull' So I guess that only works if the second argument is a non-nullable version of the

ExecuteScalar returns null or DBNull (development or production server)

混江龙づ霸主 提交于 2019-11-28 11:10:51
I'm trying to add a column to an existing DataRow in C#. Afterwards the column will be filled with a single value from my database. DataRow dr already exists and column "COLNAME" also exists. comTBP is my SqlCommand . dr["COLNAME"] = Convert.ToInt32(comTBP.ExecuteScalar()); This all works fine if there is a value in my database and ExecuteScalar() can get that value. If I test this code on my development server (local) it also works if ExecuteScalar() return null or DBNull and the value of my new column is 0. But the problem appears if I deploy my code to the production server. If I do

Dealing with System.DBNull in PowerShell

时光总嘲笑我的痴心妄想 提交于 2019-11-28 11:10:35
EDIT: As of PowerShell 7 Preview 2, -not [System.DBNull]::Value evaluates to $true , thanks to Joel Sallow via pull request 9794 Spending more time pulling SQL data in PowerShell. Running into issues with [System.DBNull]::Value and how PowerShell behaves with this during comparisons. Here's an example of the behavior I see, along with workarounds #DBNull values don't evaluate like Null... if([System.DBNull]::Value){"I would not expect this to display"} # The text displays. if([string][System.DBNull]::Value){"This won't display, but is not intuitive"} # The text does not display. #DBNull does

Linq to DataTable - Cannot cast DBNull

久未见 提交于 2019-11-28 10:42:54
问题 New to Linq, so apologies if this is basic. This query is throwing up the error {"Cannot cast DBNull.Value to type 'System.Int64'. Please use a nullable type."} when I enumerate the results. private void AddLevels(long rootid) { var results = from row in data.AsEnumerable() where row.Field<long>("ParentID") == rootid select row; foreach (DataRow row in results) { //do stuff } } The column ParentID does accept nulls - do I need to handle these separately? EDIT2: Actual solution below that

Null value in a parameter varbinary datatype

半世苍凉 提交于 2019-11-28 03:01:55
问题 How can I add a null value in a parameter varbinary datatype? When I execute the following code: using (SqlConnection myDatabaseConnection1 = new SqlConnection(myConnectionString.ConnectionString)) { using (SqlCommand mySqlCommand = new SqlCommand("INSERT INTO Employee(EmpName, Image) Values(@EmpName, @Image)", myDatabaseConnection1)) { mySqlCommand.Parameters.AddWithValue("@EmpName", textBoxEmpName.Text); mySqlCommand.Parameters.AddWithValue("@Image", DBNull.Value); myDatabaseConnection1