I would like to know how to select a category value from a combo box and the data according to the selection parses to a data grid view from a sql database, after that I want to click on a cell and the data on that cell row must parse to text boxes in the form and I need to make changes to the record and update the record or delete it from the database. I've tried the following code but I keep getting errors.
private void dataGridView_StaffDetails_CellClick(object sender, DataGridViewCellEventArgs e)
{
string staffType = comboBox_Type.SelectedItem.ToString();
if (comboBox_Type.SelectedIndex != -1)
{
comboBox_Type.SelectedItem.ToString();
}
else
{
Btn_Search.Enabled = false;
}
//---SQL Command to Select Data
try
{
//---Open Connection
connection.Open();
//---SQL Command to Select Data
SqlCommand cmd = new SqlCommand("Select * from StaffDetails where Staff_Type = '" + staffType + "' ", connection);
//---Execute SQL Command
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
//---Read Data and assign to text boxes, radio buttons and combo box
while (reader.Read())
{
Txt_FirstName.Text = reader["Customer_FirstName"].ToString();
Txt_LastName.Text = reader["Customer_LastName"].ToString();
Txt_Username.Text = reader["Username"].ToString();
Txt_Password.Text = reader["Password"].ToString();
Txt_SalaryPerMonth.Text = reader["Salary_Per_Month"].ToString();
string StaffType = reader["Staff_Type"].ToString();
comboBox_Type.SelectedItem = staffType;
Btn_Update.Enabled = true;
Btn_Delete.Enabled = true;
}
}
else
{
MessageBox.Show("Sorry! No record was found ");
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
connection.Close();
}
}