Hello
I need to retrieve data in grid view based on specific date in text box and by using stored procedure,
but the grid view not fill and nothing happened.
this is my code
C#
protected void btndate_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
try
{
using (SqlConnection con = new SqlConnection(n))
{
using (SqlCommand command1 = new SqlCommand("RetrieveByDate", con))
{
con.Open();
if (txtfrom.Text != string.Empty && txtfrom.Text != null && txtfrom.Text != "" && textto.Text != string.Empty && textto.Text != null && textto.Text != "")
{
command1.CommandType = CommandType.StoredProcedure;
command1.Parameters.AddWithValue("@Date1", txtfrom.Text);
command1.Parameters.AddWithValue("@Date2", textto.Text);
command1.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(command1);
DataTable dt = new DataTable();
sda.Fill(dt);
dgQueries.DataSource = dt;
}
else
{
}
}
}
}
catch (Exception ex)
{
string error = ex.ToString();
}
}
}
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted Jul 16, 2023, 2:10 AM EDT
Hi
Ensure GridView Properties: Make sure you have set the necessary properties for the GridView control, such as
AutoGenerateColumnsandDataKeyNames. Additionally, make sure you have added the GridView control to the page's markup.Missing GridView Binding: After setting the data source for the GridView, you need to call the
DataBind()method to bind the data and display it in the GridView. AdddgQueries.DataBind()after setting the data source.Verify Stored Procedure: Double-check that the stored procedure "RetrieveByDate" exists in your SQL Server database and accepts the correct parameters. Ensure that the stored procedure returns the desired data.
Validate Text Box Inputs: Add server-side validation to ensure that the date inputs (
txtfromandtextto) are in the correct format and contain valid dates before executing the stored procedure.DateTime fromDate; DateTime toDate; if (DateTime.TryParse(txtfrom.Text, out fromDate) && DateTime.TryParse(textto.Text, out toDate)) { // Execute stored procedure and populate the GridView } else { // Show error message or handle invalid date inputs }Handle Empty or Null Values: In your current code, when the text boxes are empty or null, no action is taken. You may want to handle this case by displaying a message or performing some other appropriate action.
If the answer helps kindly accept thanks
Name Mame164 reputationPosted Jul 16, 2023, 5:29 AM EDT
Hi
all things are done
but i trace the code and appear the error and thats what is
" System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'dgQueries'. Remove one definition. "
Jignesh KumarRank #91 · 22.9K reputationPosted Jul 16, 2023, 11:46 PM EDT
Hello,
Please pass properdate formate and do null check in youe code, please try this one,
protected void btndate_Click(object sender, EventArgs e) { if (Page.IsValid) { try { using (SqlConnection con = new SqlConnection(n)) { using (SqlCommand command1 = new SqlCommand("RetrieveByDate", con)) { con.Open(); if (!string.IsNullOrEmpty(txtfrom.Text) && !string.IsNullOrEmpty(textto.Text)) { command1.CommandType = CommandType.StoredProcedure; command1.Parameters.AddWithValue("@Date1", txtfrom.Text); command1.Parameters.AddWithValue("@Date2", textto.Text); command1.ExecuteNonQuery(); SqlDataAdapter sda = new SqlDataAdapter(command1); DataTable dt = new DataTable(); sda.Fill(dt); dgQueries.DataSource = dt; } else { } } } } catch (Exception ex) { string error = ex.ToString(); } } }Check your store procedure as well. It should have two paramter for from date and to date.
Name Mame164 reputationPosted Jul 17, 2023, 2:31 AM EDT
Hello,
The grid view still empty...
Jignesh KumarRank #91 · 22.9K reputationPosted Jul 17, 2023, 3:02 AM EDT
Could you please share your store procedure ? it will help to identify issue.