Reputation: 1332
I have two tables called visits
and members
and i am getting the data by using following query..
string sql= @"SELECT member_Firstname, member_Lastname, member_Postcode,
visit_DateTime, visit_Status, visit_Logout_DateTime, visits.member_Id, visit_AlertMsg
FROM members, visits
WHERE members.member_Id = visits.member_Id
AND members.member_Active LIKE 'y%'";
at here i am getting the visit_DateTime values with by using some comparisons with combobox values
datatable dt = helper.getdata(sql)
foreach (DataRow row in dt.Rows)
{
if (row["visit_Logout_DateTime"] != null)
{
DateTime dtlogout = DateTime.Parse(row["visit_Logout_DateTime"].ToString());
if (dtlogout != null)
{
if (cbPeriod.Text == "Today")
{
newItem.lblTime.Text = dtlogout.ToString("HH':'mm':'ss");
}
else
newItem.lblTime.Text = dtlogout.ToString("yyyy'-'MM'-'dd' - 'HH':'mm':'ss");
}
}
}
but i got the error at this line DateTime dtlogout = DateTime.Parse(row["visit_Logout_DateTime"].ToString());
error : string was not recognised as valid datetime (because of one value in that row is empty)
the row "visit_Logout_DateTime"
i have got the values of "visit_Logout_DateTime"
like this....
firstname lastname postcode visit_Logout_DateTime
------------- -------- --------- ---------------------
rob peter hhd344h
peter chan hy78kjk 2011-09-08 12:09:08
rock sam yudufg3746h 2011-08-08 09:08:45
i have tried that for checking the empty values of this visit_Logout_DateTime like i mentioned above..
but i have failed for chacking ever value is empty or not in that row...
how to check every value in this row (row["visit_Logout_DateTime"]) is empty or not
would any one pls help me on this guys ...
many thanks....
Upvotes: 1
Views: 17575
Reputation: 853
To expand on Kristof's answer you could look at extension methods to tidy the code up a little;
public static DateTime? GetDate( this DataRow Row, string ColumnName )
{
object Value = Row[ ColumnName ];
if( Value == DBNull.Value )
return null;
else
return (DateTime)Value;
}
// Repeat for other types, or use a generic version.
...
DateTime? dtlogout = row.GetDate("visit_Logout_DateTime");
Note the use of the Nullable DateTime (trailing ?) so that it can accept nulls.
Upvotes: 0
Reputation: 3328
You can use DateTime.TryParse
like this:
DateTime date = new DateTime();
if (DateTime.TryParse(row["visit_Logout_DateTime"], out date))
dtlogout = date;
Upvotes: 1
Reputation: 1455
First thing - use TryParse, not Parse to parse values when you're not sure whether the value will be a valid object or not.
For checking all values, try the following code example for checking each value:
DataTable dt = new DataTable("MyTable");
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
if (row[column] != null)
{
string value = row[column].ToString();
if (!String.IsNullOrEmpty(value))
{
// Do something
}
}
}
}
Upvotes: 1
Reputation: 6911
You cannot check row against null
. You should compare to DBNull.Value
.
So, either go with:
row["visit_Logout_DateTime"] != DBNull.Value
or
!string.IsNullOrEmpty(row["visit_Logout_DateTime"].ToString())
Upvotes: 0
Reputation: 10941
Instead of checking if the column is null
, you should check if the contents of the column are null
. You can do that by comparing it to DBNull.Value
:
if (row["visit_Logout_DateTime"] != DBNull.Value)
{
...
}
Upvotes: 4