Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Fellas,

I got stuck up in a piece of code and it has been troubling me for quite some time now. I would really like your help on this one.

What I'm trying to do is to connect to an Microsoft Access database, create an SQL statement, create an OledbCommand, add the OledbParameters and try to execute the query. However, the query is returning incorrect data.

First off, here's my code:
C#
string dbPath = "D:\\reports.mdb";

List<string> emails = new List<string>();
emails.Add("user1@e2k10.lcl");
emails.Add("aliasuser1@e2k10.lcl");

StringBuilder sbContactsLastComm = new StringBuilder();
sbContactsLastComm.Append("SELECT ");
sbContactsLastComm.Append("MAX([meces_processed].[datetime]) ");

sbContactsLastComm.Append("FROM ");
sbContactsLastComm.Append("[meces_processed], ");
sbContactsLastComm.Append("[meces_users], ");
sbContactsLastComm.Append("[meces_emailusers] ");

sbContactsLastComm.Append("WHERE ");
sbContactsLastComm.Append("[meces_emailusers].[userid] = [meces_users].[id] ");
sbContactsLastComm.Append("AND [meces_emailusers].[emailid] = [meces_processed].[uniqueguid] ");
sbContactsLastComm.Append("AND [meces_users].[emailaddress] = ? ");
sbContactsLastComm.Append("AND [meces_processed].[uniqueguid] IN ");
sbContactsLastComm.Append("( ");

sbContactsLastComm.Append("SELECT DISTINCT ");
sbContactsLastComm.Append("[meces_processed].[uniqueguid] ");

sbContactsLastComm.Append("FROM ");
sbContactsLastComm.Append("[meces_emailusers], ");
sbContactsLastComm.Append("[meces_users], ");
sbContactsLastComm.Append("[meces_processed] ");

sbContactsLastComm.Append("WHERE ");
sbContactsLastComm.Append("[meces_emailusers].[userid] = [meces_users].[id] ");
sbContactsLastComm.Append("AND [meces_emailusers].[emailid] = [meces_processed].[uniqueguid] ");
sbContactsLastComm.Append("AND [meces_users].[emailaddress] IN ( ");

for (int i = 0; i < emails.Count; ++i)
	sbContactsLastComm.Append("?, ");

sbContactsLastComm.Remove(sbContactsLastComm.Length - 2, 2);
sbContactsLastComm.Append(" ) ) ");

using (var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath))
{
	conn.Open();

	OleDbCommand cmd = new OleDbCommand(sbContactsLastComm.ToString(), conn);
	cmd.Parameters.Add(new OleDbParameter("contact", "user@domain.com"));


	for (int i = 0; i < emails.Count; ++i)
	    cmd.Parameters.Add(new OleDbParameter("email" + i, emails[i]));

	foreach (OleDbParameter p in cmd.Parameters)
		Console.WriteLine(p.ParameterName + " = " + p.Value);

	object o = cmd.ExecuteScalar();

	conn.Close();
}


Secondly, here's what I've tried:

I outputted the sql statement and it's correct. I outputted the OledbCommand parameters and they're correct too.

What I've also tried is I put the sql into Microsoft Access and replaced the ?s with the actual parameters and the statement executed successfully. Then I also tried hardcoding the paramters into the sql instead of through the use of OledbParameters and it worked fine as well.

I don't want to end up having the hardcode my sql statements as I would like to use OledbParameters for obvious reasons.

Any help / pointers would be greatly appreciated!!

Edit: I've tried the exact same thing using SqlServer (and SqlConnection etc.) and this worked perfectly fine. Still no luck with Access though! Any help / pointer would be really appreciated!!
Posted
Updated 3-Feb-14 4:49am
v2
Comments
Prasad Avunoori 3-Feb-14 23:20pm    
Hi Ryan,

1. Can you please share the expected result and actual result?
2. Put a breakpoint and capture the value of sbContactsLastCom and execute in MSACCESS.
Ryan Zahra 4-Feb-14 3:03am    
Hi Prasad,

Thanks for your reply.

1. The expected result should be a date and time, however, I'm receiving an empty result (DBNULL)
2. I've already did that, and replaced the ?s with actual values and it worked fine in MSACCESS
Prasad Avunoori 4-Feb-14 3:57am    
Try this

DateTime dateTime = cmd.ExecuteScalar();
Ryan Zahra 4-Feb-14 4:07am    
Can't do that because cmd.ExecuteScalar() is returning a DBNull value. Therefore, it will give me an invalid cast exception.

1 solution

 
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