Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c# date and form help

Status
Not open for further replies.

bouwob

Programmer
Apr 12, 2006
171
US
if (Convert.ToBoolean(Request.QueryString["searchEngineID"]) && (Convert.ToDateTime(Request.QueryString["calStartDate"]) < Convert.ToDateTime(Request.QueryString["calEndDate"])))

The problem I am having though is that if calStartDate or calEndDate is null or calStartDate > calStartDate the if statement needs to be false to move onto the next elseif. The program is crashing when it tries to convert the null to datetime. any workarounds for this?
 
Put them into variables first and then convert them if their values are not null.
 
Something like this...
Code:
            bool searchEngineID = false;
            string calStartDate = System.DBNull.Value ? null : Request.QueryString["calStartDate"];
            string calEndDate = System.DBNull.Value ? null : Request.QueryString["calEndDate"];

            if (calStartDate != null || calEndDate != null)
            {
                if (Convert.ToBoolean(searchEngineID) && 
                    (Convert.ToDateTime(calStartDate) <
                     Convert.ToDateTime(calEndDate)))
                {
                    //Put the code here...
                }
            }

Sharing the best from my side...

--Prashant--
 
gotta love those trinary operators.

unfortunately this does not compile since System.DBNull.Value is not a boolean, and QueryString returns a string or null and not DbNull.

try:

Code:
if ( Request.QueryString["calStartDate"] != null && Request.QueryString["calStartDate"].Length > 0 )
{
  //...
}


mr s. <;)

 
ok... to make things even easier and if you sitting on a larger then just 5 line project, define functions that do the conversion work for you:

Code:
public bool String2Bool(string strValue, bool bDefault)
{
   try
   {
       return Convert.ToBoolean(strValue);
   }
   catch (Exception)
   {
       return bDefault;
   }
}

if (String2Bool(searchEngineID, false) &&
    .....

------------------
When you do it, do it right.
 
Btw, guys, none of your code guarantee the program wont crash. Because if I pass 'alabama' (not null, length > 0) for calStartDate, Convert.ToDateTime will through an exception.

------------------
When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top