I am selecting some data from a access database which is in Date/Time format in the Database and also in the programme the variable the information is stored in is registered as Date/Time.
However when I run the programme it says there is a data type mismatch, has anyone any idea of what the correct two datatypes should be for my select query to work?
protected void Page_Load(object sender, EventArgs e)
{
string searchDatesValue = "";
DateTime startdt = DateTime.MinValue;
DateTime enddt = DateTime.MinValue;
string userNameValue = "";
string userIPValue = "";
searchDatesValue = Request.QueryString["StartDate"];
userNameValue = Request.QueryString["UserName"];
userIPValue = Request.QueryString["ip"];
string[] splittedstring = searchDatesValue.Split(new Char[] { ' ' }); //splitts the string fed in from the start and finsih dates
startdt = Convert.ToDateTime(splittedstring[0]); //drops the correct date in as the start date
enddt = Convert.ToDateTime(splittedstring[2]); //drops the correct date in as the end date.
if (searchDatesValue != null)
{
searchDate(startdt, enddt);
}
else if (userNameValue != null)
{
userName(userNameValue);
}
else if (userIPValue != null)
{
userIP(userIPValue);
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
public void searchDate(DateTime startdt, DateTime enddt)
{
string connectionString = GetConnectionString();
string queryString =
"SELECT * FROM tlogLoginAttempt WHERE dtmLoginAttempt BETWEEN'" + startdt + "' AND '" + enddt + "'"; ;
using (OleDbConnection connection =
new OleDbConnection(connectionString))
{
OleDbCommand command = connection.CreateCommand();
command.CommandText = queryString;
try
{
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
}
catch (Exception ex)
{
}
finally
{
connection.Close();
}
}
}
I don't know if this helps but here is my code for this page.
Member
21 Points
87 Posts
Conversion from C# DateTime to Access DateTime
Feb 07, 2011 12:09 PM|Edmondo|LINK
Hi All,
I am selecting some data from a access database which is in Date/Time format in the Database and also in the programme the variable the information is stored in is registered as Date/Time.
However when I run the programme it says there is a data type mismatch, has anyone any idea of what the correct two datatypes should be for my select query to work?
I don't know if this helps but here is my code for this page.
Many thanks for any help or advice :-)
Member
39 Points
143 Posts
Re: Conversion from C# DateTime to Access DateTime
Feb 07, 2011 12:36 PM|richardterris|LINK
I think the SQL DateTime has to be smalldatetime to get a DateTime from Access
There's a good article about data types:
http://www.databasejournal.com/features/mssql/article.php/1490561/Data-Structure-Differences-between-Access-and-SQL-Server.htm
All-Star
25756 Points
7025 Posts
Re: Conversion from C# DateTime to Access DateTime
Feb 07, 2011 01:28 PM|hans_v|LINK
You're using the worng delimiter. A ' is used for strings, you should use a # for dates. But it's good practice to use parameterized queries instead!
http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access
And more about datetimes and access:
http://www.mikesdotnetting.com/Article/92/MS-Access-Date-and-Time-with-ASP.NET
Member
21 Points
87 Posts
Re: Conversion from C# DateTime to Access DateTime
Feb 08, 2011 04:38 AM|Edmondo|LINK
Hi Thanks very much for advice! This has worked a treat!