Reputation:
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
Upvotes: 0
Views: 25743
Reputation: 1089
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.
Upvotes: 0
Reputation: 3125
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);
Upvotes: 2
Reputation: 76
Its always suggested to use a date picker control rather than manually entering it Ajax date control
Upvotes: 1
Reputation: 3740
Use validation Expression
ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d)"
Upvotes: 0