validate the date from the database in asp.net using c#

Last Reply on Apr 08, 2014 11:20 PM By Mudassar

Posted on Apr 08, 2014 11:12 PM

 

Database as follows
 
studentid       crbillno                DOM

133                  124290           20-01-2014

In run mode screen as follows
 
Batch Date 28-06-2014
 
i have one button called Submit.
 
When i click the submit i want to validate dropdownlist date must be less than DOB date(DOM).
 
for that my code as follows

DateTime coursedate;
        coursedate = Convert.ToDateTime(DdlBthDates.SelectedItem.Text.ToString());
        Sql = "select DOM from course where stud_id = '" + Session["stud_id"] + "' and CR_BILL_NO  =  '" + Session["cr_bill_no"] + "'";
        coursedr = drr.ExecRdr(Sql);
        if (coursedate < Convert.ToDateTime(coursedr[0].ToString().Trim()))
        {
            Label23.Text = "date must be ";
            return;
        }
        coursedr.Close();

when i run the code shows error as follows
 
Invalid attempt to read no data present.
 
the error shows in below line as follows

 if (coursedate < Convert.ToDateTime(coursedr[0].ToString().Trim()))
       
 

 

You are viewing reply posted by: Mudassar on Apr 08, 2014 11:20 PM.
Posted on Apr 08, 2014 11:20 PM

@narsiman,

There are some mistakes in your code

1. You must use Parameterized queries instead of concatenating SQL like you did.

2. You are missing the If condition coursedr.Read() which checks whether the query has returned data or not

coursedr = drr.ExecRdr(Sql);
if(coursedr.Read())
{
        if (coursedate < Convert.ToDateTime(coursedr[0].ToString().Trim()))
        {
            Label23.Text = "date must be ";
            return;
        }
        coursedr.Close();
}