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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. need help

need help

Scheduled Pinned Locked Moved C#
helpdatabasetutorial
14 Posts 5 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.
  • M mjawadkhatri

    Hi, this is my coding -------------------------------------------------------------------- while (odr.Read()) { if (usr.Text == odr["userid"].ToString()) { MessageBox.Show("User Login", "Information", MessageBoxButtons.OK); } else { DialogResult drsave = MessageBox.Show("Login Fail", "Erorr", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); if (drsave == DialogResult.OK) { usr.Text = ""; pass.Text = ""; usr.Focus(); } else this.Close(); } } ------------------------------------------------------------------------ My program Build Successfully but when i type in textbox any string value my program read every value in my database in give every time message. for example in my database have 5 records my program read this records in give message 5 time :sigh: i want my program check all records in give one message record found or not please give me solution thanks in advanc jawad khatri i want to

    K Offline
    K Offline
    K0306
    wrote on last edited by
    #3

    Try the below sample code query = "select username from tablename where username = '" + usr.text + "'"; sqlcommand cmd = new sqlcommand(query,con); sqldatareader dr; dr=cmd.executereader(); while(dr.read()) { if(dr["username"].tostring() == usr.text) { true user } else { false user } } dr.close();

    Regards, Karthik K...

    1 Reply Last reply
    0
    • S SeMartens

      Hi, it seems that you want to check if a user exists or not. I would seperate the presentation (your dialog) from the dataaccess (the select). Try to implement it like this:

      private bool DoesUserExists(string sUserID) {
      ... perform a select statement here with a prepared statement
      ... then check if you have exactly one record
      bool bResult = odr.Read(); --> true if you have one record
      ... close everything
      return b;
      }

      public void CheckUser() {
      ... get the value from your textbox
      // call method to check if user exists
      if(this.DoesUserExists(sUserID)) {
      // show success dialog
      } else {
      // show failure dialog
      }
      }

      That would be a better approach than yours. By the way, it doesn't make any sense to check the userid in your database within the code, use the WHERE-clause of SELECT-statements for this. Hope this helps a bit. Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      M Offline
      M Offline
      mjawadkhatri
      wrote on last edited by
      #4

      Thank U For Reply Me bt How I perform Ur Coding In My Coding???? This is my All Coding In Button_click() --------------------------------------------------------------------------- using system; using Microsoft.data.odbc; private void login_Click(object sender, EventArgs e) { OdbcConnection cn; OdbcCommand cmd; OdbcDataReader odr; string MyString; MyString = "Select * from users"; cn = new OdbcConnection("Driver={Microsoft ODBC for Oracle};Server=orcl8i;UID=itehad;PWD=creative;"); cmd = new OdbcCommand(MyString, cn); cn.Open(); MessageBox.Show("Connected"); cmd.CommandText = "Select * from users"; odr = cmd.ExecuteReader(); while (odr.Read()) { if (usr.Text == odr["userid"].ToString()) { MessageBox.Show("User Login", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { DialogResult drsave = MessageBox.Show("Login Fail", "Erorr", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); if (drsave == DialogResult.OK) { usr.Text = ""; pass.Text = ""; usr.Focus(); } else this.Close(); } } } --------------------------------------------------------------------------- Thanks In Advace Jawad Khatri

      S 1 Reply Last reply
      0
      • S SeMartens

        Hi, it seems that you want to check if a user exists or not. I would seperate the presentation (your dialog) from the dataaccess (the select). Try to implement it like this:

        private bool DoesUserExists(string sUserID) {
        ... perform a select statement here with a prepared statement
        ... then check if you have exactly one record
        bool bResult = odr.Read(); --> true if you have one record
        ... close everything
        return b;
        }

        public void CheckUser() {
        ... get the value from your textbox
        // call method to check if user exists
        if(this.DoesUserExists(sUserID)) {
        // show success dialog
        } else {
        // show failure dialog
        }
        }

        That would be a better approach than yours. By the way, it doesn't make any sense to check the userid in your database within the code, use the WHERE-clause of SELECT-statements for this. Hope this helps a bit. Regards Sebastian

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #5

        SeMartens wrote:

        odr.Read(); --> true if you have one record

        ExecuteScalar will be more appropriate in this case than reader.

        Navaneeth How to use google | Ask smart questions

        S 1 Reply Last reply
        0
        • M mjawadkhatri

          Thank U For Reply Me bt How I perform Ur Coding In My Coding???? This is my All Coding In Button_click() --------------------------------------------------------------------------- using system; using Microsoft.data.odbc; private void login_Click(object sender, EventArgs e) { OdbcConnection cn; OdbcCommand cmd; OdbcDataReader odr; string MyString; MyString = "Select * from users"; cn = new OdbcConnection("Driver={Microsoft ODBC for Oracle};Server=orcl8i;UID=itehad;PWD=creative;"); cmd = new OdbcCommand(MyString, cn); cn.Open(); MessageBox.Show("Connected"); cmd.CommandText = "Select * from users"; odr = cmd.ExecuteReader(); while (odr.Read()) { if (usr.Text == odr["userid"].ToString()) { MessageBox.Show("User Login", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { DialogResult drsave = MessageBox.Show("Login Fail", "Erorr", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); if (drsave == DialogResult.OK) { usr.Text = ""; pass.Text = ""; usr.Focus(); } else this.Close(); } } } --------------------------------------------------------------------------- Thanks In Advace Jawad Khatri

          S Offline
          S Offline
          SeMartens
          wrote on last edited by
          #6

          Okay, normally you should write it by yourself, but today is friday, so here it comes:

          private bool DoesUserExists(string sUserID) {
          OdbcConnection cn;
          OdbcCommand cmd;
          OdbcParameter dbParameter;
          OdbcDataReader odr;

          string MyString = "Select * from users WHERE userid = @userid";

          cn = new OdbcConnection("Driver={Microsoft ODBC for Oracle};Server=orcl8i;UID=itehad;PWD=creative;");
          cn.Open();
          cmd = new OdbcCommand(MyString, cn);
          dbParameter = new OdbcParameter("userid", sUserID");
          cmd.Parameters.Add(dbParameter);
          odr = cmd.ExecuteReader();
          
          bool bResult = (odr.HasRows && odr.Read());
          odr.Close();
          cn.Close();
          return bResult;
          

          }

          private void login_Click(object sender, EventArgs e)
          {
          if(this.DoesUserExists(usr.Text)) {
          MessageBox.Show("User Login", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
          } else {
          DialogResult drsave = MessageBox.Show("Login Fail", "Erorr", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
          if (drsave == DialogResult.OK)
          {
          usr.Text = "";
          pass.Text = "";
          usr.Focus();
          } else {
          this.Close();
          }
          }
          }

          You should add fail-safe to your app by using try-catch blocks, especially while interacting with the database. Next step would be to seperate the dataccess into an own assembly. Regards Sebastian

          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

          M 1 Reply Last reply
          0
          • N N a v a n e e t h

            SeMartens wrote:

            odr.Read(); --> true if you have one record

            ExecuteScalar will be more appropriate in this case than reader.

            Navaneeth How to use google | Ask smart questions

            S Offline
            S Offline
            SeMartens
            wrote on last edited by
            #7

            Yep, you are right... but we want to confuse beginners, or? :)

            It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

            M 1 Reply Last reply
            0
            • M mjawadkhatri

              Hi, this is my coding -------------------------------------------------------------------- while (odr.Read()) { if (usr.Text == odr["userid"].ToString()) { MessageBox.Show("User Login", "Information", MessageBoxButtons.OK); } else { DialogResult drsave = MessageBox.Show("Login Fail", "Erorr", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); if (drsave == DialogResult.OK) { usr.Text = ""; pass.Text = ""; usr.Focus(); } else this.Close(); } } ------------------------------------------------------------------------ My program Build Successfully but when i type in textbox any string value my program read every value in my database in give every time message. for example in my database have 5 records my program read this records in give message 5 time :sigh: i want my program check all records in give one message record found or not please give me solution thanks in advanc jawad khatri i want to

              N Offline
              N Offline
              Nagy Vilmos
              wrote on last edited by
              #8

              Be careful about how you test for equality, in all but the rarest case you shoud use the Equals method.

              a == b

              This compares two objects and returns true if they are they same object, that is the same instance. This does not compare the content.

              a.Equals(b)

              Compares the two instances and is defined by each classes implimentation. In the trivial case of the string, it returns true if they have the same content. Any class you write should override this method if you need to compare instances' values.


              Panic, Chaos, Destruction. My work here is done.

              1 Reply Last reply
              0
              • S SeMartens

                Okay, normally you should write it by yourself, but today is friday, so here it comes:

                private bool DoesUserExists(string sUserID) {
                OdbcConnection cn;
                OdbcCommand cmd;
                OdbcParameter dbParameter;
                OdbcDataReader odr;

                string MyString = "Select * from users WHERE userid = @userid";

                cn = new OdbcConnection("Driver={Microsoft ODBC for Oracle};Server=orcl8i;UID=itehad;PWD=creative;");
                cn.Open();
                cmd = new OdbcCommand(MyString, cn);
                dbParameter = new OdbcParameter("userid", sUserID");
                cmd.Parameters.Add(dbParameter);
                odr = cmd.ExecuteReader();
                
                bool bResult = (odr.HasRows && odr.Read());
                odr.Close();
                cn.Close();
                return bResult;
                

                }

                private void login_Click(object sender, EventArgs e)
                {
                if(this.DoesUserExists(usr.Text)) {
                MessageBox.Show("User Login", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                } else {
                DialogResult drsave = MessageBox.Show("Login Fail", "Erorr", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                if (drsave == DialogResult.OK)
                {
                usr.Text = "";
                pass.Text = "";
                usr.Focus();
                } else {
                this.Close();
                }
                }
                }

                You should add fail-safe to your app by using try-catch blocks, especially while interacting with the database. Next step would be to seperate the dataccess into an own assembly. Regards Sebastian

                It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                M Offline
                M Offline
                mjawadkhatri
                wrote on last edited by
                #9

                Thanks Alot Sir for Help me Sir When I Run this program give error Error Is Error 1 'Microsoft.Data.Odbc.OdbcDataReader' does not contain a definition for 'HasRows'

                S 1 Reply Last reply
                0
                • S SeMartens

                  Yep, you are right... but we want to confuse beginners, or? :)

                  It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                  M Offline
                  M Offline
                  mjawadkhatri
                  wrote on last edited by
                  #10

                  sir Actually i am use dotnet after 1 year thats why i am facing lot of problem sir i kindly request to u please give me solution :sigh:

                  1 Reply Last reply
                  0
                  • M mjawadkhatri

                    Thanks Alot Sir for Help me Sir When I Run this program give error Error Is Error 1 'Microsoft.Data.Odbc.OdbcDataReader' does not contain a definition for 'HasRows'

                    S Offline
                    S Offline
                    SeMartens
                    wrote on last edited by
                    #11

                    Why don't you use the System.Data.Odbc.OdbcDataReader?

                    It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                    M 1 Reply Last reply
                    0
                    • S SeMartens

                      Why don't you use the System.Data.Odbc.OdbcDataReader?

                      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                      M Offline
                      M Offline
                      mjawadkhatri
                      wrote on last edited by
                      #12

                      ok. i use this and trying to solve my problem sorry for distrb u Sir:sigh:

                      S 1 Reply Last reply
                      0
                      • M mjawadkhatri

                        ok. i use this and trying to solve my problem sorry for distrb u Sir:sigh:

                        S Offline
                        S Offline
                        SeMartens
                        wrote on last edited by
                        #13

                        No problem. And please don't call me sir... :) Just give a feedback if your problem is solved. Regards Sebastian

                        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                        M 1 Reply Last reply
                        0
                        • S SeMartens

                          No problem. And please don't call me sir... :) Just give a feedback if your problem is solved. Regards Sebastian

                          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

                          M Offline
                          M Offline
                          mjawadkhatri
                          wrote on last edited by
                          #14

                          Thanks Again

                          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