如果记录中有值显示消息 [英] If record has value show message in label

查看:64
本文介绍了如果记录中有值显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想当有人在数据库中输入包含完成的数字时



在标签完整中显示并清除txtbox



i试试这个



但不工作



当我输入数字时仍然完成标签不变,当我更改记录数量



我尝试过:



 private void label13_TextChanged(object sender,EventArgs e)
{



SqlConnection con = new SqlConnection(数据源= NAWAF;初始目录= waterreport;集成安全性=真);
con.Open();
SqlCommand luuud;
string luud =select * from reportonetmp WHERE no ='+ label13.Text +';
luuud = new SqlCommand(luud,con);
SqlDataReader reader = luuud.ExecuteReader();
if(reader.Read())
{
textBox1.Text = reader [rec_report]。ToString();
textBox2.Text = reader [rec_code]。ToString();
textBox3.Text = reader [initial_date_repair]。ToString();
dateTimePicker1.Text = reader [initial_hour_repair]。ToString();
label9.Text = reader [customername]。ToString();
label11.Text = reader [customerphone]。ToString();

}




reportCheck();




}

public void reportCheck()
{
string fni =finish;
label12.Text =;
SqlConnection con = new SqlConnection(Data Source = NAWAF; Initial Catalog = waterreport; Integrated Security = True);
con.Open();
SqlCommand cmd = new SqlCommand(从reportone选择Count(1),其中checkk = @fniish,con);
cmd.Parameters.AddWithValue(@ fniish,fni);
int rowsCount =(int)cmd.ExecuteScalar();
if(rowsCount> 0)
{
label12.Text =Complete;
}
其他
{
label12.Text =;
}


con.Close();

解决方案

引用:

当我输入数字时,标签中的完成在我更改记录数时不会改变



因为它没有进入 If条件块,你在标签中设置值。



试试这样

 public void reportCheck()
{
string fni =finish;
label12.Text =; //每次初始化值
SqlConnection con = new SqlConnection(Data Source = NAWAF; Initial Catalog = waterreport; Integrated Security = True);
con.Open();
SqlCommand cmd = new SqlCommand(selectone(*)from reportone where checkk = @fniish,con);
cmd.Parameters.AddWithValue(@ fniish,fni);
int rowsCount =(int)cmd.ExecuteScalar();
if(rowsCount> 0)
label12.Text =Complete; //根据条件设置值
con.Close();

}


你发出 select * 当你想要做的就是确定你是否在数据库中有匹配的记录?我要做的第一件事就是改为 SELECT COUNT(1)。然后,我会使用 ExecuteScalar [ ^ ]当您返回单个值时,您需要做的就是将结果计数转换为整数值并相应地更改标签。

i want when someone enter number that contain "finish" in database

show in label " Complete " and clear txtbox

i try this

but not working

when i enter number still "Complete" in label not change when i change number of record

What I have tried:

private void label13_TextChanged(object sender, EventArgs e)
        {
            


            SqlConnection con = new SqlConnection("Data Source=NAWAF;Initial Catalog=waterreport;Integrated Security=True");
            con.Open();
            SqlCommand luuud;
            string luud = " select * from reportonetmp WHERE no='" + label13.Text+ "'";
            luuud = new SqlCommand(luud,con);
           SqlDataReader reader = luuud.ExecuteReader();
           if (reader.Read())
           {
               textBox1.Text = reader["rec_report"].ToString();
               textBox2.Text = reader["rec_code"].ToString();
               textBox3.Text = reader["initial_date_repair"].ToString();
               dateTimePicker1.Text = reader["initial_hour_repair"].ToString();
               label9.Text = reader["customername"].ToString();
               label11.Text = reader["customerphone"].ToString();

           }




           reportCheck();




        }

        public void reportCheck()
        {
            string fni = "finish";
            label12.Text = "";
            SqlConnection con = new SqlConnection("Data Source=NAWAF;Initial Catalog=waterreport;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select Count(1) from reportone where checkk =   @fniish  ", con);
            cmd.Parameters.AddWithValue("@fniish", fni);
            int rowsCount = (int)cmd.ExecuteScalar();
            if(rowsCount > 0)
            {
                label12.Text = "Complete";
            }
            else
            {
                label12.Text ="";
            }


            con.Close();

解决方案

Quote:

when i enter number still "Complete" in label not change when i change number of record


because it doesnt goes inside the If condition block, where you are setting the value in the label.

try like this

public void reportCheck()
   {
       string fni = "finish";
       label12.Text = ""; // clear the value initially every time
       SqlConnection con = new SqlConnection("Data Source=NAWAF;Initial Catalog=waterreport;Integrated Security=True");
       con.Open();
       SqlCommand cmd = new SqlCommand("Select count(*) from reportone where checkk = @fniish", con);
       cmd.Parameters.AddWithValue("@fniish", fni);
       int rowsCount = (int)cmd.ExecuteScalar();
       if (rowsCount > 0)
           label12.Text = " Complete ";  // set the value based on condition
       con.Close();

   }


You're issuing a select * when all you want to do is identify whether or not you have matching record(s) in the database? First thing I would do is change that to SELECT COUNT(1) instead. Then, rather than issuing a data reader, I would use ExecuteScalar[^] as you're returning a single value, then all you need to do is convert the resulting count into an integer value and change the label accordingly.


这篇关于如果记录中有值显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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