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.