'Test a button in C#
I have the following buttons in C# and i should test their functionality for a university project, but i have no idea how to do that. Watched a few tutorials on Nunit and Xunit but they didnt explain how the buttons should be tested. Can someone please give an example on how i must a test code for these buttons?
private void Savebtn_Click(object sender, EventArgs e)
{
if (PrNameTb.Text == "" || CatCb.SelectedIndex == -1 || QtyTb.Text == "" || PriceTb.Text == "")
{
MessageBox.Show("Missing Information");
}
else
{
try
{
Con.Open();
SqlCommand cmd = new SqlCommand("insert into ProductTbl (PrName,PrCat,PrQty,PrPrice) values(@PN,@PC,@PQ,@PP)", Con);
cmd.Parameters.AddWithValue("@PN", PrNameTb.Text);
cmd.Parameters.AddWithValue("@PC", CatCb.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@PQ", QtyTb.Text);
cmd.Parameters.AddWithValue("@PP", PriceTb.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Product Added");
Con.Close();
DisplayProducts();
Clear();
}
catch (Exception Ex)
{ MessageBox.Show(Ex.Message); }
}
}
public void DeleteBtn_Click(object sender, EventArgs e)
{
if (Key == 0)
{
MessageBox.Show("Select an Product");
}
else
{
try
{
Con.Open();
SqlCommand cmd = new SqlCommand("Delete from ProductTbl where PrId = @PKey", Con);
cmd.Parameters.AddWithValue("@PKey", Key);
cmd.ExecuteNonQuery();
MessageBox.Show("Product Deleted");
Con.Close();
DisplayProducts();
Clear();
}
catch (Exception Ex)
{ MessageBox.Show(Ex.Message); }
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
