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. Login problem with ExecuteScalar();

Login problem with ExecuteScalar();

Scheduled Pinned Locked Moved ASP.NET
helpcomquestion
12 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.
  • M Offline
    M Offline
    Maxdd 7
    wrote on last edited by
    #1

    My users table is that:

    CREATE TABLE Users(
    ID_Users INT IDENTITY(1,1),
    Username VARCHAR(30) UNIQUE,
    Password VARCHAR(100) DEFAULT 'password',
    Nivel tinyint DEFAULT '3',
    PRIMARY KEY(ID_Users));

    When users give username and password executes the following code:

    string sqlUserName;
    sqlUserName = "SELECT ID_Users, Username,Password,Nivel FROM Users ";
    sqlUserName += " WHERE (Username ='" + UserName + "')";
    sqlUserName += " AND (Password ='" + Password + "')";
    SqlCommand com = new SqlCommand(sqlUserName, Conn);

    string existe;
    existe = (string)com.ExecuteScalar();

    if (existe != null)
    (...)

    The problem is, I added five minutes ago "SELECT ID_Users". Now I have the error: "unable to cast int32 to string". I dont understand, because I use sucessfuly tinyint and there is no problem. Ok, its not a int32, but in the end its not the same thing? What should I do ? EDIT: looks like if I do: object existe; existe = com.ExecuteScalar(); There's no error. Can I use it with no problem's ?

    A N C 3 Replies Last reply
    0
    • M Maxdd 7

      My users table is that:

      CREATE TABLE Users(
      ID_Users INT IDENTITY(1,1),
      Username VARCHAR(30) UNIQUE,
      Password VARCHAR(100) DEFAULT 'password',
      Nivel tinyint DEFAULT '3',
      PRIMARY KEY(ID_Users));

      When users give username and password executes the following code:

      string sqlUserName;
      sqlUserName = "SELECT ID_Users, Username,Password,Nivel FROM Users ";
      sqlUserName += " WHERE (Username ='" + UserName + "')";
      sqlUserName += " AND (Password ='" + Password + "')";
      SqlCommand com = new SqlCommand(sqlUserName, Conn);

      string existe;
      existe = (string)com.ExecuteScalar();

      if (existe != null)
      (...)

      The problem is, I added five minutes ago "SELECT ID_Users". Now I have the error: "unable to cast int32 to string". I dont understand, because I use sucessfuly tinyint and there is no problem. Ok, its not a int32, but in the end its not the same thing? What should I do ? EDIT: looks like if I do: object existe; existe = com.ExecuteScalar(); There's no error. Can I use it with no problem's ?

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      To make you understand, I guess ExecuteScalar executes a query and returns only the first column of the first row. All others are just ignored. So why do you select them ?? Anyways, ExecuteScalar returns an object. So you might use string existe = com.ExecuteScalar().ToString(); But let me remind you, if there is no row selected, this line will through an error. So rather check the object first.

      object result = com.ExecuteScalar();
      if(result != null)
      string existe = result.ToString();

      Hope this is fine. Or you can also check Count(*) to determine if anyone is selected or not in the query. :thumbsup:

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      My Latest Articles-->** Simplify Code Using NDepend
      Basics of Bing Search API using .NET
      Microsoft Bing MAP using Javascript

      N M 2 Replies Last reply
      0
      • A Abhishek Sur

        To make you understand, I guess ExecuteScalar executes a query and returns only the first column of the first row. All others are just ignored. So why do you select them ?? Anyways, ExecuteScalar returns an object. So you might use string existe = com.ExecuteScalar().ToString(); But let me remind you, if there is no row selected, this line will through an error. So rather check the object first.

        object result = com.ExecuteScalar();
        if(result != null)
        string existe = result.ToString();

        Hope this is fine. Or you can also check Count(*) to determine if anyone is selected or not in the query. :thumbsup:

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        My Latest Articles-->** Simplify Code Using NDepend
        Basics of Bing Search API using .NET
        Microsoft Bing MAP using Javascript

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        Why are you trying to use a string as a boolean? The more acceptable way to handle this would...

        SELECT CASE COUNT(*) WHEN 0 THEN 0 ELSE 1 END
        FROM Users WHERE Username...

        return Convert.ToBoolean(cmd.ExecuteScalar());


        I know the language. I've read a book. - _Madmatt

        A 1 Reply Last reply
        0
        • M Maxdd 7

          My users table is that:

          CREATE TABLE Users(
          ID_Users INT IDENTITY(1,1),
          Username VARCHAR(30) UNIQUE,
          Password VARCHAR(100) DEFAULT 'password',
          Nivel tinyint DEFAULT '3',
          PRIMARY KEY(ID_Users));

          When users give username and password executes the following code:

          string sqlUserName;
          sqlUserName = "SELECT ID_Users, Username,Password,Nivel FROM Users ";
          sqlUserName += " WHERE (Username ='" + UserName + "')";
          sqlUserName += " AND (Password ='" + Password + "')";
          SqlCommand com = new SqlCommand(sqlUserName, Conn);

          string existe;
          existe = (string)com.ExecuteScalar();

          if (existe != null)
          (...)

          The problem is, I added five minutes ago "SELECT ID_Users". Now I have the error: "unable to cast int32 to string". I dont understand, because I use sucessfuly tinyint and there is no problem. Ok, its not a int32, but in the end its not the same thing? What should I do ? EDIT: looks like if I do: object existe; existe = com.ExecuteScalar(); There's no error. Can I use it with no problem's ?

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          See other reply for a more acceptable way to handle this. Also, you should be strongly encouraged to not use inline SQL or at the very least use a parameterized query. Search Google for SQL Injection attack.


          I know the language. I've read a book. - _Madmatt

          A 1 Reply Last reply
          0
          • N Not Active

            Why are you trying to use a string as a boolean? The more acceptable way to handle this would...

            SELECT CASE COUNT(*) WHEN 0 THEN 0 ELSE 1 END
            FROM Users WHERE Username...

            return Convert.ToBoolean(cmd.ExecuteScalar());


            I know the language. I've read a book. - _Madmatt

            A Offline
            A Offline
            Abhishek Sur
            wrote on last edited by
            #5

            Check the last line.. I think I have already mentioned that.. . I think the author wants to use the UserID or the data that is selected. So he wrote like this. ;)

            Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


            My Latest Articles-->** Simplify Code Using NDepend
            Basics of Bing Search API using .NET
            Microsoft Bing MAP using Javascript

            N 1 Reply Last reply
            0
            • N Not Active

              See other reply for a more acceptable way to handle this. Also, you should be strongly encouraged to not use inline SQL or at the very least use a parameterized query. Search Google for SQL Injection attack.


              I know the language. I've read a book. - _Madmatt

              A Offline
              A Offline
              Abhishek Sur
              wrote on last edited by
              #6

              Mark Nischalke wrote:

              you should be strongly encouraged to not use inline SQL or at the very least use a parameterized query.

              I accept with you in this regard. :rose::rose:

              Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


              My Latest Articles-->** Simplify Code Using NDepend
              Basics of Bing Search API using .NET
              Microsoft Bing MAP using Javascript

              1 Reply Last reply
              0
              • A Abhishek Sur

                To make you understand, I guess ExecuteScalar executes a query and returns only the first column of the first row. All others are just ignored. So why do you select them ?? Anyways, ExecuteScalar returns an object. So you might use string existe = com.ExecuteScalar().ToString(); But let me remind you, if there is no row selected, this line will through an error. So rather check the object first.

                object result = com.ExecuteScalar();
                if(result != null)
                string existe = result.ToString();

                Hope this is fine. Or you can also check Count(*) to determine if anyone is selected or not in the query. :thumbsup:

                Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                My Latest Articles-->** Simplify Code Using NDepend
                Basics of Bing Search API using .NET
                Microsoft Bing MAP using Javascript

                M Offline
                M Offline
                Maxdd 7
                wrote on last edited by
                #7

                Because I also use SqlDataReader :) I will use object, I think in this case is the best choice for me :) Many thanks for all your help!

                A N 2 Replies Last reply
                0
                • M Maxdd 7

                  Because I also use SqlDataReader :) I will use object, I think in this case is the best choice for me :) Many thanks for all your help!

                  A Offline
                  A Offline
                  Abhishek Sur
                  wrote on last edited by
                  #8

                  Maxdd 7 wrote:

                  Many thanks for all your help!

                  You are most welcome bro... Cheers. :thumbsup:

                  Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                  My Latest Articles-->** Simplify Code Using NDepend
                  Basics of Bing Search API using .NET
                  Microsoft Bing MAP using Javascript

                  1 Reply Last reply
                  0
                  • M Maxdd 7

                    Because I also use SqlDataReader :) I will use object, I think in this case is the best choice for me :) Many thanks for all your help!

                    N Offline
                    N Offline
                    Not Active
                    wrote on last edited by
                    #9

                    Maxdd 7 wrote:

                    Because I also use SqlDataReader

                    That has nothing to do with it. DataReader provides methods such as GetString, GetInt, GetBoolean. Learning to use the tools properly will be a great benefit to you in the future.


                    I know the language. I've read a book. - _Madmatt

                    M 1 Reply Last reply
                    0
                    • A Abhishek Sur

                      Check the last line.. I think I have already mentioned that.. . I think the author wants to use the UserID or the data that is selected. So he wrote like this. ;)

                      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                      My Latest Articles-->** Simplify Code Using NDepend
                      Basics of Bing Search API using .NET
                      Microsoft Bing MAP using Javascript

                      N Offline
                      N Offline
                      Not Active
                      wrote on last edited by
                      #10

                      Abhishek Sur wrote:

                      So he wrote like this

                      This doesn't lessen our responsibility to also point out misunderstandings and mistakes in usage and give guidance on more correct ways of implementation.


                      I know the language. I've read a book. - _Madmatt

                      1 Reply Last reply
                      0
                      • N Not Active

                        Maxdd 7 wrote:

                        Because I also use SqlDataReader

                        That has nothing to do with it. DataReader provides methods such as GetString, GetInt, GetBoolean. Learning to use the tools properly will be a great benefit to you in the future.


                        I know the language. I've read a book. - _Madmatt

                        M Offline
                        M Offline
                        Maxdd 7
                        wrote on last edited by
                        #11

                        I was just answering to this question: " To make you understand, I guess ExecuteScalar executes a query and returns only the first column of the first row. All others are just ignored. So why do you select them ?? " I use SqlData reader (I erased those lines on first post here) so I can extract those values.

                        1 Reply Last reply
                        0
                        • M Maxdd 7

                          My users table is that:

                          CREATE TABLE Users(
                          ID_Users INT IDENTITY(1,1),
                          Username VARCHAR(30) UNIQUE,
                          Password VARCHAR(100) DEFAULT 'password',
                          Nivel tinyint DEFAULT '3',
                          PRIMARY KEY(ID_Users));

                          When users give username and password executes the following code:

                          string sqlUserName;
                          sqlUserName = "SELECT ID_Users, Username,Password,Nivel FROM Users ";
                          sqlUserName += " WHERE (Username ='" + UserName + "')";
                          sqlUserName += " AND (Password ='" + Password + "')";
                          SqlCommand com = new SqlCommand(sqlUserName, Conn);

                          string existe;
                          existe = (string)com.ExecuteScalar();

                          if (existe != null)
                          (...)

                          The problem is, I added five minutes ago "SELECT ID_Users". Now I have the error: "unable to cast int32 to string". I dont understand, because I use sucessfuly tinyint and there is no problem. Ok, its not a int32, but in the end its not the same thing? What should I do ? EDIT: looks like if I do: object existe; existe = com.ExecuteScalar(); There's no error. Can I use it with no problem's ?

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #12

                          where do username and password come from ? Does the user input them ? If they do, this is terrible code. Even if they don't, it is not great.

                          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                          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