473,467 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading DateTime table from mdb file with C#

Hi everybody!
I have a C# app that connects to a mdb file and displays all the
fields from a table
in dataGridView. I have trouble searching in a Access Date/Time field
like this.

string searchDate = "12/11/2007";
Result = DateTime.Parse(Result,
CultureInfo.CurrentCulture).ToShortDateString();

searchDate = "'" + searchDate + "'";

strSQL = "SELECT * FROM TableName WHERE Date=" +
searchDate;
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,
this.conn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);

DataSet ds = new DataSet();
da.Fill(ds, "TableName");

I recieve the following error at run-time:
"Data type mismatch in criteria expression"

I guess the problem has something to do with Regional Settengs.

Jul 9 '07 #1
6 5422
On Jul 9, 10:48 am, jr1024 <jr1...@gmail.comwrote:
Hi everybody!

I have a C# app that connects to a mdb file and displays all the
fields from a table
in dataGridView. I have trouble searching in a Access Date/Time field
like this.

string searchDate = "12/11/2007";
Result = DateTime.Parse(Result,
CultureInfo.CurrentCulture).ToShortDateString();

searchDate = "'" + searchDate + "'";

strSQL = "SELECT * FROM TableName WHERE Date=" +
searchDate;
You should use a parameterised query instead of inserting the date
directly into the SQL.
See the docs for OleDbParameter for an example.

Jon

Jul 9 '07 #2
Hi,

"jr1024" <jr****@gmail.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
Hi everybody!
I have a C# app that connects to a mdb file and displays all the
fields from a table
in dataGridView. I have trouble searching in a Access Date/Time field
like this.
Do this, put a breakpoint right after this line, and see what it contains:

strSQL = "SELECT * FROM TableName WHERE Date=" +
searchDate;

will be like
SELECT * FROM TableName WHERE Date= 4/2/2007

So basically you are making a division :)

You have to use a delimiter, IIRC Access use # to delimite dates.

But the CORRECT solution is using a parameter as Skeet suggest.
Jul 9 '07 #3
On Jul 9, 4:59 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
Hi,

"jr1024" <jr1...@gmail.comwrote in message

news:11**********************@n60g2000hse.googlegr oups.com...
Hi everybody!
I have a C# app that connects to a mdb file and displays all the
fields from a table
in dataGridView. I have trouble searching in a Access Date/Time field
like this.

Do this, put a breakpoint right after this line, and see what it contains:

strSQL = "SELECT * FROM TableName WHERE Date=" +
searchDate;

will be like
SELECT * FROM TableName WHERE Date= 4/2/2007

So basically you are making a division :)

You have to use a delimiter, IIRC Access use # to delimite dates.

But the CORRECT solution is using a parameter as Skeet suggest.

Thank you all for replying.
I was able to get the select query to work like that:

strDateSearch = "#" + dt.Month.ToString() + "/"
+ dt.Day.ToString() + "/" +
dt.Year.ToString() + "#";
But I also want to update the Date fields in the mdb file.
And when I do so with:

OleDbDataAdapter da = ...;
DataSet ds = new DataSet();
da.Fill(ds, "TableName");
//...
da.Update(ds, "TableName");
I get Update syntax error. The same code updates string and number
fields no problem.

I will take a look at OleDbParameter later today.
Thank you for your time.

Jul 10 '07 #4
Hi,

Thank you all for replying.
I was able to get the select query to work like that:

strDateSearch = "#" + dt.Month.ToString() + "/"
+ dt.Day.ToString() + "/" +
dt.Year.ToString() + "#";
That might solve your problem, but it's not the best solution, use a
Parameterized query instead
>

But I also want to update the Date fields in the mdb file.
And when I do so with:

OleDbDataAdapter da = ...;
DataSet ds = new DataSet();
da.Fill(ds, "TableName");
//...
da.Update(ds, "TableName");
I get Update syntax error. The same code updates string and number
fields no problem.

I will take a look at OleDbParameter later today.
Thank you for your time.
What error you are getting?
Jul 10 '07 #5
On Jul 10, 5:21 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
But I also want to update the Date fields in the mdb file.
And when I do so with:
OleDbDataAdapter da = ...;
DataSet ds = new DataSet();
da.Fill(ds, "TableName");
//...
da.Update(ds, "TableName");
I get Update syntax error. The same code updates string and number
fields no problem.
I will take a look at OleDbParameter later today.
Thank you for your time.

What error you are getting?
I get unhandled exception: "Syntax error in UPDATE statement"

And I get same error even when I try do it like that:

private void dataGrid_CellEndEdit(object sender,
DataGridViewCellEventArgs e)
{
DataGridViewCell theCell = dataGrid[e.ColumnIndex,
e.RowIndex];
DataGridViewCell ID_Cell = dataGrid[e.ColumnIndex-1,
e.RowIndex];

if (theCell.ValueType == typeof(DateTime))
{
if (conn != null)
conn.Close();
conn = new OleDbConnection(strConn);

DateTime dt = (DateTime)theCell.Value;
string updateQ = "UPDATE TableName SET Date=@p1 WHERE
ID=@p2";

conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = updateQ;
OleDbParameter p1 = new OleDbParameter("@p1",
OleDbType.DBDate);
p1.Value = dt; //dt.ToOADate();
cmd.Parameters.Add(p1);
OleDbParameter p2 = new OleDbParameter("@p2",
OleDbType.Integer);
p2.Value = ID_Cell.Value;
cmd.Parameters.Add(p2);
try
{
cmd.ExecuteNonQuery();
}
catch(System.InvalidOperationException ex)
{
MessageBox.Show(ex.Message, "zzz Invalid Operation
Exception");
}

}

What am I doing wrong?

Jul 11 '07 #6
Hi,
What if you write the code like "UPDATE TableName SET [Date]=@p1 WHERE
ID=@p2";

I do not remember if DATE is a reserved word

"jr1024" <jr****@gmail.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.com...
On Jul 10, 5:21 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
But I also want to update the Date fields in the mdb file.
And when I do so with:
OleDbDataAdapter da = ...;
DataSet ds = new DataSet();
da.Fill(ds, "TableName");
//...
da.Update(ds, "TableName");
I get Update syntax error. The same code updates string and number
fields no problem.
I will take a look at OleDbParameter later today.
Thank you for your time.

What error you are getting?

I get unhandled exception: "Syntax error in UPDATE statement"

And I get same error even when I try do it like that:

private void dataGrid_CellEndEdit(object sender,
DataGridViewCellEventArgs e)
{
DataGridViewCell theCell = dataGrid[e.ColumnIndex,
e.RowIndex];
DataGridViewCell ID_Cell = dataGrid[e.ColumnIndex-1,
e.RowIndex];

if (theCell.ValueType == typeof(DateTime))
{
if (conn != null)
conn.Close();
conn = new OleDbConnection(strConn);

DateTime dt = (DateTime)theCell.Value;
string updateQ = "UPDATE TableName SET Date=@p1 WHERE
ID=@p2";

conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = updateQ;
OleDbParameter p1 = new OleDbParameter("@p1",
OleDbType.DBDate);
p1.Value = dt; //dt.ToOADate();
cmd.Parameters.Add(p1);
OleDbParameter p2 = new OleDbParameter("@p2",
OleDbType.Integer);
p2.Value = ID_Cell.Value;
cmd.Parameters.Add(p2);
try
{
cmd.ExecuteNonQuery();
}
catch(System.InvalidOperationException ex)
{
MessageBox.Show(ex.Message, "zzz Invalid Operation
Exception");
}

}

What am I doing wrong?

Jul 11 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
9
by: Worker | last post by:
Hi , i`ve been wondering how many days do you guys need to read an average asp.net programming book, answer honestly please, for example 800 pages big, how many pages a day? I just wanna see if i...
9
by: Tom | last post by:
Hi There is a datetime type with length 8 in a table I tried to insert the Local System datetime to it from aspx.cs file. But, the outcome are string sysdatetime =...
1
by: PawelR | last post by:
Hi group, Sorry my English is very little I've question about reading data from database. In sql2k I've table (50k+ records). Commerce application writing to this table same data (up to 5 records...
16
by: Christine | last post by:
I was wondering if it is possible to read in a text file as a data type other than string. I would like to read it in as some type that handles numbers, like double, or float.
4
by: Brian Parker | last post by:
Here's a snippet of code I have: ============================================== DataSet ds = new DataSet(); string strXMLFileName = Path.GetTempFileName(); StreamWriter sw = File.AppendText(...
3
by: Bharathi | last post by:
Hi, I got strucked with reading date value from excel file using C#.NET. For Jan-2000 the value I am getting is 36526.0. For all other dates also I am getting some double value like this. ...
1
by: avbergen | last post by:
Hi, I use the following code to read a xml file: While (reader.Read()) If reader.Name = "DATETIME" Then DateVar = reader.ReadString
7
by: dm3281 | last post by:
Hello, I have this non-standard XML file that I have to manually sort thru each day and verify whether various jobs finish successfully or not. This file isn't a real XML file, so I cannot appear...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.