This is my table in db
SQL
create table gridview(id bigint identity(1,1),name varchar(65),startdate varchar(1000),enddate varchar(1000),gender char(25),mobileno bigint,designition varchar(100),
country nvarchar(65),state nvarchar(65),city nvarchar(65))i want to write a procedure for from date to to to date
once i click on button it should display only records between the two dates only pls help me
This is my procedure
SQL
create PROCEDURE sp_SearchCustomerDetailsByDate
@StartDate varchar(100),
@EndDate varchar(100)
AS
BEGIN
select * from gridview where startdate between @StartDate and @EndDate
ENDThis is My code for getting date
C#
private DataTable GetDate(string fromdate, string enddate)
{
using (SqlCommand cmd = new SqlCommand("sp_SearchCustomerDetailsByDate", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StartDate",fromdate);
cmd.Parameters.AddWithValue("@EndDate",enddate);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}This is my button1click code
C#
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = GetDate(TextBox12.Text,TextBox13.Text);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}pls help me out

Tuhin PaulRank #7 · 71.9K reputationPosted Feb 24, 2023, 8:06 AM EST
Hi Jaya,
Your code looks mostly correct. However, there are a few minor things you could improve.
Firstly, instead of using varchar(1000) for startdate and enddate, you should use date or datetime data types. This will allow you to use the BETWEEN operator directly on the date values without having to worry about string formatting issues.
Here's the updated CREATE TABLE statement with the date columns changed to datetime data type:
CREATE TABLE gridview ( id bigint identity(1,1), name varchar(65), startdate datetime, enddate datetime, gender char(25), mobileno bigint, designition varchar(100), country nvarchar(65), state nvarchar(65), city nvarchar(65) );Next, it's a good practice to explicitly specify the columns you want to select from the table instead of using SELECT *. This can improve query performance and reduce network traffic.
Here's the updated stored procedure code:
CREATE PROCEDURE sp_SearchCustomerDetailsByDate @StartDate datetime, @EndDate datetime AS BEGIN SELECT id, name, startdate, enddate, gender, mobileno, designition, country, state, city FROM gridview WHERE startdate BETWEEN @StartDate AND @EndDate ENDFinally, when passing the date values to the stored procedure, you should convert them to DateTime objects instead of passing them as strings. You can use DateTime.Parse or DateTime.TryParse to do this.
Here's the updated GetDate method:
private DataTable GetDate(string fromdate, string enddate) { DateTime startDate; DateTime endDate; if (!DateTime.TryParse(fromdate, out startDate) || !DateTime.TryParse(enddate, out endDate)) { // Handle invalid date inputs here return new DataTable(); } using (SqlCommand cmd = new SqlCommand("sp_SearchCustomerDetailsByDate", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@StartDate", startDate); cmd.Parameters.AddWithValue("@EndDate", endDate); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); return dt; } } }With these changes, your code should work as expected.
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 24, 2023, 3:59 AM EST
Hello Jaya,
Your SP whould be like below:
CREATE PROCEDURE GetGridViewByDateRange @StartDate VARCHAR(1000), @EndDate VARCHAR(1000) AS BEGIN SELECT * FROM gridview WHERE CONVERT(DATE, startdate, 101) BETWEEN CONVERT(DATE, @StartDate, 101) AND CONVERT(DATE, @EndDate, 101) END //101 is style code, which represents the mm/dd/yyyy format.Thanks
Naimish Makwana
Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 24, 2023, 4:12 AM EST
getting error sir Conversion failed when converting date and/or time from character string.