1

Hi want to read data from data base from the given ID (not Premanent). I am using the following code:

        OleDbConnection co = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb");
        co.Open();
        **OleDbCommand cmd = new OleDbCommand("SELECT * FROM Category1 Where ID = '"+textBox1.Text+"'", co);**
        OleDbDataReader reader = cmd.ExecuteReader();

        int i = 1;
        while (reader.Read())
        {
            ListViewItem li = new ListViewItem(i.ToString());
            li.SubItems.Add(reader.GetString(1));
            li.SubItems.Add(reader.GetString(2));
            li.SubItems.Add(reader.GetString(3));
            li.SubItems.Add(reader.GetString(4));
            listView1.Items.Add(li);
            i++;
        }

but it shows me an error message on the Bold line of code:

Data type mismatch in criteria expression.

1
  • Is ID an integer or a string in Access? Don't surround an integer with single quotes. Commented Jan 5, 2011 at 1:50

3 Answers 3

2

Is the ID field numeric? You are comparing it to a string. That might be part of the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Data type mismatch very strongly suggests that it is not.
0

Like other people said: it sounds very much like ID isn't a string. However you should use parameters when adding user input or any variables to your SQL statement: http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx

OleDbCommand cmd = new OleDbCommand("SELECT * FROM Category1 WHERE id = @id", co);
cmd.Parameters.AddWithValue("@id", textBox1.Text);

Comments

0

Like other people said: it sounds very good working

string strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\Db1.accdb";
string strSQL = "SELECT * FROM Tbl1" ;
// create Objects of ADOConnection and ADOCommand
OleDbConnection myConn = new OleDbConnection(strDSN);
OleDbDataAdapter myCmd = new OleDbDataAdapter( strSQL, myConn ); 
myConn.Open();
DataSet dtSet = new DataSet();
myCmd.Fill( dtSet, "Tbl1" );
DataTable dTable = dtSet.Tables[0];
foreach( DataRow dtRow in dTable.Rows )
{
    listBox1.Items.Add( dtRow["id"].ToString());
    listBox2.Items.Add( dtRow["nm"].ToString());
    listBox3.Items.Add(dtRow["ag"].ToString());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.