未检查空状态 [英] Empty condition not checked

查看:71
本文介绍了未检查空状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码工作正常,但当用户未填写项目说明时未检查空状态。







this code is working fine but it is not checked the empty condition when user not fill item description.



private void btnSave_Click(object sender, EventArgs e)
       {
           string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
           SqlDataReader reader = null;
           SqlConnection conn = null;
           try
           {
               string query = "insert into ItemTable values('" + cmbItemType.Text +  "','" + txtItemCode.Text + "','" + txtItemDescription.Text + "','" + txtLaborCharge.Text + "')";
               conn = new SqlConnection(connstr);

               if (txtItemDescription.Text != "" & txtItemCode.Text != "")
               {
                   conn.Open();
                   //Checking User Name Exists in CustomerTable
                   string query1 = "select txtItemCode from ItemTable where txtItemCode='" + txtItemCode.Text + "'";
                   SqlCommand cmd = new SqlCommand(query1, conn);
                   reader = cmd.ExecuteReader();
                   if (reader != null && reader.HasRows)
                   {
                       //User exists in db do something
                       MessageBox.Show("Item Already Exists!!");
                   }
                   else
                   {
                       if (reader != null)
                       {
                           reader.Close(); // close the reader before making a new connection
                       }
                           SqlCommand cmd1 = new SqlCommand(query, conn);
                           cmd1.ExecuteNonQuery();
                           txtItemDescription.Clear();
                           txtLaborCharge.Clear();
                           txtItemCode.Clear();
                           MessageBox.Show("Values Save in DataBase");

                   }
                   reader.Close();
                   conn.Close();
               }
               else
               {
                   MessageBox.Show("Only Labor Charge Can be Empty");
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("At Least 0 Value In Labor Charge", ex.Message);
           }
           finally
           {
               if (reader != null)
               {
                   reader.Close();
               }
               if (conn != null)
               {
                   conn.Close();
               }
           }
       }

推荐答案

只是为了解决这个问题list



您需要添加&到你的if语句



Just to get this off the unanswered list

You need to add a & to your if statement

if (txtItemDescription.Text != "" & txtItemCode.Text != "")





应该是



should be

if (txtItemDescription.Text != "" && txtItemCode.Text != "")





&本身就是这里描述的逻辑AND运算符[ ^ ]



什么你真正想要的是有条件的和描述的这里 [ ^ ]



& by itself is the logical AND operator described here[^]

What you really want is the conditional AND described here[^]


这篇关于未检查空状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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