Unable to view message if wrong data enter or null data enter

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 14
Reputation: shxrainz is an unknown quantity at this point 
Solved Threads: 0
shxrainz shxrainz is offline Offline
Newbie Poster

Unable to view message if wrong data enter or null data enter

 
0
  #1
Sep 21st, 2008
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..

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. String admId = textBox1.Text;
  4. String passw = textBox2.Text;
  5.  
  6. OleDbConnection conn = new OleDbConnection("provider = microsoft.jet.oledb.4.0;data source = dbStd.mdb;");
  7. try
  8. {
  9. conn.Open();
  10. }
  11. catch(Exception db)
  12. {
  13. MessageBox.Show("Error accessing the database: " + db.Message);
  14. }
  15.  
  16. OleDbCommand command = new OleDbCommand();
  17. command.Connection = conn;
  18. command.CommandText = "SELECT AdminId,Password FROM Admin WHERE AdminId ='"+admId+"'AND Password = '"+passw+"'";
  19.  
  20. OleDbDataReader datareader = command.ExecuteReader();
  21.  
  22.  
  23. while (datareader.Read()== true)
  24. {
  25.  
  26. string autAdmin = datareader.GetString(0);
  27. string autPass = datareader.GetString(1);
  28.  
  29. if (admId == autAdmin && passw == autPass)
  30. {
  31. MessageBox.Show("Welcome!");
  32.  
  33. }
  34. else if (admId == "" || passw == "")
  35. {
  36. MessageBox.Show("Try Again");
  37. }
  38.  
  39. }
  40. datareader.Close();
  41. conn.Close();
  42.  
  43.  
  44.  
  45. }
Last edited by cscgal; Sep 21st, 2008 at 2:47 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Unable to view message if wrong data enter or null data enter

 
0
  #2
Sep 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 14
Reputation: shxrainz is an unknown quantity at this point 
Solved Threads: 0
shxrainz shxrainz is offline Offline
Newbie Poster

Re: Unable to view message if wrong data enter or null data enter

 
0
  #3
Sep 22nd, 2008
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Unable to view message if wrong data enter or null data enter

 
0
  #4
Sep 22nd, 2008
Create a boolean variable, thats a "flag" as its yes or no.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Unable to view message if wrong data enter or null data enter

 
0
  #5
Sep 22nd, 2008
Originally Posted by shxrainz View Post
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..

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. String admId = textBox1.Text;
  4. String passw = textBox2.Text;
  5.  
  6. OleDbConnection conn = new OleDbConnection("provider = microsoft.jet.oledb.4.0;data source = dbStd.mdb;");
  7. try
  8. {
  9. conn.Open();
  10. }
  11. catch(Exception db)
  12. {
  13. MessageBox.Show("Error accessing the database: " + db.Message);
  14. }
  15.  
  16. OleDbCommand command = new OleDbCommand();
  17. command.Connection = conn;
  18. command.CommandText = "SELECT AdminId,Password FROM Admin WHERE AdminId ='"+admId+"'AND Password = '"+passw+"'";
  19.  
  20. OleDbDataReader datareader = command.ExecuteReader();
  21.  
  22.  
  23. while (datareader.Read()== true)
  24. {
  25.  
  26. string autAdmin = datareader.GetString(0);
  27. string autPass = datareader.GetString(1);
  28.  
  29. if (admId == autAdmin && passw == autPass)
  30. {
  31. MessageBox.Show("Welcome!");
  32.  
  33. }
  34. else if (admId == "" || passw == "")
  35. {
  36. MessageBox.Show("Try Again");
  37. }
  38.  
  39. }
  40. datareader.Close();
  41. conn.Close();
  42.  
  43.  
  44.  
  45. }
Hello,

  1. if (admId == autAdmin && passw == autPass)
  2. {
  3. MessageBox.Show("Welcome!");
  4.  
  5. }
  6. else if (admId == "" || passw == "")
  7. {
  8. MessageBox.Show("Try Again");
  9. }

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.

  1. if (admId == autAdmin && passw == autPass)
  2. {
  3. MessageBox.Show("Welcome!");
  4.  
  5. }
  6. else
  7. {
  8. MessageBox.Show("Try Again");
  9. }

try this one. working fine from my end.

Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 14
Reputation: shxrainz is an unknown quantity at this point 
Solved Threads: 0
shxrainz shxrainz is offline Offline
Newbie Poster

Re: Unable to view message if wrong data enter or null data enter

 
0
  #6
Sep 23rd, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Unable to view message if wrong data enter or null data enter

 
0
  #7
Sep 23rd, 2008
Originally Posted by shxrainz View Post
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,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 14
Reputation: shxrainz is an unknown quantity at this point 
Solved Threads: 0
shxrainz shxrainz is offline Offline
Newbie Poster

Re: Unable to view message if wrong data enter or null data enter

 
0
  #8
Sep 23rd, 2008
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");
}

}
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Unable to view message if wrong data enter or null data enter

 
0
  #9
Sep 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 300
Reputation: jireh is an unknown quantity at this point 
Solved Threads: 42
jireh's Avatar
jireh jireh is offline Offline
Posting Whiz

Re: Unable to view message if wrong data enter or null data enter

 
0
  #10
Sep 23rd, 2008
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.


  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. string admId = textBox1.Text;
  4. string passw = textBox2.Text;
  5. string strqry = string.Format(@"SELECT AdminId,Password
  6. FROM Admin
  7. WHERE AdminId ={0} AND
  8. Password = {1}", admId, passw);
  9.  
  10. OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data
  11. source=dbStd.mdb;");
  12. OleDbDataAdapater adapter = null;
  13. try
  14. {
  15. conn.Open();
  16. adapter = new OleDbDataAdapter(strqry, conn.ConnectionString);
  17. adapter.Fill(dt);
  18.  
  19. if (dt.Rows.Count >0)
  20. {
  21. string username = dt.Rows[0][0].ToString();
  22. string pw = dt.Rows[0][1].ToString();
  23.  
  24. if (username == admId && pw == passw)
  25. {
  26. MessageBox.Show("Welcome!");
  27. }
  28. else
  29. {
  30. MessageBox.Show("Access Denied.");
  31. }
  32. }
  33. else
  34. {
  35. MessageBox.Show("Access Denied.");
  36. }
  37.  
  38. }
  39. catch (Exception exc)
  40. {
  41. MessageBox.Show(exc.ToString());
  42. }
  43. finally
  44. {
  45. adapter.Dispose();
  46. adapter.Close();
  47. conn.Close();
  48. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC