2
Answers

Date select option from one column

Photo of Shovan Saha

Shovan Saha

8y
204
1
string sql = "SELECT * FROM LalData WHERE PaymentDate between '"+ dtpStartDate.Value.AddDays(-1).ToString() + "' AND '" + dtpEndDate.Value.ToString()+"'";
 
I need data from start date but when I select a date from datetimepicker, that takes the value from the next day. That is why, I used AddDays(-1). But it is well for within any month. When I select two days from two datetimepicker for two month but it does not work. Can anyone help me? 

Answers (2)

1
Photo of Nilesh Shah
NA 22.3k 215.1k 8y
Did you put a debug point on this line and checked what is the value of the "sql" string?
 
Ideally you shold be able to run that "Sql" string as a query as it is in SQL Server.
 
First try to fix it. Until you are not able to get the result in SQL this way, your code will not work.
 
I suspect here the Value.toString() is producing some date with unexpected format or with date time and seconds fractions.
 
try to create date in YYYY-MM-DD format from the datepicker first.
Accepted
1
Photo of Shovan Saha
NA 306 17.9k 8y
Dear Sir, How can I get data of selected date? I always get the between value. If i use AddDays(-1) and select 1st date of any month, I get previous month's data. I do not use sql server here. It is desktop application with access database. I am new in C#.
 
private void btnSearch_Click(object sender, EventArgs e)
{
con = new OleDbConnection(conString);
string sql = "SELECT * FROM LalData WHERE PaymentDate between '"+ dtpStartDate.Value.AddDays(-1).ToString() + "' AND '" + dtpEndDate.Value.ToString()+"'";
con.Open();
adapter = new OleDbDataAdapter(sql, con);
dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
btnSubmit.Enabled = false;
}