Hi All

Can anyone help me with this problem please.
I am writing a program in C# and have an access db07. The user should be able to enter a date into a text box then that date is addedd to my sql query and then return all record with that date. I keep getting the error data type miss match. When i code in a date into the query itself it works but not when passed from a text box. The data type of the field in access is Date/Time general date. Not sure what i need to do???

Heres the code.

public static string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "" + Application.StartupPath + "\\WorkforceDB.accdb";
        
public Form1()
{
     InitializeComponent();
}

private void bnQuery_Click(object sender, EventArgs e)
{

DataSet dataSet = GetDataFromSqlStatement("SELECT Staff.FName, Staff.SName, Shift.SDate, Shift.SStart, Shift.SEnd FROM Staff INNER JOIN Shift ON Staff.[StaffID] = Shift.[StaffID]WHERE Shift.SDate = '" + tbStartDate.Text + "';"); 

DataTable dataTable = dataSet.Tables[0];

foreach (DataRow dataRow in dataTable.Rows)
{
lb1.Items.Add(dataRow["FName"].ToString() + " " + dataRow["SName"].ToString() + " - " + dataRow["SDate"].ToString());

}
}

public static DataSet GetDataFromSqlStatement(string sqlStatement)
{

OleDbConnection connection = new OleDbConnection(ConnectionString);
connection.Open();

OleDbDataAdapter adapter = new OleDbDataAdapter(sqlStatement, ConnectionString);

DataSet dataSet = new DataSet();

adapter.Fill(dataSet);

connection.Close();

return dataSet;

}

>When i code in a date into the query itself it works but not when passed from a text box.

Use mm/dd/yy date format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.