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
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();
}
}
}
Jignesh KumarPosted Jul 17, 2023, 7:02 AM
Could you please share your store procedure ? it will help to identify issue.
Name MamePosted Jul 17, 2023, 6:31 AM
Hello,
The grid view still empty...
Jignesh KumarPosted Jul 17, 2023, 3:46 AM
Hello,
Please pass properdate formate and do null check in youe code, please try this one,
Check your store procedure as well. It should have two paramter for from date and to date.
Name MamePosted Jul 16, 2023, 9:29 AM
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. "
Mohamed Azarudeen ZPosted Jul 16, 2023, 6:10 AM
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.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