Click here to Skip to main content
15,924,317 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
Posted
Updated 10-Jan-18 1:21am
v3
Comments
Maciej Los 10-Jan-18 6:17am    
"Not working" is not descriptive.
Member 13421231 10-Jan-18 6:21am    
i update my Q
Richard Deeming 10-Jan-18 12:53pm    
string luud = " select * from reportonetmp WHERE no='" + label13.Text+ "'";

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

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();

   }
 
Share this answer
 
Comments
Member 13421231 10-Jan-18 7:06am    
Same problem label still has "Complete" and not change when i enter another number
Karthik_Mahalingam 10-Jan-18 7:10am    
where you are entering, show your updated code.
Member 13421231 10-Jan-18 7:29am    
i update it now
Karthik_Mahalingam 10-Jan-18 7:35am    
run this query in your sql studio and check the count
Select count(*) from reportone where checkk = 'finish'
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900