I have an access table called LoginTable with text columns named Username, Password and a integer column called group. A windows form called AddUser with a textbox called Username_txtBx and a combobox called Department_cmbBx. and also a button called Add_btn . I can add a user with the following code in the button click event. But how would I go about having it Search the database to check if the Username already exists and if it does throw a messagebox telling the user it does and if it doesn't run the code below. I have found a lot of examples for SQL databases but none for an Access database.
try
{
int g = new int();
if (Department_cmbBx.SelectedItem.ToString() == "Office")
{
g = 1;
}
else if (Department_cmbBx.SelectedItem.ToString() == "Stores")
{
g = 2;
}
else if (Department_cmbBx.SelectedItem.ToString() == "Workshop")
{
g = 3;
}
else if (Department_cmbBx.SelectedItem.ToString() == "Management")
{
g = 4;
}
else if (Department_cmbBx.SelectedItem.ToString() == "Admin")
{
g = 5;
}
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into LoginTable(Username,[Password],[Group]) values ('" + Username_txtBx.Text + "','password'," + g + ")";
command.ExecuteNonQuery();
connection.Close();
Username_txtBx.Text = "";
Department_cmbBx.Text = "";
}
catch (Exception ex)
{
MessageBox.Show("error " + ex);
}