0

I'm struggling to change a variable from a database into a usable Int so I can compare against the database to delete rows.

Here's the code I'm using:

private void EditEmployee_Load(object sender, EventArgs e)
    {
        DataTable table = new DataTable(); //creates a Table to store the data before populating the ComboBox
        string connstring = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\HoliPlanData.accdb;Persist Security Info=False";
        try
        {
            using (OleDbConnection conn = new OleDbConnection(connstring))
            {
                conn.Open();
                string query = "SELECT PayrollNo, (FirstName + ' ' + LastName) AS NAME FROM [Employee]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
                adapter.Fill(table);
                DropBoxEmp.DataSource = table;
                DropBoxEmp.DisplayMember = "NAME";                    
                DropBoxEmp.ValueMember = "PayrollNo";
                string BadNumber = DropBoxEmp.ValueMember;
                int GoodNumber = Int32.Parse(BadNumber);

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

The problem is that the string BadNumber is set to the string "PayrollNo", instead of a string version of the Int that the Datatable stores PayrollNo as. When I set the DisplayMember to "PayrollNo", the combobox displays 1 and 2 and so on, as the Int's selected in the SELECT query.

Why does BadNumber take the literal assigned to DropBoxEmp.ValueMember instead of the numbers? Any help would be gratefully received.

4
  • "PayrollNo" is the name of the column in the database? If this name doesn't match, you will just select it as a string. Commented Feb 24, 2016 at 12:44
  • Yes PayrollNo is the name of the first Column in the database. Commented Feb 24, 2016 at 12:52
  • 1
    Did you check the table values with debuger to see if you get what you expect from the execution of the query? Do a print of table.Rows[0][1].ToString(). Commented Feb 24, 2016 at 12:59
  • The query Executes fine, I checked using a break in the code and opened the table view. First column has PayrollNo's and Second Column has the appended names Commented Feb 24, 2016 at 13:02

2 Answers 2

2

You want DropBoxEmp.SelectedItem, not ValueMember. You are misunderstanding what the ValueMember property is. It tells the combo box which field in its binding to use as the backing value.

To unbox, do this:

var theItem = DropBoxEmp.SelectedItem as DataRowView;
MessageBox.Show(theItem[0].ToString());

-- as an aside, the data adapter will open the connection so you don't need to do that.

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

7 Comments

Selected Value is set as an object and cannot be converted to string
@Josh, yes, you must unbox it and access the desired property. I believe it is a DataRowView.
I apoligize for not stating earlier that my coding skill is very low. This is my first coding project and im sorry but I don't know how to unbox and access the desired property, how would I do this?
@Josh, See my edit, I changed to SelectedItem and show how to unbox.
That worked a treat, thank you very much for the help . Is theItem a copy of the Collumn in the database? Would I be able to Query it against the data for equal terms to delete a row?
|
1

Try using the column name and the row index to get the value:

if(table.Rows.Count==1) // if it's supposed to be unique
{
    int GoodNumber = Int32.Parse(table.Rows[0]["PayrollNo"].ToString());
}
else
{
    //Show some error
}

1 Comment

Opps, at the beginning I thought of check to be just greater than zero, then I realized that this is surely supposed to be unique and changed it, but forgot to replace the 0 with 1 for the ==. It's just a type, because I'm not actually compiling, sorry.

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.