Question

I am building a simple Point of Sale program and working on a "search invoice" button that allows up to 3 search criteria (InvoiceID , ClientName, and ClientID). These are the names of 3 of the columns in the table named "Invoicing".

InvoiceID is the key column of type Int32, ClientName is of type String, ClientID is of type Int32. ClientName and ClientID searches work perfect.

MY PROBLEM: If I include InvoiceID in the select query, I get the following error. And I have spent a few days trying to figure it out.

ERROR: Database Error: Datatype mismatch in criteria expression.

Can you more experienced programmers help me out? thank you!

String connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data" + @" Source=TESTDB.accdb";
            String tableName = "Invoicing";
            String query = String.Format("select * from [{0}] where", tableName);
            //ADD IN SEARCH CRITERIA
            int filled = 0;
            if (invoiceBox.Text != "") { query += " InvoiceID='" + invoiceBox.Text+"'"; filled += 1; }
            /*if (DateCheckBox.Checked == true) 
            {
                if (filled>=1) { query += " and DateNTime='" + monthCalendar1.SelectionStart.ToString() + "'"; filled += 1; }
                else { query += " DateNTime='" + monthCalendar1.SelectionStart.ToString()+ "'"; filled += 1; }
            }
             * */
            if (ClientNameBox.Text != "") //Doesnot work
            {
                if (filled >= 1) { query += " and Client='" + ClientNameBox.Text + "'"; filled += 1; }
                else { query += " Client='" + ClientNameBox.Text + "'"; filled += 1; }
            }

            if (ClientIDBox.Text != "") //THIS search criteria works!!!!
            {
                if (filled >= 1) { query += " and ClientID='" + ClientIDBox.Text + "'"; filled += 1; }
                else { query += " ClientID='" + ClientIDBox.Text + "'"; filled += 1; }
            }
            //CHECK IF SEARCH CRITERIA ARE PRESENT
            if (filled < 1) { MessageBox.Show("At least One Search criteria above is required"); return; }

            DataSet dsInvoicing = new DataSet();
            OleDbConnection conn = new OleDbConnection(connectionString);
            try
            {
                //Open Database Connection
                conn.Open();
                OleDbDataAdapter daInvoicing = new OleDbDataAdapter(query, conn);
                //Fill the DataSet
                daInvoicing.Fill(dsInvoicing, tableName);
                //MessageBox.Show("dsInventory has "+dsInventory.Tables[0].Rows.Count+" search results");
                conn.Close();
               this.dataGridView1.DataSource = dsInvoicing.Tables[0];
            }
            catch (OleDbException exp){ MessageBox.Show("Database Error: " + exp.Message.ToString());}

Need more information? I will post up more if I haven't provided enough. DATABASE INFORMATION or other.

Thank you very much to all programmers.

Was it helpful?

Solution

Looks like the data type of InvoiceID in your database is some numeric kind. While in query you are treating it as string. Try not to wrap InvoiceID value in single quotes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top