Sk Jha

Sk Jha

  • NA
  • 113
  • 43.4k

date wise filter record in gridview in asp.net

Jan 13 2018 9:35 PM
My code is not working for date wise filter record and bind to gridview. giving error
 
Conversion failed when converting date and/or time from character string.
 
my code is 
  1. protected void btnShow_Click(object sender, EventArgs e)  
  2.     {  
  3.         try  
  4.         {  
  5.               
  6.             SqlCommand cmd = new SqlCommand("select * from OrraStock_Sales where createDate between '@fromDt' and '@toDt'", con);  
  7.             cmd.Parameters.AddWithValue("@fromDt",txtFromDt.Text);  
  8.             cmd.Parameters.AddWithValue("@toDt",DateTime.Parse(txtToDt.Text));  
  9.             con.Open();  
  10.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  11.             ds = new DataSet();  
  12.             da.Fill(ds);  
  13.             GridView1.DataSource = ds;  
  14.             GridView1.DataBind();  
  15.             lblTotRecord.Text = "" + ds.Tables[0].Rows.Count;  
  16.             con.Close();  
  17.         }  
  18.         catch (Exception ex)  
  19.         { Response.Write(ex.Message); }  
  20.     }  
Please help me. 

Answers (1)