ExecuteNonQuery

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony M

    #1

    ExecuteNonQuery



    Shouldn't the ExecuteNonQuery be TRUE if it finds a record and FALSE is not?
    I get FALSE all the time and I seeded the database with the email address.
    I just want to check if record exist before moving on.
    If this doesn't work I guess I could load a datatable and check to see if
    rows 0.


    Dim cnString As OleDbConnection

    cnString = New OleDbConnection ("Provider=Micr oSoft.Jet.OLEDB .4.0; Data
    source = " & Server.MapPath( "Someplace.mdb" ))

    Dim CheckCus As New OleDbCommand("S ELECT * from Cus where EMailAddress =" &
    QM & Trim(txtEMail.T ext) & QM, cnString)


    CheckCus.Connec tion.Open()

    If CheckCus.Execut eNonQuery() Then
    txtFirstName.Te xt = "Yes"
    Else
    txtFirstName.Te xt = "No " & Now
    End If

    CheckCus.Connec tion.Close()

    Thanks
    Tony


  • Scott M.

    #2
    Re: ExecuteNonQuery

    A SELECT statement is a query. You don't use ExecuteNonQuery when you are,
    in fact, querying the database. Since your query *can* return results, you
    need to use the ExecuteReader method of your command, which returns a
    DataReader object that you then use to look at the query results.

    Try this:

    Try
    Dim con As New OleDbConnection ("Provider=Micr oSoft.Jet.OLEDB .4.0; Data
    Source = " & Server.MapPath( "Someplace.mdb" ))
    Dim cmd As New OleDbCommand("S ELECT * from Cus where EMailAddress =" &
    QM & Trim(txtEMail.T ext) & QM, cnString)
    con.Open()

    Dim dr As Oledb.OledbData Reader = cmd.ExecuteRead er()

    If dr.HasRows Then
    txtFirstName.Te xt = "Yes"
    Else
    txtFirstName.Te xt = "No " & Now
    End If

    Catch ex As Oledb.OledbExce ption
    'Database exceptions handled here
    Catch ex As Exception
    'All other exceptions handled here
    Finally
    dr.Close()
    con.Close()
    con.Dispose()
    End Try




    "Tony M" <TonyMast_NOSPA M@msn.comwrote in message
    news:e$v9PY8yHH A.5980@TK2MSFTN GP04.phx.gbl...
    >
    >
    Shouldn't the ExecuteNonQuery be TRUE if it finds a record and FALSE is
    not?
    I get FALSE all the time and I seeded the database with the email address.
    I just want to check if record exist before moving on.
    If this doesn't work I guess I could load a datatable and check to see if
    rows 0.
    >
    >
    Dim cnString As OleDbConnection
    >
    cnString = New OleDbConnection ("Provider=Micr oSoft.Jet.OLEDB .4.0; Data
    source = " & Server.MapPath( "Someplace.mdb" ))
    >
    Dim CheckCus As New OleDbCommand("S ELECT * from Cus where EMailAddress ="
    & QM & Trim(txtEMail.T ext) & QM, cnString)
    >
    >
    CheckCus.Connec tion.Open()
    >
    If CheckCus.Execut eNonQuery() Then
    txtFirstName.Te xt = "Yes"
    Else
    txtFirstName.Te xt = "No " & Now
    End If
    >
    CheckCus.Connec tion.Close()
    >
    Thanks
    Tony
    >

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: ExecuteNonQuery

      Tony M wrote:
      Shouldn't the ExecuteNonQuery be TRUE if it finds a record and FALSE is not?
      I get FALSE all the time and I seeded the database with the email address.
      I just want to check if record exist before moving on.
      If this doesn't work I guess I could load a datatable and check to see if
      rows 0.
      >
      >
      Dim cnString As OleDbConnection
      >
      cnString = New OleDbConnection ("Provider=Micr oSoft.Jet.OLEDB .4.0; Data
      source = " & Server.MapPath( "Someplace.mdb" ))
      >
      Dim CheckCus As New OleDbCommand("S ELECT * from Cus where EMailAddress =" &
      QM & Trim(txtEMail.T ext) & QM, cnString)
      >
      >
      CheckCus.Connec tion.Open()
      >
      If CheckCus.Execut eNonQuery() Then
      txtFirstName.Te xt = "Yes"
      Else
      txtFirstName.Te xt = "No " & Now
      End If
      >
      CheckCus.Connec tion.Close()
      >
      Thanks
      Tony
      >
      The method doesn't return a boolean at all, it returns an integer. If
      you would have use Option Strict the compiler would have told you that.

      The return value is the number of affected records. As a select query
      never changes any records, your call will always return zero.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Tony M

        #4
        Re: ExecuteNonQuery

        Thanks to all.
        seems like allot of work just to see if a record exist, but it works.

        Thanks again

        "Scott M." <s-mar@nospam.nosp amwrote in message
        news:OHIZ3k8yHH A.2224@TK2MSFTN GP02.phx.gbl...
        >A SELECT statement is a query. You don't use ExecuteNonQuery when you are,
        >in fact, querying the database. Since your query *can* return results, you
        >need to use the ExecuteReader method of your command, which returns a
        >DataReader object that you then use to look at the query results.
        >
        Try this:
        >
        Try
        Dim con As New OleDbConnection ("Provider=Micr oSoft.Jet.OLEDB .4.0; Data
        Source = " & Server.MapPath( "Someplace.mdb" ))
        Dim cmd As New OleDbCommand("S ELECT * from Cus where EMailAddress =" &
        QM & Trim(txtEMail.T ext) & QM, cnString)
        con.Open()
        >
        Dim dr As Oledb.OledbData Reader = cmd.ExecuteRead er()
        >
        If dr.HasRows Then
        txtFirstName.Te xt = "Yes"
        Else
        txtFirstName.Te xt = "No " & Now
        End If
        >
        Catch ex As Oledb.OledbExce ption
        'Database exceptions handled here
        Catch ex As Exception
        'All other exceptions handled here
        Finally
        dr.Close()
        con.Close()
        con.Dispose()
        End Try
        >
        >
        >
        >
        "Tony M" <TonyMast_NOSPA M@msn.comwrote in message
        news:e$v9PY8yHH A.5980@TK2MSFTN GP04.phx.gbl...
        >>
        >>
        >Shouldn't the ExecuteNonQuery be TRUE if it finds a record and FALSE is
        >not?
        >I get FALSE all the time and I seeded the database with the email
        >address.
        >I just want to check if record exist before moving on.
        >If this doesn't work I guess I could load a datatable and check to see if
        >rows 0.
        >>
        >>
        >Dim cnString As OleDbConnection
        >>
        >cnString = New OleDbConnection ("Provider=Micr oSoft.Jet.OLEDB .4.0; Data
        >source = " & Server.MapPath( "Someplace.mdb" ))
        >>
        >Dim CheckCus As New OleDbCommand("S ELECT * from Cus where EMailAddress ="
        >& QM & Trim(txtEMail.T ext) & QM, cnString)
        >>
        >>
        >CheckCus.Conne ction.Open()
        >>
        >If CheckCus.Execut eNonQuery() Then
        > txtFirstName.Te xt = "Yes"
        >Else
        > txtFirstName.Te xt = "No " & Now
        >End If
        >>
        >CheckCus.Conne ction.Close()
        >>
        >Thanks
        >Tony
        >>
        >
        >
        >

        Comment

        • Spam Catcher

          #5
          Re: ExecuteNonQuery

          "Tony M" <TonyMast_NOSPA M@msn.comwrote in
          news:e$v9PY8yHH A.5980@TK2MSFTN GP04.phx.gbl:
          Dim CheckCus As New OleDbCommand("S ELECT * from Cus where EMailAddress
          =" & QM & Trim(txtEMail.T ext) & QM, cnString)
          You should always use SQL Parameters for your queries. Otherwise you can be
          the victim of a SQL injection attack.

          Comment

          • Johnny Jörgensen

            #6
            Re: ExecuteNonQuery

            Gotta ask...

            What the heck is a "SQL injection attack"?

            Cheers,
            Johnny J.




            "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
            news:Xns99781AB 507DABusenethon eypotrogers@127 .0.0.1...
            "Tony M" <TonyMast_NOSPA M@msn.comwrote in
            news:e$v9PY8yHH A.5980@TK2MSFTN GP04.phx.gbl:
            >
            >Dim CheckCus As New OleDbCommand("S ELECT * from Cus where EMailAddress
            >=" & QM & Trim(txtEMail.T ext) & QM, cnString)
            >
            You should always use SQL Parameters for your queries. Otherwise you can
            be
            the victim of a SQL injection attack.

            Comment

            • Spam Catcher

              #7
              Re: ExecuteNonQuery

              "Johnny Jörgensen" <jojo@altcom.se wrote in
              news:#NlQWlozHH A.4824@TK2MSFTN GP02.phx.gbl:
              Gotta ask...
              >
              What the heck is a "SQL injection attack"?




              There are .NET examples in the wikipedia article.

              Comment

              • Scott M.

                #8
                Re: ExecuteNonQuery

                Just remember Tony, that connecting to a database and querying it is one of
                the most common things programmers do and it's one of the most common things
                programmers do incorrectly or incompletely.

                Putting your code inside of a Try...Catch and remembering to dispose of the
                connection are essential to building robust database applications.

                -Scott

                "Tony M" <TonyMast_NOSPA M@msn.comwrote in message
                news:eJ3JSTnzHH A.1484@TK2MSFTN GP06.phx.gbl...
                Thanks to all.
                seems like allot of work just to see if a record exist, but it works.
                >
                Thanks again
                >
                "Scott M." <s-mar@nospam.nosp amwrote in message
                news:OHIZ3k8yHH A.2224@TK2MSFTN GP02.phx.gbl...
                >>A SELECT statement is a query. You don't use ExecuteNonQuery when you
                >>are, in fact, querying the database. Since your query *can* return
                >>results, you need to use the ExecuteReader method of your command, which
                >>returns a DataReader object that you then use to look at the query
                >>results.
                >>
                >Try this:
                >>
                >Try
                > Dim con As New OleDbConnection ("Provider=Micr oSoft.Jet.OLEDB .4.0; Data
                >Source = " & Server.MapPath( "Someplace.mdb" ))
                > Dim cmd As New OleDbCommand("S ELECT * from Cus where EMailAddress =" &
                >QM & Trim(txtEMail.T ext) & QM, cnString)
                > con.Open()
                >>
                > Dim dr As Oledb.OledbData Reader = cmd.ExecuteRead er()
                >>
                > If dr.HasRows Then
                > txtFirstName.Te xt = "Yes"
                > Else
                > txtFirstName.Te xt = "No " & Now
                > End If
                >>
                >Catch ex As Oledb.OledbExce ption
                > 'Database exceptions handled here
                >Catch ex As Exception
                > 'All other exceptions handled here
                >Finally
                > dr.Close()
                > con.Close()
                > con.Dispose()
                >End Try
                >>
                >>
                >>
                >>
                >"Tony M" <TonyMast_NOSPA M@msn.comwrote in message
                >news:e$v9PY8yH HA.5980@TK2MSFT NGP04.phx.gbl.. .
                >>>
                >>>
                >>Shouldn't the ExecuteNonQuery be TRUE if it finds a record and FALSE is
                >>not?
                >>I get FALSE all the time and I seeded the database with the email
                >>address.
                >>I just want to check if record exist before moving on.
                >>If this doesn't work I guess I could load a datatable and check to see
                >>if rows 0.
                >>>
                >>>
                >>Dim cnString As OleDbConnection
                >>>
                >>cnString = New OleDbConnection ("Provider=Micr oSoft.Jet.OLEDB .4.0; Data
                >>source = " & Server.MapPath( "Someplace.mdb" ))
                >>>
                >>Dim CheckCus As New OleDbCommand("S ELECT * from Cus where EMailAddress
                >>=" & QM & Trim(txtEMail.T ext) & QM, cnString)
                >>>
                >>>
                >>CheckCus.Conn ection.Open()
                >>>
                >>If CheckCus.Execut eNonQuery() Then
                >> txtFirstName.Te xt = "Yes"
                >>Else
                >> txtFirstName.Te xt = "No " & Now
                >>End If
                >>>
                >>CheckCus.Conn ection.Close()
                >>>
                >>Thanks
                >>Tony
                >>>
                >>
                >>
                >>
                >
                >

                Comment

                • Scott M.

                  #9
                  Re: ExecuteNonQuery

                  It's when the user passes data (injects) that would cause SQL to throw an
                  exception and possibly reveal information about your database, table, fields
                  away for more detailed attacks. Simply passing a single quote can do it.


                  "Johnny Jörgensen" <jojo@altcom.se wrote in message
                  news:%23NlQWloz HHA.4824@TK2MSF TNGP02.phx.gbl. ..
                  Gotta ask...
                  >
                  What the heck is a "SQL injection attack"?
                  >
                  Cheers,
                  Johnny J.
                  >
                  >
                  >
                  >
                  "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
                  news:Xns99781AB 507DABusenethon eypotrogers@127 .0.0.1...
                  >"Tony M" <TonyMast_NOSPA M@msn.comwrote in
                  >news:e$v9PY8yH HA.5980@TK2MSFT NGP04.phx.gbl:
                  >>
                  >>Dim CheckCus As New OleDbCommand("S ELECT * from Cus where EMailAddress
                  >>=" & QM & Trim(txtEMail.T ext) & QM, cnString)
                  >>
                  >You should always use SQL Parameters for your queries. Otherwise you can
                  >be
                  >the victim of a SQL injection attack.
                  >
                  >

                  Comment

                  Working...