user3605321
user3605321

Reputation: 45

DB connection adapter "Data type mismatch"

I have succesufuly setup a connection to my acces DB (.mdb) using OleDb.I am encountering a

"Data type mismatch in criteria expression."

error if I use a "where" statement in the SQL command.If I remove it I succesufuly get all the Comments.

Any ideas?

My database looks something like this:

ID (short text)     Comment (long text)
431                 They_study_math
321                 They_study_biology

and my code looks like this:

public void auth_group(string group)
    {
        connDB.Open();
        DataSet ds = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT Comment FROM groups WHERE ID=431", connDB);
        adapter.Fill(ds);
        connDB.Close();

        DataTable dt = ds.Tables[0];
        foreach (DataRow dr in dt.Rows)
        {
            listBox1.Items.Add(dr["Comment"].ToString());
        }
    }

Upvotes: 0

Views: 141

Answers (2)

RSobhani
RSobhani

Reputation: 36

your id field is string . you should write like this:

public void auth_group(string group) { connDB.Open(); DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT Comment FROM groups WHERE ID='431'", connDB); adapter.Fill(ds); connDB.Close();

    DataTable dt = ds.Tables[0];
    foreach (DataRow dr in dt.Rows)
    {
        listBox1.Items.Add(dr["Comment"].ToString());
    }
}

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98750

I feel like your ID column is a character type, that's why you should use it with single quotes like;

WHERE ID = '431'

I have to say this, looks like the right type for your ID column is integer not character.

Also use using statement to dispose your database connections and OleDbDataAdapter.

Upvotes: 1

Related Questions