locked
Execute scalar RRS feed

  • Question

  • User-1556877445 posted

    I'm trying to use this code but don't understand what ExecuteScalar does despite googling.

    userId is returned from a stored procedure. Could someone please clarify?

    Protected Sub ValidateUser(sender As Object, e As EventArgs)
            Dim userId As Integer = 0
            Dim constr As String = ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("Validate_User")
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@Username", Login1.UserName)
                    cmd.Parameters.AddWithValue("@Password", Login1.Password)
                    cmd.Connection = con
                    con.Open()
                    userId = Convert.ToInt32(cmd.ExecuteScalar())
                    con.Close()
                End Using
                Select Case userId
                    Case -1
                        Login1.FailureText = "Username and/or password is incorrect."
                        Exit Select
                    Case -2
                        Login1.FailureText = "Account has not been activated."
                        Exit Select
                    Case Else
                        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet)
                        Exit Select
                End Select
            End Using
        End Sub

    Wednesday, January 4, 2017 1:50 AM

Answers

  • User2103319870 posted

    but don't understand what ExecuteScalar does despite googling

    Execute scalar is used when the result of query return a single resultset. Execute scalar run the query, and returns the first column of the first row in the result set returned by the query.

    You can find a good explanation in this link

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2017 4:00 AM
  • User1853794821 posted

    Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2017 1:55 PM
  • User753101303 posted

    Hi,

    Or just use the documentation for this kind of basic question. From https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx :

    "Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored."

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2017 2:31 PM

All replies

  • User2103319870 posted

    but don't understand what ExecuteScalar does despite googling

    Execute scalar is used when the result of query return a single resultset. Execute scalar run the query, and returns the first column of the first row in the result set returned by the query.

    You can find a good explanation in this link

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2017 4:00 AM
  • User1853794821 posted

    Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2017 1:55 PM
  • User753101303 posted

    Hi,

    Or just use the documentation for this kind of basic question. From https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx :

    "Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored."

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 4, 2017 2:31 PM
  • User-1556877445 posted

    Thanks, and honestly I did Google this several times but obviously didn't find the right page to explain it.

    Wednesday, January 4, 2017 11:36 PM