Password Checking
-
Hi...im trying to do password checking for multiple users... the problem is that i get a runtime error saying the specified cast is invalid(line 272) ...i've check that im getting a string...so what could the problem be
private void btnOK_Click(object sender, System.EventArgs e) { dataSetPWD.Clear(); if(txtPassword.Text.Length > 10 && txtPassword.Text != "") { MessageBox.Show("The password textbox should contain more than 1 character and less than 10 characters. Please check your password.","Information"); } else { try { oleDbConnection1.Open(); oleDbDataAdapter1.SelectCommand.CommandText = "SELECT UserType FROM Password WHERE UserName = '"+txtUsername.Text+"' AND Password = '"+txtPassword.Text+"'"; oleDbDataAdapter1.Fill(dataSetPWD); System.Data.DataTable Password = dataSetPWD.Tables[0]; if(Password.Rows.Count != 0) { string s = (string) Password.Rows[0][0]; --> LINE 272 s.Trim(); s.ToString(); if(s.Equals("reception")) { frmReception rm = new frmReception(); rm.Show(); } else if(s.Equals("doctor")) { frmDiagnosis dm = new frmDiagnosis(); dm.Show(); } else if(s.Equals("pharmacy")) { frmPharMenu pm = new frmPharMenu(); pm.Show(); } /*if(s.Equals("cashier")) { frmCashier cm = new frmCashier(); cm.Show(); }*/ else { MessageBox.Show("Invalid Password or User Name. Please check your user name and password. It is case sensitive","Invalid Entry"); } } else { MessageBox.Show("No Such Record"); } } catch (Exception ErrorPassword) { MessageBox.Show(ErrorPassword.ToString(),"Password Error"); } finally { oleDbConnection1.Close(); oleDbConnection1.Dispose(); } } }
CODER -
Hi...im trying to do password checking for multiple users... the problem is that i get a runtime error saying the specified cast is invalid(line 272) ...i've check that im getting a string...so what could the problem be
private void btnOK_Click(object sender, System.EventArgs e) { dataSetPWD.Clear(); if(txtPassword.Text.Length > 10 && txtPassword.Text != "") { MessageBox.Show("The password textbox should contain more than 1 character and less than 10 characters. Please check your password.","Information"); } else { try { oleDbConnection1.Open(); oleDbDataAdapter1.SelectCommand.CommandText = "SELECT UserType FROM Password WHERE UserName = '"+txtUsername.Text+"' AND Password = '"+txtPassword.Text+"'"; oleDbDataAdapter1.Fill(dataSetPWD); System.Data.DataTable Password = dataSetPWD.Tables[0]; if(Password.Rows.Count != 0) { string s = (string) Password.Rows[0][0]; --> LINE 272 s.Trim(); s.ToString(); if(s.Equals("reception")) { frmReception rm = new frmReception(); rm.Show(); } else if(s.Equals("doctor")) { frmDiagnosis dm = new frmDiagnosis(); dm.Show(); } else if(s.Equals("pharmacy")) { frmPharMenu pm = new frmPharMenu(); pm.Show(); } /*if(s.Equals("cashier")) { frmCashier cm = new frmCashier(); cm.Show(); }*/ else { MessageBox.Show("Invalid Password or User Name. Please check your user name and password. It is case sensitive","Invalid Entry"); } } else { MessageBox.Show("No Such Record"); } } catch (Exception ErrorPassword) { MessageBox.Show(ErrorPassword.ToString(),"Password Error"); } finally { oleDbConnection1.Close(); oleDbConnection1.Dispose(); } } }
CODERYou could use .ToString() instead of (string) to see what you actually get. Could it be that Column[0] isn't the user type you expect? I think it would be more robust and easier to read if you used Password.Rows["UserType"][0] (or whatever your column's name is)... BTW, the first if clause is wrong :suss:. It won't get passwords that are too short. You should change && for ||. Regards, mav