Reputation: 39
I need help, When I select tomorrow date then only today's data got. How to handle it? I don't have set any date. I have only date in database not datetime.
private static DataTable getData(string table, DateTime startDate, DateTime endDate)
{
string constr = ConfigurationManager.ConnectionStrings["db_Demo"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table1 WHERE Entry_Datetime between '" + startDate + "' and '" + endDate + "'"))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}
}
}
}
This is view
<label for="startDate" style="color:mediumvioletred">From:</label>
@Html.TextBox("startDate", DateTime.Today.ToString("yyyy-MM-dd"), new { @class = "form-control date", type = "date" })
<label for="endDate" style="color:mediumvioletred">To:</label>
@Html.TextBox("endDate", DateTime.Today.ToString("yyyy-MM-dd"), new { @class = "form-control date", type = "date" })
<br />
<input type="submit" class="btn btn-primary" value="Download" />
}
Upvotes: 0
Views: 2566
Reputation: 753
I hope this will fix your issue..
string startDate = dtp_dfrom.Value.ToString("YYYY-MM-DD");
string endDate = dtp_dto.Value.ToString("YYYY-MM-DD");
string constr = ConfigurationManager.ConnectionStrings["db_Demo"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table1 WHERE Entry_Datetime
between @startDate and @endDate"))
{
DataTable dt = new DataTable();
cmd.Parameters.AddWithValue("@startDate", startDate);
cmd.Parameters.AddWithValue("@endDate", endDate);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}
Upvotes: 1
Reputation: 87
You probably have startDate set to eg 00:00:00 30/12/2019 and endDate as 00:00:00 31/12/2019. Try adding one day to endDate so that you'll have 00:00:00 1/01/2020 and you'll have results from 00:00:00 30/12/2019 to 23:59:59 31/12/2019
Upvotes: 1