| | |
Unable to view message if wrong data enter or null data enter
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
![]() |
•
•
Join Date: Sep 2008
Posts: 14
Reputation:
Solved Threads: 0
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..

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)
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(); }
Last edited by cscgal; Sep 21st, 2008 at 2:47 am. Reason: Added code tags
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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
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
•
•
•
•
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)
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(); }
C# Syntax (Toggle Plain Text)
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.
C# Syntax (Toggle Plain Text)
if (admId == autAdmin && passw == autPass) { MessageBox.Show("Welcome!"); } else { MessageBox.Show("Try Again"); }
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
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
Join Date: Sep 2008
Posts: 14
Reputation:
Solved Threads: 0
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");
}
}
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");
}
}
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
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.
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)
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(); }
A conclusion is the place where you got tired of thinking. http://www.martin2k.co.uk/forums/index.php?showforum=4
http://www.a1vbcode.com/a1vbcode/vbforums/Forum3-1.aspx
http://www.developerfusion.co.uk/for...orum&ForumID=4
![]() |
Other Threads in the C# Forum
- Previous Thread: Graph in 2D
- Next Thread: SQL Server Connection how to's
| Thread Tools | Search this Thread |
.net access algorithm animation array asp bitmap box c# check checkbox client column combobox control conversion csharp database datagrid datagridview datagridviewcheckbox dataset datetime degrees directrobot display draganddrop drawing encryption enum equation excel file form format formatting formbox forms formupdate function gdi+ hash image input install java leak linux list math mouseclick mp3 mysql namevaluepairs native networking operator packaging path photoshop picturebox pixelinversion post powerpacks print process programming radians regex remoting reporting richtextbox robot safari server sleep snooze socket sql statistics string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio wait webbrowser wfa winforms wpf xml






