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. Web Development
  3. ASP.NET
  4. Plz find out error in my code.

Plz find out error in my code.

Scheduled Pinned Locked Moved ASP.NET
csharphelpasp-netcareer
10 Posts 7 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.
  • C Offline
    C Offline
    Care Career
    wrote on last edited by
    #1

    Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER

    C R M D D 6 Replies Last reply
    0
    • C Care Career

      Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Care Career wrote:

      This code is not working

      That is vague. What do you mean by "not working"?


      Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      1 Reply Last reply
      0
      • C Care Career

        Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER

        R Offline
        R Offline
        Russell Jones
        wrote on last edited by
        #3

        I'd probably use something like if ((ds.Tables["CreateAccount"].Select(string.format("User_Name = '{0}' AND Password = '{1}'", new object []{txtUser.Text,txtPassword.text})).length ==1) //login message else //failed message except i wouldn't process the message like that, i'd do a query such as: Select count(*) from users where User_Name = AND Password = and check that the return value was 1. The danger would be that someone will pick up your asp.net login function and deploy it in winforms where the client would have a list of all the username / password entries in the database. Russell

        1 Reply Last reply
        0
        • C Care Career

          Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER

          M Offline
          M Offline
          Md Ismail
          wrote on last edited by
          #4

          The code will produce output for all row of the table. So user never understand that he successfully logged in or not.

          1 Reply Last reply
          0
          • C Care Career

            Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER

            D Offline
            D Offline
            Dave Sexton
            wrote on last edited by
            #5

            Well firstly you can't apply indexing with [] to a DataTable - it should be more like this (note bold text)

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
            if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"]) &&
            (txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"]))
            {
            Response.Write("You have succesfully logged in");
            }
            else
            {
            Response.Write("User Name Does Not Exist");
            }
            }

            Also - I'm no C# guru but I find using the curly braces helps improve readability - one of the experts can correct me if I'm wrong but I don't see any harm in using curly braces in your "if" statement in this context. Secondly, how do you know that the username doesn't exist if the password is wrong? I'd suggest either breaking it down into 2 separate helper functions to determine where the problem is or (easier) changing the response to "Either the username or password is incorrect". Thirdly, you are passing the password as plaintext, which leads me to assume you're saving the password as plain text - bad idea. Rather use a hash to encrypt the password before processing or saving. Take a look at this[^]

            D 1 Reply Last reply
            0
            • D Dave Sexton

              Well firstly you can't apply indexing with [] to a DataTable - it should be more like this (note bold text)

              for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
              {
              if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"]) &&
              (txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"]))
              {
              Response.Write("You have succesfully logged in");
              }
              else
              {
              Response.Write("User Name Does Not Exist");
              }
              }

              Also - I'm no C# guru but I find using the curly braces helps improve readability - one of the experts can correct me if I'm wrong but I don't see any harm in using curly braces in your "if" statement in this context. Secondly, how do you know that the username doesn't exist if the password is wrong? I'd suggest either breaking it down into 2 separate helper functions to determine where the problem is or (easier) changing the response to "Either the username or password is incorrect". Thirdly, you are passing the password as plaintext, which leads me to assume you're saving the password as plain text - bad idea. Rather use a hash to encrypt the password before processing or saving. Take a look at this[^]

              D Offline
              D Offline
              DeepToot
              wrote on last edited by
              #6

              Have you tried putting a .ToString() at the end of the DataSet? for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"].ToString()) && (txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"].ToString())) { Response.Write("You have succesfully logged in"); } else { Response.Write("User Name Does Not Exist"); } } Steve Welborn Software Engineer BitWise Solutions

              D D 2 Replies Last reply
              0
              • D DeepToot

                Have you tried putting a .ToString() at the end of the DataSet? for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"].ToString()) && (txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"].ToString())) { Response.Write("You have succesfully logged in"); } else { Response.Write("User Name Does Not Exist"); } } Steve Welborn Software Engineer BitWise Solutions

                D Offline
                D Offline
                DeepToot
                wrote on last edited by
                #7

                And to add to that in your for loop, its zero based..you need to make it like this: for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++) { }

                Steve Welborn Software Engineer BitWise Solutions

                1 Reply Last reply
                0
                • C Care Career

                  Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER

                  D Offline
                  D Offline
                  DeepToot
                  wrote on last edited by
                  #8

                  ismailiiuc is right. This will loop through the whole DataSet. what you should do is take it out of a loop all together. Its not needed. There should only be one username and password in there. after you fill your DataSet just do this: if (txtUser.Text == ds.Tables["CreateAccount"][0]["User_Name"].ToString() && txtPassword.Text == ds.Tables["CreateAccount"][0]["Password"].ToString()) { Response.Write("You have succesfully logged in"); } else { Response.Write("User Name Does Not Exist"); }

                  Steve Welborn Software Engineer BitWise Solutions

                  1 Reply Last reply
                  0
                  • C Care Career

                    Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER

                    S Offline
                    S Offline
                    Sujit Gupta
                    wrote on last edited by
                    #9

                    use this to find data from table ds.Tables["CreateAccount"].rows[i]["User_Name"].tostring(); or ds.Tables[0].rows[i]["User_Name"].tostring();

                    Sujit

                    1 Reply Last reply
                    0
                    • D DeepToot

                      Have you tried putting a .ToString() at the end of the DataSet? for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"].ToString()) && (txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"].ToString())) { Response.Write("You have succesfully logged in"); } else { Response.Write("User Name Does Not Exist"); } } Steve Welborn Software Engineer BitWise Solutions

                      D Offline
                      D Offline
                      Dave Sexton
                      wrote on last edited by
                      #10

                      I did it "off-the-cuff" & missed that one (oops! & thanks), what did you think of the rest or the advice?

                      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