如何在按钮单击事件中执行if-else条件语句 [英] How to execute an if-else conditional statement within button-click event

查看:149
本文介绍了如何在按钮单击事件中执行if-else条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I got a situation here. I need to insert values into tables depending on what a user provides on the Window form. If a good does not exist and more than is necessary is acquired the the excess must be entered into a table called "BulkAvailable" this is where a big exists in my code as when I comment this part out the code runs well. Please find the piece of code below




When I run the code it returns an exception message: "String of binary data would be truncated. The statement has been terminated"





我尝试了什么:





What I have tried:

try
           {
               SqlConnection con = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False");
               con.Open();
               float a = float.Parse(textBox8.Text, System.Globalization.CultureInfo.InvariantCulture);
               int b = int.Parse(textBox9.Text);
               float c = a * b;
               var T = c.ToString(System.Globalization.CultureInfo.InvariantCulture);

               float x = float.Parse(textBox4.Text, System.Globalization.CultureInfo.InvariantCulture);
               int z = int.Parse(textBox3.Text);
               float y = x * z;
               var total = y.ToString(System.Globalization.CultureInfo.InvariantCulture);

               int d = b - z;

               string uba = "insert into BulkSale(ProductName, ProductSource, Date, Quantity, Type, UnitPrice, Total, Nature) values('" + textBox1.Text + "', '" + textBox2.Text + "', '" + dateTimePicker1.Value + "', '" + textBox3.Text + "', '" + textBox6.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "', '"+textBox7.Text+"')";
               string A = "insert into BulkInput(ProductName, ProductSource, Date, Quantity, Type, UnitPrice, Total, Nature) values('"+textBox1.Text+"','"+textBox2.Text+"','"+dateTimePicker1.Value+"','"+b+"','"+textBox6.Text+"','"+a+"','"+c+"', '"+textBox7.Text+"')";
               SqlCommand cmd = new SqlCommand(uba, con);
               SqlCommand X = new SqlCommand(A, con);
               cmd.ExecuteNonQuery();
               X.ExecuteNonQuery();

               try
               {
                   if (int.Parse(textBox9.Text) > int.Parse(textBox3.Text))
                   {
                       string B = "insert into BulkAvailable(ProductSource,ProductName,Date,Quantity,Type) values('" + textBox2.Text + "','" + textBox1.Text + "','" + dateTimePicker1.Text + "','" + d + "','" + textBox6.Text + "')";
                       SqlCommand Bc = new SqlCommand(B, con);
                       Bc.ExecuteNonQuery();
                   }

                   else
                   {
                       MessageBox.Show("You successfully Bought and Sold", " ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   }
               }
               catch (Exception aze)
               {
                   MessageBox.Show(aze.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               }

               MessageBox.Show("Operation Successfully Executed", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
               con.Close();
           }
           catch (Exception er)
           {
               MessageBox.Show(er.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
           }
       }

推荐答案

正如所指出的那样,消息说你试图插入一个太大的值一栏。



然而,更大的问题是程序的结构:

- 你不使用参数,SQL的问题注入,数据类型等

- 你没有处置对象,你过度使用资源

- 只有在没有引发异常的情况下关闭连接等等。



因此代码应该类似于

As pointed out the message says that you try to insert a value too large to a column.

However, the bigger problem is the structure of the program:
- You don't use parameters, problems with SQL injection, data types etc
- You don't dispose objects, you overuse resources
- You close the connection only in a situation where no exception is raised and so on.

So the code should look something like
try
{
   using (SqlConnection con = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False")) {
      con.Open();
      float a = float.Parse(textBox8.Text, System.Globalization.CultureInfo.InvariantCulture);
      int b = int.Parse(textBox9.Text);
      float c = a * b;
      var T = c.ToString(System.Globalization.CultureInfo.InvariantCulture);
 
      float x = float.Parse(textBox4.Text, System.Globalization.CultureInfo.InvariantCulture);
      int z = int.Parse(textBox3.Text);
      float y = x * z;
      var total = y.ToString(System.Globalization.CultureInfo.InvariantCulture);
 
      int d = b - z;
 
      string uba = @"insert into BulkSale(ProductName, ProductSource, Date, Quantity, Type, UnitPrice, Total, Nature) 
                     values (@ProductName, @ProductSource, @Date, @Quantity, @Type, @UnitPrice, @Total, @Nature)";
      using (SqlCommand cmd = new SqlCommand(uba, con)) {
         cmd.Parameters.AddWithValue("@ProductName", textBox1.Text);
         cmd.Parameters.AddWithValue("@ProductSource", textBox2.Text);
         cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value);
         cmd.Parameters.AddWithValue("@Quantity", textBox3.Text);
         cmd.Parameters.AddWithValue("@Type", textBox6.Text);
         cmd.Parameters.AddWithValue("@UnitPrice", textBox4.Text);
         cmd.Parameters.AddWithValue("@Total", textBox5.Text);
         cmd.Parameters.AddWithValue("@Nature", textBox7.Text);
         cmd.ExecuteNonQuery();
         ...



还尝试使用UI对象的描述性名称。而不是 textBox1 使用例如 ProductName 。这同样适用于方法中的变量名称(例如 cmd X )。这使得代码更容易调试和理解,特别是当你稍后再回来时。



什么是连接字符串,你可能在很多地方使用它程序所以从可维护性的角度来看,将它存储在一个地方,也许是在一个配置文件中。



执行数据库命令后,请看一下正确执行数据库操作 [ ^ ]


Also try to use descriptive names with the UI objects. Instead of textBox1 use for example ProductName. The same applies to variable names in methods (like cmd or X). This makes the code easier to debug and understand especially when you come back to it later.

What comes to the connection string, you probably use it in many places in the program so from the maintainability point of view, store it in a single place, perhaps in a configuration file.

What comes to executing the database commands, have a look at Properly executing database operations[^]


这篇关于如何在按钮单击事件中执行if-else条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆