Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. smart device Forms-Database

smart device Forms-Database

Scheduled Pinned Locked Moved C#
helpcsharpdatabasecomquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tunisien86
    wrote on last edited by
    #1

    Hi, I develop a Csharp smart device application.I was in the first step.I create a ConnexionForm where user writes his login and password,the system verifys if they are in the database .sdf.I write this code:

    private void button1_Click(object sender, EventArgs e)
    {
    /*string req = "select Login, MotPasse from Connexion";
    DataTable tab = new DataTable();
    SqlCeCommand com = new SqlCeCommand(req, sqlceconn);
    SqlCeDataAdapter ada = new SqlCeDataAdapter(com);
    ada.Fill(tab);*/
    string s1 = textBox1.Text.ToString();
    string s2 = textBox2.Text.ToString();
    BaseGmaoLocaleDataSet2 dat = new BaseGmaoLocaleDataSet2();
    DataRow[] foundRows;
    foundRows = dat.Tables["Connexion"].Select("Login like s1 and MotPasse like s2");

    if (foundRows != null)
    {
    MessageBox.Show("Authentification réussie");
    MenuP m = new MenuP();
    m.Show();
    }
    else
    {
    MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
    }
    }}}

    But an error appears to me telling me that there is an error in the form of the 2 strings s1 et S2 Can you help me? Thanks for all u suggestions

    T L O 3 Replies Last reply
    0
    • T Tunisien86

      Hi, I develop a Csharp smart device application.I was in the first step.I create a ConnexionForm where user writes his login and password,the system verifys if they are in the database .sdf.I write this code:

      private void button1_Click(object sender, EventArgs e)
      {
      /*string req = "select Login, MotPasse from Connexion";
      DataTable tab = new DataTable();
      SqlCeCommand com = new SqlCeCommand(req, sqlceconn);
      SqlCeDataAdapter ada = new SqlCeDataAdapter(com);
      ada.Fill(tab);*/
      string s1 = textBox1.Text.ToString();
      string s2 = textBox2.Text.ToString();
      BaseGmaoLocaleDataSet2 dat = new BaseGmaoLocaleDataSet2();
      DataRow[] foundRows;
      foundRows = dat.Tables["Connexion"].Select("Login like s1 and MotPasse like s2");

      if (foundRows != null)
      {
      MessageBox.Show("Authentification réussie");
      MenuP m = new MenuP();
      m.Show();
      }
      else
      {
      MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
      }
      }}}

      But an error appears to me telling me that there is an error in the form of the 2 strings s1 et S2 Can you help me? Thanks for all u suggestions

      T Offline
      T Offline
      Tim Yen
      wrote on last edited by
      #2

      I would try putting the s1 and s2 into single quotes and maybe using wild cards (%)

      foundRows = dat.Tables["Connexion"].Select("Login like '%" + s1 + "%' and MotPasse like '%" + s2 +"%'");

      Lucs answer is better. See that.

      modified on Tuesday, April 13, 2010 9:06 AM

      L T 2 Replies Last reply
      0
      • T Tunisien86

        Hi, I develop a Csharp smart device application.I was in the first step.I create a ConnexionForm where user writes his login and password,the system verifys if they are in the database .sdf.I write this code:

        private void button1_Click(object sender, EventArgs e)
        {
        /*string req = "select Login, MotPasse from Connexion";
        DataTable tab = new DataTable();
        SqlCeCommand com = new SqlCeCommand(req, sqlceconn);
        SqlCeDataAdapter ada = new SqlCeDataAdapter(com);
        ada.Fill(tab);*/
        string s1 = textBox1.Text.ToString();
        string s2 = textBox2.Text.ToString();
        BaseGmaoLocaleDataSet2 dat = new BaseGmaoLocaleDataSet2();
        DataRow[] foundRows;
        foundRows = dat.Tables["Connexion"].Select("Login like s1 and MotPasse like s2");

        if (foundRows != null)
        {
        MessageBox.Show("Authentification réussie");
        MenuP m = new MenuP();
        m.Show();
        }
        else
        {
        MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
        }
        }}}

        But an error appears to me telling me that there is an error in the form of the 2 strings s1 et S2 Can you help me? Thanks for all u suggestions

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, several comments: 1. your database does not know what s1 and s2 mean in Select("Login like s1 and MotPasse like s2"); you want the content of s1 and s2, not the names of the variables; so they must not be inside double quotes. and then you want SQL to see them as string literals, which requires single quotes. So at least you should change it to

        ...Select("Login like '"+s1+"' and MotPasse like '"+s2+"'");

        2. it does not make sense to use like like that, there are no wildcards, nor anything special. So better write

        ...Select("Login = '"+s1+"' and MotPasse = '"+s2+"'");

        3. you should not pass user input straight to an SQL statement, it makes your app very vulnerable; the user could type things that end up your SQL statement do things you don't want such as delete a table. Either check your inputs (you must avoid empty fields too!) or use parameterized SQL (use SQLParameter). 4. You should not store plain passwords in a database; you should use encryption or hashing. Read up on best practices for passwords! :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Prolific encyclopedia fixture proof-reader browser patron addict?
        We all depend on the beast below.


        1 Reply Last reply
        0
        • T Tim Yen

          I would try putting the s1 and s2 into single quotes and maybe using wild cards (%)

          foundRows = dat.Tables["Connexion"].Select("Login like '%" + s1 + "%' and MotPasse like '%" + s2 +"%'");

          Lucs answer is better. See that.

          modified on Tuesday, April 13, 2010 9:06 AM

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          it should work, however I don't think wildcards are a good idea for authentication. Chances are entering

          username: a
          password: a

          will let you in, as it would match Jan/MySecretPassword as well as an infinite number of other possible accounts. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Prolific encyclopedia fixture proof-reader browser patron addict?
          We all depend on the beast below.


          1 Reply Last reply
          0
          • T Tunisien86

            Hi, I develop a Csharp smart device application.I was in the first step.I create a ConnexionForm where user writes his login and password,the system verifys if they are in the database .sdf.I write this code:

            private void button1_Click(object sender, EventArgs e)
            {
            /*string req = "select Login, MotPasse from Connexion";
            DataTable tab = new DataTable();
            SqlCeCommand com = new SqlCeCommand(req, sqlceconn);
            SqlCeDataAdapter ada = new SqlCeDataAdapter(com);
            ada.Fill(tab);*/
            string s1 = textBox1.Text.ToString();
            string s2 = textBox2.Text.ToString();
            BaseGmaoLocaleDataSet2 dat = new BaseGmaoLocaleDataSet2();
            DataRow[] foundRows;
            foundRows = dat.Tables["Connexion"].Select("Login like s1 and MotPasse like s2");

            if (foundRows != null)
            {
            MessageBox.Show("Authentification réussie");
            MenuP m = new MenuP();
            m.Show();
            }
            else
            {
            MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
            }
            }}}

            But an error appears to me telling me that there is an error in the form of the 2 strings s1 et S2 Can you help me? Thanks for all u suggestions

            O Offline
            O Offline
            O Phil
            wrote on last edited by
            #5

            string s1 = textBox1.Text.ToString();
            string s2 = textBox2.Text.ToString();

            Text property is already a string, so you don't need the ToString() method here. So :

            string s1 = textBox1.Text;
            string s2 = textBox2.Text;

            is sufficient.

            SqlCeConnection cnx = new SqlCeConnection("..."); // Replace ... by the right connexion string
            string req = "SELECT * FROM Connexion WHERE Login = @login AND MotPasse = @pass";
            SqlCeCommand cmd = new SqlCeCommand(req, cnx);
            SqlCeParameter login = new SqlCeParameter("login", s1);
            cmd.Parameters.Add(login);
            SqlCeParameter pass = new SqlCeParameter("pass", s2);
            cmd.Parameters.Add(pass);
            ...

            Here's the general idea. Hope it'll be useful.

            modified on Tuesday, April 13, 2010 10:54 AM

            L T 2 Replies Last reply
            0
            • T Tim Yen

              I would try putting the s1 and s2 into single quotes and maybe using wild cards (%)

              foundRows = dat.Tables["Connexion"].Select("Login like '%" + s1 + "%' and MotPasse like '%" + s2 +"%'");

              Lucs answer is better. See that.

              modified on Tuesday, April 13, 2010 9:06 AM

              T Offline
              T Offline
              Tunisien86
              wrote on last edited by
              #6

              Hi, Thank u Lucs,the error desappears but a new problem appears: althougth the login and password are not in my database .sdf,the system returns"successful authentification". What should I add to correct this?? Thanks a lot for u contribution

              1 Reply Last reply
              0
              • O O Phil

                string s1 = textBox1.Text.ToString();
                string s2 = textBox2.Text.ToString();

                Text property is already a string, so you don't need the ToString() method here. So :

                string s1 = textBox1.Text;
                string s2 = textBox2.Text;

                is sufficient.

                SqlCeConnection cnx = new SqlCeConnection("..."); // Replace ... by the right connexion string
                string req = "SELECT * FROM Connexion WHERE Login = @login AND MotPasse = @pass";
                SqlCeCommand cmd = new SqlCeCommand(req, cnx);
                SqlCeParameter login = new SqlCeParameter("login", s1);
                cmd.Parameters.Add(login);
                SqlCeParameter pass = new SqlCeParameter("pass", s2);
                cmd.Parameters.Add(pass);
                ...

                Here's the general idea. Hope it'll be useful.

                modified on Tuesday, April 13, 2010 10:54 AM

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                O.Phil wrote:

                textBox1.Text.ToString()

                or textBox1.Text.ToString().ToString() :confused:

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                Prolific encyclopedia fixture proof-reader browser patron addict?
                We all depend on the beast below.


                O 1 Reply Last reply
                0
                • L Luc Pattyn

                  O.Phil wrote:

                  textBox1.Text.ToString()

                  or textBox1.Text.ToString().ToString() :confused:

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Prolific encyclopedia fixture proof-reader browser patron addict?
                  We all depend on the beast below.


                  O Offline
                  O Offline
                  O Phil
                  wrote on last edited by
                  #8

                  I was just observing that "Text property is already a string, so you don't need the ToString() method here." ;)

                  L 1 Reply Last reply
                  0
                  • O O Phil

                    I was just observing that "Text property is already a string, so you don't need the ToString() method here." ;)

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    right. And a good thing about TextBox is it's Text property never returns null. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    Prolific encyclopedia fixture proof-reader browser patron addict?
                    We all depend on the beast below.


                    1 Reply Last reply
                    0
                    • O O Phil

                      string s1 = textBox1.Text.ToString();
                      string s2 = textBox2.Text.ToString();

                      Text property is already a string, so you don't need the ToString() method here. So :

                      string s1 = textBox1.Text;
                      string s2 = textBox2.Text;

                      is sufficient.

                      SqlCeConnection cnx = new SqlCeConnection("..."); // Replace ... by the right connexion string
                      string req = "SELECT * FROM Connexion WHERE Login = @login AND MotPasse = @pass";
                      SqlCeCommand cmd = new SqlCeCommand(req, cnx);
                      SqlCeParameter login = new SqlCeParameter("login", s1);
                      cmd.Parameters.Add(login);
                      SqlCeParameter pass = new SqlCeParameter("pass", s2);
                      cmd.Parameters.Add(pass);
                      ...

                      Here's the general idea. Hope it'll be useful.

                      modified on Tuesday, April 13, 2010 10:54 AM

                      T Offline
                      T Offline
                      Tunisien86
                      wrote on last edited by
                      #10

                      Hi, I add what u tell me about but still the same problem.

                      private void button1_Click(object sender, EventArgs e)
                      {
                      /*string req = "select Login, MotPasse from Connexion";
                      DataTable tab = new DataTable();
                      SqlCeCommand com = new SqlCeCommand(req, sqlceconn);
                      SqlCeDataAdapter ada = new SqlCeDataAdapter(com);
                      ada.Fill(tab);*/
                      string s1 = textBox1.Text;
                      string s2 = textBox2.Text;
                      SqlCeConnection cnx = new SqlCeConnection();
                      string wCS = String.Format("DATA SOURCE = '{0}';", Program.gFichierBase);
                      cnx.ConnectionString = wCS;
                      string req = "SELECT * FROM Connexion WHERE Login = @login AND MotPasse = @pass";
                      SqlCeCommand cmd = new SqlCeCommand(req, cnx);
                      SqlCeParameter login = new SqlCeParameter("login", s1);
                      cmd.Parameters.Add(login);
                      SqlCeParameter pass = new SqlCeParameter("pass", s2);
                      cmd.Parameters.Add(pass);
                      BaseGmaoLocaleDataSet2 dat = new BaseGmaoLocaleDataSet2();
                      DataRow[] foundRows;
                      foundRows = dat.Tables["Connexion"].Select("Login like '%" + s1 + "%' and MotPasse like '%" + s2 + "%'");
                      if (foundRows != null)
                      {
                      MessageBox.Show("Authentification réussie");
                      MenuP m = new MenuP();
                      m.Show();
                      }
                      else
                      {
                      MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
                      }
                      }

                      It seems so difficult :) Thanks for u contribution

                      O 1 Reply Last reply
                      0
                      • T Tunisien86

                        Hi, I add what u tell me about but still the same problem.

                        private void button1_Click(object sender, EventArgs e)
                        {
                        /*string req = "select Login, MotPasse from Connexion";
                        DataTable tab = new DataTable();
                        SqlCeCommand com = new SqlCeCommand(req, sqlceconn);
                        SqlCeDataAdapter ada = new SqlCeDataAdapter(com);
                        ada.Fill(tab);*/
                        string s1 = textBox1.Text;
                        string s2 = textBox2.Text;
                        SqlCeConnection cnx = new SqlCeConnection();
                        string wCS = String.Format("DATA SOURCE = '{0}';", Program.gFichierBase);
                        cnx.ConnectionString = wCS;
                        string req = "SELECT * FROM Connexion WHERE Login = @login AND MotPasse = @pass";
                        SqlCeCommand cmd = new SqlCeCommand(req, cnx);
                        SqlCeParameter login = new SqlCeParameter("login", s1);
                        cmd.Parameters.Add(login);
                        SqlCeParameter pass = new SqlCeParameter("pass", s2);
                        cmd.Parameters.Add(pass);
                        BaseGmaoLocaleDataSet2 dat = new BaseGmaoLocaleDataSet2();
                        DataRow[] foundRows;
                        foundRows = dat.Tables["Connexion"].Select("Login like '%" + s1 + "%' and MotPasse like '%" + s2 + "%'");
                        if (foundRows != null)
                        {
                        MessageBox.Show("Authentification réussie");
                        MenuP m = new MenuP();
                        m.Show();
                        }
                        else
                        {
                        MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");
                        }
                        }

                        It seems so difficult :) Thanks for u contribution

                        O Offline
                        O Offline
                        O Phil
                        wrote on last edited by
                        #11

                        Hi, When you use the SqlCe Connection and Command object, you don't need to use your dataset anymore. If you have MSDN installed, you can check the use of these objects (particularly the SqlCeCommand object, since this is the one that actually does the request). So :

                        {...}
                        cmd.Parameters.Add(pass);
                        int count = cmd.ExecuteScalar();
                        if (count == 1)
                        {
                        MessageBox.Show("Authentification réussie");
                        MenuP m = new MenuP();
                        m.Show();
                        }
                        else
                        MessageBox.Show("Login ou mot de passe incorrect veuillez réessayer");

                        Here's the idea. Courage ! ;)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups