Hi, i have a Gridview that displays records by using a button click after specifying date range. My requirement is to display data when page loads using todays date. NB: I have a hidden textbox in the form that captures today's date. Any help?
Thank you

Vishal JoshiRank #377 · 6.7K reputationPosted Jan 4, 2023, 8:22 AM EST
Hello Ahmed,
You can try the below code to get the expected result.
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.BindGrid(); } } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("SELECT * FROM table WHERE tblDate = " + txtHiddenDate.Text)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } }Thanks
Pasang TamangRank #449 · 5.6K reputationPosted Jan 4, 2023, 3:54 AM EST
Hi,
It should work by using same code that you have used in button click. You have to use same code in page load. Date Range value you can use today's date DateTime.Now. You can check this example also https://www.aspsnippets.com/questions/528010/Filter-ASPNet-GridView-using-DataView-for-Date-range-using-C-and-VBNet/
Regards,
Pasang
Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 4, 2023, 4:08 AM EST
Create a common method with input as a date parameter to load the data, which suppose to be called from the OnPageLoad event and Button click event. In OnpageLoad pass the input from hidden text box, if it has value take the value otherwise pass it as datetine.now. While doing the button click, prompt alert if you wana to do, like input is empty. otherwise apply the same logic, take the specific date range otherwise get today datetime. hope this helps you
Sachin SinghRank #66 · 26.2K reputationPosted Jan 4, 2023, 5:15 AM EST
simply in you stored procedure keep the datetime column default value GetDate() so that for initial load without parameters it will fetch the current date records only
Alter procedure yourSP @YourDateTime DateTime=getDate(), ....other columns as begin select * from table where DateTimeColumn=@YourDateTime endAhmed Khalid185 reputationPosted Jan 4, 2023, 12:38 PM EST
I have modified my procedure as below:
but i am getting error in '" + txt_hiddenDate.Text + "'
noting that txt_hiddenDate is a textbox ID in the form and is displaying default Today's date on page load
Sachin SinghRank #66 · 26.2K reputationPosted Jan 5, 2023, 1:19 AM EST
@Ahmed, it should be
SELECT[FullName], [TaskDate], [TaskType], [TaskStatus] FROM[TaskTransMaster_Tbl] where TaskDate = '" + Convert.ToDateTime(txt_hiddenDate.Text) + "' ORDER BY [PrayerDate] DESCAhmed Khalid185 reputationPosted Jan 5, 2023, 2:39 AM EST
Hi Sachin,
I have modified but it still shows the error in the textbox reference as shown in attachment. I have verified that the text field ID for the hidden textbox in the form is exactly the same
Ahmed Khalid185 reputationPosted Jan 5, 2023, 1:03 PM EST
Dear Vishal,
I have applied your suggestion, it works properly. however, when i deployed in live, the grid with today's date selection is never displayed. I deployed the same code as in development and testing environment. Any idea where the issue is?
Thanks & Rgrds