简体   繁体   中英

Object is not an ADODB Record set = C# filling datatable

I am trying to bring back records from my access DB by matching the number selected in my comboBox2 and bring back all the rows in relation to that number - this has worked fine in other projects but for some reason I keep getting the related error above - any help would be nice.. the error is on the dap.Fill(dt, typeof(Int32)); - When I take "typeof int32 out" I get this error: "SYNTAX ERROR MISSING OPERATOR IN EXPRESSIONS 'OCR = ';"

EDIT: if it makes a difference I am using this code in this type of event:

EDIT FIXED WIH HELP FROM ALL COMMENTS - FIXED CODE NOW ADDED AFTER CODE THAT DIDNT WORK

comboBox2_SelectedValueChanged

   string strprovider = @"provider=microsoft.jet.oledb.4.0;data source=C:\Users\farrejos\Documents\inboxV2.mdb;persist security info=false";
            OleDbConnection newConn = new OleDbConnection(strprovider);
            System.Data.DataTable dt = new System.Data.DataTable();
            //DataSet ds = new DataSet();
            //ds.Tables.Add(dt);
            OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);

            if (dap != null)
            {
                //dap.Fill(ds);

                dap.Fill(dt, typeof(Int32));
            }

            //dap.Fill(ds, "ocr");

            foreach (DataRow myRow in dt.Rows)
            {
                textBox3.Text = myRow[4].ToString();
                textBox4.Text = myRow[6].ToString();
            }
        }

FIXED CODE:

  System.Data.DataTable dt = new System.Data.DataTable();
        DataSet ds = new DataSet();

        OleDbDataAdapter da = new OleDbDataAdapter("Select *  from ocr where [OCR] = " + comboBox2.SelectedText.ToString() + "", newConn);

        da.Fill(dt);
        foreach (DataRow myRow in dt.Rows)
        {
            textBox3.Text = myRow[4].ToString();

            textBox4.Text = myRow[6].ToString();
        }

Do like this

 string strprovider = @"provider=microsoft.jet.oledb.4.0;data source=C:\Users\farrejos\Documents\inboxV2.mdb;persist security info=false";
        OleDbConnection newConn = new OleDbConnection(strprovider);
        System.Data.DataTable dt = new System.Data.DataTable();
        DataSet ds = new DataSet();
        //ds.Tables.Add(dt);
        OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);

        if (dap != null)
        {                
            dap.Fill(ds);
        }

        foreach (DataRow myRow in ds.Tables[0].Rows)
        {
            textBox3.Text = myRow[4].ToString();
            textBox4.Text = myRow[6].ToString();
        }
    }

If your OCR field is a string field, you might need to enclose your ComboBox value with apostrophes:

OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = '" + comboBox2.SelectedText.ToString() + "'", newConn);

EDIT

I have not done it in a few years, but I believe to remember that the OleDbDataAdapter gave me some troubles occasionally when using it with a where clause in the constructor.

Try it with an OleDbCommand instead?

            OleDbDataAdapter dap = new OleDbDataAdapter();
            dap.SelectCommand = new OleDbCommand("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);
            dap.Fill(dt, typeof(Int32));

Edit 2

Your SQL statement uses "ocr" as both table name and field name. Possibly this is an ambigiouty the provider has troubles with?

Change it to:

Select * from [ocr] where [ocr].[OCR] = ...

Some more ideas, try it out in the following order:

  • Instead of * list the field names you want to select manually
  • Enclose table name with [ ] brackets
  • Enclose field names with [ ] brackets

  • Rewrite your code to use OleDbParameter for your queries.

In any way, I strongly suggest to use parameterized queries anyway. This will help you to get syntactical problems straight as well as it helps you to protect against SQL injection attacks.

You need to start to break down your code, firstly as its complaining about the SQL expression I would move it to its own variable.

    DataSet ds = new DataSet();
    //ds.Tables.Add(dt);
    string sql = String.Format("select * from [ocr] where [ocr].[OCR] = {0}", comboBox2.SelectedText);
    System.Diagnostics.Debug.WriteLine(s);

    OleDbDataAdapter dap = new OleDbDataAdapter(sql, newConn);

Then in the output window you'll see the SQL it tried to execute which should help you narrow down the root cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM