Click here to Skip to main content
15,917,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to runn the following query from my App to fill a gridview with records from a Database with records between specified date ranges.

DataTable timeEntries = new DataTable("tblTimeSheetDetails");
OleDbCommand selectAlltimeEntries = conn.CreateCommand();
selectAlltimeEntries.CommandText = "SELECT [tblTimesheetDetails].[EmployeeID], [tblTimesheetDetails].[TSProjectCodeID], [tblTimesheetDetails].[TimeSheetDate], [tblTimesheetDetails].[HoursWorked] FROM [tblTimeSheetDetails]";
//conn.Open();
timeEntries.Load(selectAlltimeEntries.ExecuteReader(CommandBehavior.CloseConnection));
DataView dv = new DataView(timeEntries, "EmployeeID = '?' AND TimeSheetDate >= '#" + mondaydate + "#' AND TimeSheetDate <= '#" + sundaydate + "#'", "TimeSheetDate", DataViewRowState.CurrentRows);
OleDbParameter p1 = new OleDbParameter();
OleDbParameter p2 = new OleDbParameter();
OleDbParameter p3 = new OleDbParameter();
p1.Value = statuslblUsername.Text;
p2.Value = mondaydate.ToString();
p3.Value = sundaydate.ToString();
//MessageBox.Show({0}, p1.ToString());
dataGridView1.DataSource = dv;
Posted
Comments
Sandeep Mewara 25-Jul-10 15:46pm    
So what is the issue? Looks like you forgot to ask question!

Create the command
Use Parameters (?) in command text
Add parameters to command (convert to matching data type)
Fill table
Use Table for data binding

I did no tests with a real database, but it should help you to start.

Dont forget to add error handling!

Here's a sample code:

using (OleDbConnection conn = new OleDbConnection("CONNECTIONSTRING"))
{
    OleDbCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT [tblTimesheetDetails].[EmployeeID], [tblTimesheetDetails].[TSProjectCodeID], [tblTimesheetDetails].[TimeSheetDate], [tblTimesheetDetails].[HoursWorked] FROM [tblTimeSheetDetails] where EmployeeID=? AND TimeSheetDate BETWEEN ? AND ?";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Add(new OleDbParameter("p1", int.Parse(statuslblUsername.Text)));
    cmd.Parameters.Add(new OleDbParameter("p2", DateTime.Parse(mondaydate.Text)));
    cmd.Parameters.Add(new OleDbParameter("p3", DateTime.Parse(sundaydate.Text)));

    conn.Open();
    DataTable timeEntries = new DataTable("tbl");
    timeEntries.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
    dataGridView1.DataSource = timeEntries;
}
 
Share this answer
 
Hi Guys. I think I may have inadvertantly left out the question. I have managed to resolve this. Thanks again for the advise.

The below bit of code seem to have worked for me.

DateTime now = DateTime.Now;
int dayOfWeek = (int)now.DayOfWeek;
dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;
DateTime startOfWeek = now.AddDays(1 - (int)now.DayOfWeek);
DateTime sun = now.AddDays(7 - (int)now.DayOfWeek);
string mondaydate = startOfWeek.ToString("yyyy/MM/dd");
string sundaydate = sun.ToString("yyyy/MM/dd");
DateTime mondaydate1 = Convert.ToDateTime(startOfWeek.ToString("yyyy/MM/dd"));
string sql = @"select EmployeeID, TSProjectCodeID, TimeSheetDate, HoursWorked, TaskCode, Notes from tblTimesheetDetails WHERE EmployeeID = '" + statuslblUsername.Text + "' AND TimeSheetDate BETWEEN '" + mondaydate + "' AND '" + sundaydate + "'";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900