Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
{
            if (grv_studentlist.SelectedRows.Count == 1)
            {
                try
                {
                    string delstu = grv_studentlist.SelectedRows[0].Cells["StudentID"].Value.ToString();

                    connect.Open();
                    string delquery = "Delete From Students Where StudentID = @studentid";
                    SqlCommand cm = new SqlCommand(delquery, connect);
                    cm.Parameters.AddWithValue("@studentid", delstu);

                    int rowsAffected = cm.ExecuteNonQuery();
                    if (rowsAffected > 0)
                    {
                        MessageBox.Show("Student Deleted");
                        LoadingData();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("No Students were Deleted. Please check the selected Student ID");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Delete Failed");
                }
                finally
                {
                    connect.Close();
                }
            }
            else
            {
                MessageBox.Show("Please select Student from the list");
            }
        }