vendredi 10 avril 2020

Cannot link MS Access data to Label and Combobox when in "If" statement but can if it's not... "No data exist for the row/column"

I'm encountering some problem in my Button "button_checkbarcode" code..

What I'm trying to achieve with this button is to be able to check if the barcode text that is in my "label_txtboxtolabel" exist to my MS Access database... and if the barcode exist - its "Line Number" and "Chase Number" must be displayed to my "label_linenumber" and "comboBox_chasenumber" repectively.

You can see the same codes in my "while", "if", and "if else" about my label and comboBox because I'm testing out first that code to my "while" and commenting out all of my "if" statements.. The code in "while" works! but when I'm trying it to the "if" statements(I've already set my "while" code as comment except the "int") it doesn't work.. the error says:

"System.InvalidOperationException: No data exist for the row/column at System.Data.OleDb.OleDbDataReader.DoValueCheck(Int32 ordinal) at System.Data.OleDb.OleDbDataReader.GetValue(Int32 ordinal) at System.Data.OleDb.OleDbDataReader.get_Item(String name) at nameofproject.Form2.button_checkbarcode_Click(Object_sender,EvetArgs e) in "path location here"\Form2.cs:line230"

The "else" code is working if the text in the label doesn't exist to the database.

I've just copied my codes for "if statements" and "int" in my log-in form as I think it should also do the trick...

As for the details about the datas in my Access Database, I can't make primary key because each tool can be put to many chase like this for example: 1 charger (the tool) = 1 barcode (its unique barcode) ; this charger is designated to Line 1 only ; but can charge 3 phones (not all at once.. but one at a time, like first come first serve. | that's why it has 3 entries of "Line 1" in it.). That's why my query has DISTINCT so that it will compare all the copies of "Line" and just displays one.

I'm sorry if this is very long. I can't think of anything to make this short... and I'm still very newbie in programming.

Hoping that everyone in this community is safe in times like this!!

private void button_checkbarcode_Click(object sender, EventArgs e)
    {
        /*what this button must do is:
         1. transfers the value in textbox to label_txtboxtolabel
         2. connect that value to the database
         3. be able to link the line# from the database to the combobox base on the barcode*/
        label_txtboxtolabel.Text = textBox_scanbarcode.Text; //this is 1. and it works

        //this is 2. and 3. And it works... kind of...
        try
        {
            string accesspath = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\username\Documents\Access\REAL\nameofdatabase.accdb";
            //string accesspath = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\" + Environment.UserName + "\\nameofdatabase.accdb";
            OleDbConnection accessconnection = new OleDbConnection(accesspath);
            accessconnection.Open();
            OleDbCommand accesscommand = new OleDbCommand();
            accesscommand.Connection = accessconnection;

            string Checkbarcodequery = "SELECT DISTINCT [Barcode], [Line Number], [Chase Number] from tablenamehere where Barcode = '"+ label_txtboxtolabel.Text +"'";
            accesscommand.CommandText = Checkbarcodequery;
            OleDbDataReader accessreader = accesscommand.ExecuteReader();

            //int addcount = 0;
            while (accessreader.Read())
            {
                label_linenumber.Text = accessreader["Line Number"].ToString();
                comboBox_chasenumber.Items.Add(accessreader["Chase Number"].ToString());

                //addcount += 1;
            }
            //if (addcount == 1)
            //{
                //label_linenumber.Text = accessreader["Line Number"].ToString();
                //comboBox_chasenumber.Items.Add(accessreader["Chase Number"].ToString());
            //}
            //else if (addcount > 1) //like when there is a duplicate.
            //{
                //label_linenumber.Text = accessreader["Line Number"].ToString();
                //comboBox_chasenumber.Items.Add(accessreader["Chase Number"].ToString());
            //}
            //else
            //{
                //MessageBox.Show("This Barcode does not exist in the database!");
            //}

            accessconnection.Close();
        }
        catch (Exception addnewerror1)
        {
            MessageBox.Show("Error! " + addnewerror1);
        }
    }

Aucun commentaire:

Enregistrer un commentaire