Hi,
I have created a login form that has adminid and password..
I use visual studio 2005,database access.
login form work smoothly when i enter the correct adminid and password but never display an error message if wrong data was entered or no data was entered.
I need a help on this.
Along with this i show my coding.
Hope somebody will help me..
:)

private void button1_Click(object sender, EventArgs e)
        {
            String admId = textBox1.Text;
            String passw = textBox2.Text;

            OleDbConnection conn = new OleDbConnection("provider = microsoft.jet.oledb.4.0;data source = dbStd.mdb;");
            try
            {
                conn.Open();
            }
            catch(Exception db)
            {
                MessageBox.Show("Error accessing the database: " + db.Message);
            }

            OleDbCommand command = new OleDbCommand();
            command.Connection = conn;
            command.CommandText = "SELECT AdminId,Password FROM Admin WHERE AdminId ='"+admId+"'AND Password = '"+passw+"'";

            OleDbDataReader datareader = command.ExecuteReader();

            
                while (datareader.Read()== true)
                {
                       
                        string autAdmin = datareader.GetString(0);
                        string autPass = datareader.GetString(1);

                        if (admId == autAdmin && passw == autPass)
                        {
                            MessageBox.Show("Welcome!");
                             
                        }
                        else if (admId == "" || passw == "")
                        {
                            MessageBox.Show("Try Again");
                        }
                   
                }            
                datareader.Close();
                conn.Close();
                
            

        }

If you dont enter any it doesnt find a record in the db so no it wont show a message (debugging would show you this as the records returned would be none)

set a flag at the top of the routine to false
if you get a valid login, set it true, if its still false when you get to the bottom then they didnt have a valid id

Thank for replying my thread..
Actually i'm still new in c# and don't know how to use the flag..
really need your help..

Create a boolean variable, thats a "flag" as its yes or no.

Hi,
I have created a login form that has adminid and password..
I use visual studio 2005,database access.
login form work smoothly when i enter the correct adminid and password but never display an error message if wrong data was entered or no data was entered.
I need a help on this.
Along with this i show my coding.
Hope somebody will help me..
:)

private void button1_Click(object sender, EventArgs e)
        {
            String admId = textBox1.Text;
            String passw = textBox2.Text;

            OleDbConnection conn = new OleDbConnection("provider = microsoft.jet.oledb.4.0;data source = dbStd.mdb;");
            try
            {
                conn.Open();
            }
            catch(Exception db)
            {
                MessageBox.Show("Error accessing the database: " + db.Message);
            }

            OleDbCommand command = new OleDbCommand();
            command.Connection = conn;
            command.CommandText = "SELECT AdminId,Password FROM Admin WHERE AdminId ='"+admId+"'AND Password = '"+passw+"'";

            OleDbDataReader datareader = command.ExecuteReader();

            
                while (datareader.Read()== true)
                {
                       
                        string autAdmin = datareader.GetString(0);
                        string autPass = datareader.GetString(1);

                        if (admId == autAdmin && passw == autPass)
                        {
                            MessageBox.Show("Welcome!");
                             
                        }
                        else if (admId == "" || passw == "")
                        {
                            MessageBox.Show("Try Again");
                        }
                   
                }            
                datareader.Close();
                conn.Close();
                
            

        }

Hello,

if (admId == autAdmin && passw == autPass)
                        {
                            MessageBox.Show("Welcome!");
                             
                        }
                        else if (admId == "" || passw == "")
                        {
                            MessageBox.Show("Try Again");
                        }

this code means if you enter correct adminid and pass then it`ll show you welcome msg but it will show try again msg only if both fields are blank.

if (admId == autAdmin && passw == autPass)
                        {
                            MessageBox.Show("Welcome!");
                             
                        }
                        else
                        {
                            MessageBox.Show("Try Again");
                        }

try this one. working fine from my end.

Regards,

It still doesn't work..
The message for "try again" didn't pop up when no data enter or wrong data enter..:(

Can i have the syntax of Boolean flag?

It still doesn't work..
The message for "try again" didn't pop up when no data enter or wrong data enter..:(

Can i have the syntax of Boolean flag?

It works when correct details are entered ?

I checked it again and working fine with me.

Regards,

i already can view the "try again message box" with just editing the SELECT statement..
but then another problem arise..

the message box keep pop up until all the data in the database being checked..
i try to do switch statement..
but got an error..
in mydatabase,i already have 3 data..
and the message keep view for 3 times until it finish looping..

can anyone help me?

this is the current code i being used:

OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT AdminId,Password FROM Admin"; //WHERE AdminId = '"+admId+"' AND Password = '"+passw+"'";

            OleDbDataReader DtReader = cmd.ExecuteReader();



            while(DtReader.Read())
            {
                string autAdmin = DtReader.GetString(0);
                string autPass = DtReader.GetString(1);


                if(admId == autAdmin && passw == autPass)
                {
                    MessageBox.Show("Welcome");
                }
                else 
                {
                    MessageBox.Show("Try Again");
                }

            }

Firstly if you did a correct selection from the db, it would return 1 line that matched or 0, it shouldnt do it for all users in the db.

Secondly, if you created a flag to equate to "found" or "not found" you could then deal with it at the end. So even if you did have multiple entries loops through for some odd reason, you would only display one message.

try this one...
Don't limit the "try catch" the error will occur anytime so its best to put all your codes in a try catch block.

private void button1_Click(object sender, EventArgs e)
{
      string admId = textBox1.Text;
      string passw = textBox2.Text;
      string strqry = string.Format(@"SELECT AdminId,Password 
                                                    FROM    Admin 
                                                    WHERE  AdminId ={0} AND 
                                                                Password = {1}", admId,  passw);

      OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data  
                                                                             source=dbStd.mdb;");
      OleDbDataAdapater adapter = null;
      try
      {
             conn.Open();
             adapter = new OleDbDataAdapter(strqry, conn.ConnectionString);
             adapter.Fill(dt);

             if (dt.Rows.Count >0)    
             {
                  string username = dt.Rows[0][0].ToString();
                  string pw = dt.Rows[0][1].ToString();
                
                  if (username == admId && pw == passw)
                  {
                        MessageBox.Show("Welcome!");
                  }
                  else
                  {
                        MessageBox.Show("Access Denied.");
                  }
             }
             else
             {
                     MessageBox.Show("Access Denied.");
             }

       }
       catch (Exception exc)
       {
              MessageBox.Show(exc.ToString());
       }
       finally
       {
             adapter.Dispose();
             adapter.Close();
             conn.Close();
       }

One more thing... since this is about a log in thing... its expected that every username or adminID is unique therefore if will log-in you're selecting a specific user, thus, the query will return only one row on none.

So why do you need to loop? I think it's unnecessary.

Hope this would help.

regards...

:(
I still got an error

after i editing ur code by declaring a new dataTable object that you been missing then an error occurs:

Error   1   'System.Data.OleDb.OleDbDataAdapter' does not contain a definition for 'Close'

this is the coding part that i edited

  string admId = textBox1.Text;
  string passw = textBox2.Text;
  string strqry = string.Format(@"SELECT AdminId,Password 
                                                FROM    Admin 
                                                WHERE  AdminId ={0} AND 
                                                            Password = {1}", admId,  passw);

  OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=dbStd.mdb;");
  OleDbDataAdapter adapter = null;
  try
  {
         [B][COLOR="Red"][U][U]DataTable dt = new DataTable();[/U][/U][/COLOR][/B]
         conn.Open();
         adapter = new OleDbDataAdapter(strqry, conn.ConnectionString);
         adapter.Fill(dt);

         if (dt.Rows.Count >0)    
         {
              string username = dt.Rows[0][0].ToString();
              string pw = dt.Rows[0][1].ToString();

              if (username == admId && pw == passw)
              {
                    MessageBox.Show("Welcome!");
              }
              else
              {
                    MessageBox.Show("Access Denied.");
              }
         }
         else
         {
                 MessageBox.Show("Access Denied.");
         }

   }
   catch (Exception exc)
   {
          MessageBox.Show(exc.ToString());
   }
   finally
   {
         adapter.Dispose();
         adapter.Close();
         conn.Close();
   }

:)
thanks jireh...
i already solve the problem..
it just b'coz of no '{0}' and '{1}' in the coding..
It works!

Thank a lot..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.