0

When page_load event is occured i am displaying entire data to the gridview. But initially both From Date and To Date's are empty and if i am trying to switch between pageindex of gridview then it shows above error.

Here is my page_load code...

    protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        TextBox1.Attributes.Add("readonly", "readonly");
        TextBox2.Attributes.Add("readonly", "readonly");

        myConn.Open();
        SqlCommand cmd = new SqlCommand("select User_id, LoginDate from LoginLog", myConn);
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        myConn.Close(); 
    }
}        

Here is my GridView1_PageIndexChanging code...

 public void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    string ToDate = "";
    DateTime dtd = DateTime.Parse(TextBox2.Text);
    dtd = dtd.AddDays(1);
    ToDate = dtd.ToShortDateString();


    myConn.Open();
    SqlCommand cmd = new SqlCommand(@"select User_id , LoginDate from LoginLog where LoginDate between
                                     ('" + TextBox1.Text + "') and ('" + ToDate + "')", myConn);
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    sda.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
    myConn.Close();
}
Kalpesh
  • 37
  • 1
  • 6
  • 1
    For starters, be aware that your code is *wide open* to **SQL injection**. You're basically executing any code your users feel like sending you. Aside from that, where specifically does the error occur? What is the input value you're using? What do you expect that input value to be? Why? – David Aug 27 '18 at 11:33
  • 1
    What is the value of `TextBox2.Text` in the debugger? In general you should invest some time to research this issue, there are thousands of similar questions on SO. – Tim Schmelter Aug 27 '18 at 11:34
  • Possible duplicate of [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) – mjwills Aug 27 '18 at 11:34
  • 1
    The date string you are passing from `TextBox2.Text` it's format should match with the system date where you are running this application. Use `DateTime.ParseExact()` instead of `DateTime.Parse()` so that you can have total control over your passed date string. Giving it a fixed format e.g. dd MM yyyy will allow you overcome such type runtime exception. Please keep in mind same format should be passed from client as well else it will again throw exception. – Suprabhat Biswal Aug 27 '18 at 11:39
  • You will also get this exception when `TextBox2.Text` holds empty/blank value. `DateTime.Parse()` doesn't have ability to parse empty content neither does `DateTime.ParseExact()` have. My advice will be to first check for `!String.IsNullOrEmpty(TextBox2.Text)` and then only proceed ahead. – Suprabhat Biswal Aug 27 '18 at 11:49
  • Possible duplicate of [String was not recognized as a valid DateTime " format dd/MM/yyyy"](https://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy) – Tetsuya Yamamoto Aug 28 '18 at 01:40

0 Answers0