Further to sjelen's answer and earlier comment
Quote:
There are so many wrong things in this code, I wouldn't know where to begin.
(which I happen to agree with) I thought I'd have a crack at practising my refactoring and peer review skills. The code I'm posting here may not be perfect but it demonstrates better and/or alternative ways of going about it. Look at the comments I've added to understand why I'm suggesting things.
bool errorsFound = false;
if (textBox2.Text == "")
{
errorProvider1.SetError(textBox2, "Provide Owner name");
errorsFound = true;
}
if (textBox3.Text == "")
{
errorProvider1.SetError(textBox3, "Provide Address");
errorsFound = true;
}
int a = 0, b = 0, c = 0;
if (!int.TryParse(textBox5.Text, NumberStyles.Integer,
CultureInfo.CurrentUICulture, out a))
{
errorProvider1.SetError(textBox5, "u need to enter value");
errorsFound = true;
}
if (!int.TryParse(textBox9.Text, NumberStyles.Integer,
CultureInfo.CurrentUICulture, out b))
{
errorProvider1.SetError(textBox9, "u need to enter value");
errorsFound = true;
}
if (b > a)
{
errorProvider1.SetError(textBox9, "ur amount greater then total");
errorsFound = true;
}
if(toolStripTextBox1.Text.Trim() == "")
{
errorProvider1.SetError(toolStripTextBox1, "enter a name");
errorsFound = true;
}
if(errorsFound)
{
MessageBox.Show("Please correct the errors indicated");
return;
}
try
{
UGIcon.Open();
SqlCommand cmdRead = new SqlCommand("select * from purchase where cm = @cm", UGIcon);
cmdRead.Parameters.Add(new SqlParameter("@cm", toolStripTextBox1.Text));
SqlDataReader s = cmd.ExecuteReader();
bool readSuccess = s.Read();
if(readSuccess && s.HasRows)
{
c = a - b;
textBox10.Text = c.ToString(CultureInfo.CurrentUICulture);
long status = Convert.ToInt64(textBox10.Text);
textBox4.Text = (status == 0) ? "paid" : "pending";
SqlCommand cmdWrite = new SqlCommand("update purchase set om='@p1',address=' @p2', phone='@p3',baled='@p4',paid='@p5',status='@p6',balance='@p7' where cm='@cm'", UGIcon);
cmdWrite.Parameters.Add(new SqlParameter("@p1", textBox2.Text));
cmdWrite.Parameters.Add(new SqlParameter("@p2", textBox3.Text));
cmdWrite.Parameters.Add(new SqlParameter("@p3", maskedTextBox1.Text));
cmdWrite.Parameters.Add(new SqlParameter("@p4", numericUpDown2.Value.ToString()));
cmdWrite.Parameters.Add(new SqlParameter("@p5", textBox9.Text));
cmdWrite.Parameters.Add(new SqlParameter("@p6", textBox4.Text));
cmdWrite.Parameters.Add(new SqlParameter("@p7", textBox10.Text));
cmdWrite.Parameters.Add(new SqlParameter("@cm", toolStripTextBox1.Text));
int rowsUpdated = cmdWrite.ExecuteNonQuery();
if(rowsUpdated == 1)
MessageBox.Show("Details has been updated sucessfully", "Update Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
else if(rowsUpdated > 1)
MessageBox.Show("Duplicate rows on database", "Update Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Details have not been updated ", "Update Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Name not found on database", "Update Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
UGIcon.Close();
}
catch (Exception ex)
{
throw;
}