Hey all,

I am having trouble with a query and nonquery in C# while accessing an Access database.

The query I am having problems with is using the LIKE statement in the query.

sqlString =
                    "SELECT * "
                    + "FROM Convocations " 
                    + "WHERE Name LIKE \"*" + txtFilterParameter.Text + "*\""
                    + "ORDER BY " + getSortField() + " " + getSortDirection();`

That works when directly pasted into Access, but for some reason not when I have it going in C#.

The nonquery I'm having trouble with at the moment is INSERT.

(The Value names are the same as the Category names, I know. I did it to keep them lined up.

string sqlStatement =
                           "INSERT INTO Convocations (Name, Classification, Date, Location) "
                           + "VALUES ('" + Name + "', '" + Classification
                           + "', '" + Date +"', '" + Location + "')";

For the INSERT statement, it is saying that it has a Syntax Error, but I debugged it and couldn't find anythign wrong.

An example return:

INSERT INTO Convocations (Name, Classification, Date, Location) VALUES ('My Birthday', 'Party', '11/16/1992 5:30:00 PM', 'My House')

Recommended Answers

All 2 Replies

Are you using ADO, OLEDB or DAO?
Are you using an ODBC source?
Wich is the Access versión?
Can you put your connection string?

Hmmmm, I can see a few things that don't seem quite right in the SQL statements you've got in there. Firstly, make sure that the connection to your database is established without any errors. Secondly, have a look at table names and fields that you're trying to query.

With the first query, Wouldn't you have something like this

"SELECT * FROM Convocations 
WHERE Name LIKE = 'txtFilterParameter.Text'  
ORDER BY  getSortField() , getSortDirection()";

The second query is:

"INSERT INTO Convocations (Name, Classification, Date, Location) 
 VALUES ('Name', 'Classification', 'Date', 'Location')";

You may or may not need the single quotes around Name, Classfication.. so on.. That depends on how you've set up your script. I'm assuming they're variables?

I hope that helps..

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.