943,724 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 19300
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 21st, 2008
0

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

Expand 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..

C# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shxrainz is offline Offline
14 posts
since Sep 2008
Sep 21st, 2008
0

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

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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Sep 22nd, 2008
0

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

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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shxrainz is offline Offline
14 posts
since Sep 2008
Sep 22nd, 2008
0

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

Create a boolean variable, thats a "flag" as its yes or no.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Sep 22nd, 2008
0

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

Click to Expand / Collapse  Quote originally posted by shxrainz ...
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..

C# Syntax (Toggle Plain Text)
  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,

C# Syntax (Toggle Plain Text)
  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.

C# Syntax (Toggle Plain Text)
  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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Sep 23rd, 2008
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shxrainz is offline Offline
14 posts
since Sep 2008
Sep 23rd, 2008
0

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

Click to Expand / Collapse  Quote originally posted by shxrainz ...
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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Sep 23rd, 2008
0

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

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");
}

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shxrainz is offline Offline
14 posts
since Sep 2008
Sep 23rd, 2008
0

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

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Sep 23rd, 2008
0

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

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.


C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 49
Posting Whiz
jireh is offline Offline
316 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Graph in 2D
Next Thread in C# Forum Timeline: SQL Server Connection how to's





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC