C# .NET - match condition - Asked By Priyanka on 31-Aug-10 06:50 AM

private void button1_Click(object sender, EventArgs e)

{

OleDbConnection MyConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\db2.mdb");

try

{

MyConnection.Open();

OleDbDataReader rd;

string str = "SELECT name,bday FROM Table1 WHERE name='" + textBox1.Text + "' AND bday='" + textBox2.Text + "'";

OleDbCommand cmd = new OleDbCommand(str, MyConnection);

rd = cmd.ExecuteReader();

if (rd.Read())

{

MyConnection.Close();

Form2 form2 = new Form2();

form2.Show();

this.Hide();

}

else

{

MessageBox.Show("User Name or Password is incorrect");

MyConnection.Close();

}

}

catch

{

MyConnection.Close();

}

i am using the above code for login form with access database but the problem is it match all the records in the database but i want that it match only single column in database....
will i have to create another table for that or it can be done in that Table also...

Super Man replied to Priyanka on 31-Aug-10 07:45 AM

For that better to use like this:

protected void Button1_Click1(object sender, EventArgs e)

    {

      OleDbConnection MyConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\db2.mdb");

 

      try

      {

 

        MyConnection.Open();

 

       

 

        string str = "SELECT count(*) FROM Table1 WHERE name='" + textBox1.Text + "' AND bday='" + textBox2.Text + "'";

 

        OleDbCommand cmd = new OleDbCommand(str, MyConnection);

 

        int rd = cmd.ExecuteNonQuery();

 

        if (rd == 1 )

        {

 

          MyConnection.Close();

 

          Form2 form2 = new Form2();

 

          form2.Show();

 

          this.Hide();

 

        }

 

        else

        {

 

          MessageBox.Show("User Name or Password is incorrect");

 

          MyConnection.Close();

 

        }

 

      }

 

      catch

      {

 

        MyConnection.Close();

 

      }

 

    }

 

 

It will count the matched  records and then if it is 1 means then it will execute another code.

 

 

Hope you understand it…

Goniey N (Mr. G) replied to Priyanka on 31-Aug-10 08:52 AM

-- First Of All Change Your Connection String Like Below :

1.OleDbConnection MyConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:/db2.mdb");



-- If You Want To That It Will Watch only Single Column Then Just Pass One Column Name In Your Query Then It Will Search only That Single Column.

-- In Your Code You Pass "name" & "bday" In Query That's Why It Will Watch That Two Column In Your Database...

-- So Just Change Your Query Like Below :

1.string str = "SELECT name FROM Table1 WHERE name='" + textBox1.Text + "' AND bday='" + textBox2.Text + "'";


-- Here I Pass Only "name" Then It Will Show You Only Single Column Data Which Is "name" Depending on the Given Two Condition...
-- Instead of "name" You Can Pass Your Column Name Which You Want..


-- I Hope That Whatever I Understand Of Your Problem, It Is Correct & Given Answer Will Helpful To You...