0

page showing the Error

    DateTime startDate = DateTime.ParseExact(txtstart.Text, "MM/dd/yyyy", null);
    DateTime endDate = DateTime.ParseExact(txtend.Text, "MM/dd/yyyy", null);

    string n1 = DropDownList2.SelectedItem.Text;

    if (DropDownList1.SelectedItem.Text == "Membership")// here you can add selectedindex as well
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ToString());
        con.Open();
        SqlDataAdapter adapter = new SqlDataAdapter("select p.Name,m.* from Membership_det m INNER JOIN Personal_det p  ON m.FID= p.FID where m.updateDate  between @Start and @End and m.FID =" + n1 + "", con);
        adapter.SelectCommand.Parameters.Add("@Start", SqlDbType.Date).Value = startDate;
        adapter.SelectCommand.Parameters.Add("@End", SqlDbType.Date).Value = endDate;
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        con.Close();
        GridView1.DataSource = dt;
        GridView1.DataBind();

        // you can use this datatable dt to get that items and use dt to bind the corresponding control.

    }

I need date validation code.. It should accept the date in the format mm/dd/yyyy or else it should give error message

The aspx code is shown below

    <asp:TextBox ID="txtstart" runat="server" ></asp:TextBox>


    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter in the date in MM/dd/yyyy Format" ControlToValidate="txtstart"></asp:RequiredFieldValidator>

    <asp:Label ID="Label2" runat="server" Text="End Date:"></asp:Label>

    <asp:TextBox ID="txtend" runat="server" ></asp:TextBox>
 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Enter in the date in MM/dd/yyyy Format" ControlToValidate="txtend"></asp:RequiredFieldValidator>    

It gives the debug message and straightly goes to the code..wehn the error appears while running the program..I just want to display a error message in the page itself

4 Answers 4

2

Set maxlength property of your textbox 10,

<asp:TextBox ID="txtvaliddate" runat="server" MaxLength="10"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                   ControlToValidate="txtvaliddate" ValidationExpression="^(([1-9])|(0[1-9])|(1[0-2]))\/((0[1-9])|([1-31]))\/((19|20)\d\d)$" Display="Dynamic" SetFocusOnError="true" ErrorMessage="invalid date">*</asp:RegularExpressionValidator>

in c# If you need to specify which date format you want to use, you would use DateTime.ParseExact (MSDN Article)

string[] formats= { "MM/dd/yyyy" }
DateTime dateTime = DateTime.ParseExact(txtstart.Text, formats, new CultureInfo("en-US"), DateTimeStyles.None);
Sign up to request clarification or add additional context in comments.

7 Comments

@SainPradeepfor both text box should i use this..?Start date and end date..?
If you want to check in both text box then you need to add separate regular expression validator to each one.
Its working but in c# code DateTime startDate = DateTime.ParseExact(txtstart.Text, "MM/dd/yyyy", null); It giving error as String was not recognized as a valid DateTime.
DateTime startDate = DateTime.ParseExact(txtstart.Text, "MM/dd/yyyy", null); This above line gives the error -->String was not recognized as a valid DateTime When the wrong input is given...Instead of giving error message...invalid date
DateTime startDate = DateTime.ParseExact(txtstart.Text, "MM/dd/yyyy", null);
|
1

Its always suggested to use a date picker control rather than manually entering it Ajax date control

Comments

0

Regular expressions that are already shown here will help you on the client side but you also need to validate these on the server side.

Also, you should use DateTime.TryParse instead of DateTime.ParseExact because second one will throw exception if something is not ok.

DateTime startDate; 
DateTime endDate;

if (DateTime.TryParse(txtstart.Text, out startDate) && DateTime.TryParse(txtend.Text, out endDate))
{
     string n1 = DropDownList2.SelectedItem.Text;

     if (DropDownList1.SelectedItem.Text == "Membership")
     {
      SqlConnection con = new 
SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ToString());
       con.Open();
      SqlDataAdapter adapter = new SqlDataAdapter("select p.Name,m.* from Membership_det m INNER JOIN Personal_det p  ON m.FID= p.FID where m.updateDate  between @Start and @End and m.FID =" + n1 + "", con);
      adapter.SelectCommand.Parameters.Add("@Start", SqlDbType.Date).Value = startDate;
      adapter.SelectCommand.Parameters.Add("@End", SqlDbType.Date).Value = endDate;
      DataTable dt = new DataTable();
      adapter.Fill(dt);
      con.Close();
      GridView1.DataSource = dt;
      GridView1.DataBind();                
      }
}
else
{ 
     //Show error message
}

I’d also add another parameter to your SQL query for m.FID parameter just like you added for @Start and @End. This makes your code vulnerable to SQL injection.

Comments

0

Use validation Expression

ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.