Validate that DataTable cell is not null or empty
A null value would be returned as DBNull
, and not a null literal.
var tab = new System.Data.DataTable();
tab.Columns.Add("c1");
tab.Rows.Add();
tab.Rows[0]["c1"] = null;
tab.Rows[0]["c1"].GetType() //returns DBNull
And as the DBNull.ToString() is guaranteed to return an empty string, you can safely use !String.IsNullOrEmpty(value.ToString()))
.
You can always guard against not being sure if an object is null by simply using null coalescing.
!String.IsNullOrEmpty(value?.ToString()))
Checking for null value with DataTable.Select
CopyToDataTable should fix the issue:
LV_Employees.DataSource = employees.Select("Name LIKE '%" + TB_Search.Text + "%'")
.CopyToDataTable();
LV_Employees.DataBind();
How to get a value that is not null in a datatable column
Try this
DataTable dt = new DataTable();
dt.Columns.Add("emp_name", typeof(string));
dt.Columns.Add("gender", typeof(string));
dt.Columns.Add("Position", typeof(string));
DataRow[] result = dt.Select("Position != null and Position != ''");
foreach (DataRow row in result)
{
string position = row.Field<string>(2);
}
How to check empty DataTable
If dataTable1
is null, it is not an empty datatable.
Simply wrap your foreach in an if-statement that checks if dataTable1
is null.
Make sure that your foreach counts over DataTable1.Rows
or you will get a compilation error.
if (dataTable1 != null)
{
foreach (DataRow dr in dataTable1.Rows)
{
// ...
}
}
How to determine a session variable contains data table is null or empty in C#
Your test is returning false because you are using a 'safe cast': as
casting will return null if the conversion can't be made (see more on the C# guide here). Since you are trying to cast a datatable to a string, this will always return null, even when your session variable has a value.
To see the true nature of your problem, try converting them like this:
if(!string.IsNullOrEmpty((string)Session["tblItems"]))
This should thrown an exception, because you can't convert a datatable to a string like this. You will need to test for null like this:
if (Session["tblItems"] != null))
Update: having seen your comments, above, I've added a fuller example to explain what need to change. Your code should look more like this:
DataTable dt;
if (Session["tblItems"] != null))
{
dt = (DataTable)Session["tblItems"];
...
}
else
{
dt = new DataTable("tblItems1");
...
}
DataRow dr;
dr = dt.NewRow();
...
Note the changes I've made: initialise dt
from session if there is a value, the cast will work in this case, and when it is null, construct a new one. You can then use the value in the section below without issue, because it will always have a value.
In addition, I would also recommend that you don't store your datatable in session at all, but rather just the values that you need.
Related Topics
Build Query String for System.Net.Httpclient Get
Empty String Not Being Converted to Null When Passing Json Object to Controller
C# How to Check If a Url Exists/Is Valid
How to Submit Disabled Input in ASP.NET MVC
How to Refresh an Entity Framework Core Dbcontext
Discord.Net C# 1.0.2 How to Send Messages to Specific Channels
How to Rename Keys Within a Json File
How to Create a 2D Array from a CSV File
How to Map to a Dictionary Object from Database Results Using Dapper Dot Net
C# SQL Server Stored Procedure Parameter Return List
How to Match Hyphens With Regular Expression
Access Variables/Functions from Another Component
Read and Parse a Json File in C#
How to Perform Join Between Multiple Tables in Linq Lambda
How to Show a Console Output/Window in a Forms Application
How to Redirect to Login Page If Session Is Not Available in MVC