Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Helloo,

i confused into display a error message when comapre the textbox data from the ArrayList.

i have 100 data into Arraylist. and i compare the textbox value with condition like :

id_user is a name of Arraylist.

for (int k = 0; k < id_user.Count; k++)
{
       if (id_user[k].Equals(txt_uname.Text))
       {
             string username = "select ID,password from empl where id='" + txt_uname.Text + "' and password='" + txt_pass.Text + "'";
             SqlDataAdapter ad = new SqlDataAdapter(username, c.getcon());
             DataSet ds = new DataSet();
             ad.Fill(ds);
             c.close();
             if (ds.Tables[0].Rows.Count != 0)
             {
                    // do something
             }
             else
             {
                   Label4.Text = "Invalid Username or Password";
             }
       }
       else
       {
          Label4.Text = "You Are Not Authorized User";
       }

}


so this is a problem:
if a arraylist have 5 records and match the condition on 1st record but not enter proper id and password it also display the else message because it continue to check
the remaning 4 data and execute the error message of that last record.

so i want to do that if it match the condition and not enter proper id and password it display first error message " invalid ...." else another message if not match from the entire Arraylist.

Please help me....
Thanks in advance....
Mitesh
Posted
Updated 5-Jul-12 21:20pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Jul-12 3:24am    
This is not a question. Why cannot you do exactly what you describe in this post?
--SA
Sergey Alexandrovich Kryukov 6-Jul-12 3:25am    
By the way, don't use obsolete ArrayList, use System.Collections.Generic.List<>.
--SA
[no name] 6-Jul-12 3:25am    
i tried but not success, so then after i want to help from you.
Sergey Alexandrovich Kryukov 6-Jul-12 3:28am    
"Not success" is not descriptive; we cannot work out any solution from that.
Also, do not use string concatenation, use parametrized commands. What you do is opening the wide door the the SQL Injection (read about this exploit in Wikipedia).
--SA
[no name] 6-Jul-12 3:31am    
ok. thanks for suggestion but how t stop that arraylist selection.

I think you need a variable authState (or similar) which is set when authentication is succeeded/failed.
Just set the value within the for loop and then check the variable's value after. Notice the break statement which terminates the loop.

Example:
// 0 for not authorized
// 1 for invalid user/pass
// 2 for success
// NOTE: this could also be enum
int authState = 0;
for (int k = 0; k < id_user.Count; k++)
{
       if (id_user[k].Equals(txt_uname.Text))
       {
             string username = "select ID,password from empl where id='" + txt_uname.Text + "' and password='" + txt_pass.Text + "'";
             SqlDataAdapter ad = new SqlDataAdapter(username, c.getcon());
             DataSet ds = new DataSet();
             ad.Fill(ds);
             c.close();
             if (ds.Tables[0].Rows.Count != 0)
             {
                    // do something
                   authState = 2;
                   break;
             }
             else
             {
                   authState = 1;
                   break;
             }
       }
       // this branch is not needed because 0 is default value for authState
       else
       {
          authState = 0;
       }
 
}

// Check the result
switch(authState)
{
   case 0:
      Label4.Text = "You Are Not Authorized User";
      break;
   case 1:
      Label4.Text = "Invalid Username or Password";
      break;
   case 2:
      Label4.Text = "Success".
      break;
}
 
Share this answer
 
v2
Comments
[no name] 6-Jul-12 4:34am    
thanks brother, it works fine and what i say to you for solving my problem.... Thanks a Lot and of course my 5++++++
you can put "Invalid Username or Password" this message in a different variable as messege and display final message after comparing Label4.text and message, if once for loop satisfies
condition if (id_user[k].Equals(txt_uname.Text))then name is in arraylist and if it is in database then do smething show appropriate message in Label4 (if needed) and break the loop.

Please try this.

id_user is a name of Arraylist.
C#
string messege="";
for (int k = 0; k < id_user.Count; k++)
{
       if (id_user[k].Equals(txt_uname.Text))
       {
             string username = "select ID,password from empl where id='" + txt_uname.Text + "' and password='" + txt_pass.Text + "'";
             SqlDataAdapter ad = new SqlDataAdapter(username, c.getcon());
             DataSet ds = new DataSet();
             ad.Fill(ds);
             c.close();
             if (ds.Tables[0].Rows.Count != 0)
             {
                    // do something
Label4.Text ="valid user";
break;
             }
             else
             {
                  messege = "Invalid Username or Password";
             }
       }
       else
       {
          Label4.Text = messege==""?"You Are Not Authorized User":messege;
       }
 
}
 
Share this answer
 
v2

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