Israel

Israel

  • 693
  • 1.3k
  • 219.4k

Code with specific condition

Aug 16 2019 9:05 AM
Hi,
 
I would like to resolve a logic. Little bit complicated for my.
 
I have four Textboxes (TextBox1, TextBox2, TextBox3, TexBox4) and three Buttons (Button1, button2, Button3).
 
What I need to do?
 
If TextBox1 and TextBox2 or TextBox1 and TextBox3 are filled by number more than 0 or minus 0 then click at Button3 (for example do Button3.PerformClick()). Otherwise don't activate Button3 and read the code above to save to my Database.
  1. private void Button1_Click(object sender, EventArgs e)  
  2. {  
  3. //////////////////////////////////////////////////////////////////// 
  4. // The condition should be here. Its should pass ONLY these TextBoxes when are not filled  
  5. //////////////////////////////////////////////////////////////////
  6. if (conn.State != ConnectionState.Open)  
  7. conn.Open();  
  8. SqlCommand comm1 = new SqlCommand();  
  9. comm1.Connection = conn;  
  10. SqlParameter data = new SqlParameter("@date", SqlDbType.Date);  
  11. SqlParameter nord = new SqlParameter("@number", SqlDbType.VarChar);  
  12. SqlParameter contas = new SqlParameter("@name", SqlDbType.VarChar);  
  13. comm1.Parameters.Add(date);  
  14. comm1.Parameters.Add(number);  
  15. comm1.Parameters.Add(name);  
  16. date.Value = TextBox1.Text;  
  17. number.Value = TextBox2.Text;  
  18. name.Value = TextBox3.Text;  
  19. comm1.Connection = conn;  
  20. comm1.CommandText = "insert into Table ([date],[number],[name])values(@date,@number,@name)";  
  21. {  
  22. if (MessageBox.Show("Are you sure to save?""Saving window", MessageBoxButtons.YesNo) == DialogResult.Yes)  
  23. {  
  24. try  
  25. {  
  26. comm1.ExecuteNonQuery();  
  27. SqlCommand cmd = conn.CreateCommand();  
  28. cmd.CommandType = CommandType.Text;  
  29. cmd.CommandText = "select [date],[number],[name] from Table";  
  30. DataTable dt = new DataTable();  
  31. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  32. da.Fill(dt);  
  33. dgvTest.DataSource = dt;  
  34. }  
  35. finally  
  36. {  
  37. dgvTest.CurrentCell = dgvTest.Rows[dgvTest.Rows.Count - 2].Cells[0];  
  38. }  

Answers (9)