المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : how to compare textbox string with data base field



C# Programming
10-13-2009, 01:10 AM
hi,
i doing one stanalone application in that i am crearing a login control in this i am checking the name and pssw which stored in the data base if the name and passw present in data base then user get login else error mssg
i tried some here but when i run my prog. it shows runtime exception that connection not open need help
here is my code


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private OleDbCommand cmd;
private void Form1_Load(object sender, EventArgs e)
{
String connectionString = "Provider= Microsoft.Jet.OLEDB.4.0; Data Source= C:\\Login1.mdb; username=xxx;password=xxx;database=login1";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();

string selectString =
"SELECT Name, Password " +
"FROM login1 " +
"WHERE Name = '" + login.Text + "' AND Password = '" + psw.Text + "'";

cmd = new OleDbCommand(selectString, connection);
connection.Open();
String strResult = String.Empty;
if (strResult.Length > 0)
{
label5.Text = "YOU ARE LOGGED IN!";
}

else
{
label5.Text = "INCORRECT USER/PASS!";
}
connection.Close();