473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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=MicroSoft.Jet.OLEDB.4.0; Data
source = " & Server.MapPath("Someplace.mdb"))

Dim CheckCus As New OleDbCommand("SELECT * from Cus where EMailAddress =" &
QM & Trim(txtEMail.Text) & QM, cnString)
CheckCus.Connection.Open()

If CheckCus.ExecuteNonQuery() Then
txtFirstName.Text = "Yes"
Else
txtFirstName.Text = "No " & Now
End If

CheckCus.Connection.Close()

Thanks
Tony
Jul 21 '07 #1
8 4569
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=MicroSoft.Jet.OLEDB.4.0; Data
Source = " & Server.MapPath("Someplace.mdb"))
Dim cmd As New OleDbCommand("SELECT * from Cus where EMailAddress =" &
QM & Trim(txtEMail.Text) & QM, cnString)
con.Open()

Dim dr As Oledb.OledbDataReader = cmd.ExecuteReader()

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

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


"Tony M" <To*************@msn.comwrote in message
news:e$**************@TK2MSFTNGP04.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=MicroSoft.Jet.OLEDB.4.0; Data
source = " & Server.MapPath("Someplace.mdb"))

Dim CheckCus As New OleDbCommand("SELECT * from Cus where EMailAddress ="
& QM & Trim(txtEMail.Text) & QM, cnString)
CheckCus.Connection.Open()

If CheckCus.ExecuteNonQuery() Then
txtFirstName.Text = "Yes"
Else
txtFirstName.Text = "No " & Now
End If

CheckCus.Connection.Close()

Thanks
Tony

Jul 21 '07 #2
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=MicroSoft.Jet.OLEDB.4.0; Data
source = " & Server.MapPath("Someplace.mdb"))

Dim CheckCus As New OleDbCommand("SELECT * from Cus where EMailAddress =" &
QM & Trim(txtEMail.Text) & QM, cnString)
CheckCus.Connection.Open()

If CheckCus.ExecuteNonQuery() Then
txtFirstName.Text = "Yes"
Else
txtFirstName.Text = "No " & Now
End If

CheckCus.Connection.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
_____
http://www.guffa.com
Jul 21 '07 #3
Thanks to all.
seems like allot of work just to see if a record exist, but it works.

Thanks again

"Scott M." <s-***@nospam.nospamwrote in message
news:OH**************@TK2MSFTNGP02.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=MicroSoft.Jet.OLEDB.4.0; Data
Source = " & Server.MapPath("Someplace.mdb"))
Dim cmd As New OleDbCommand("SELECT * from Cus where EMailAddress =" &
QM & Trim(txtEMail.Text) & QM, cnString)
con.Open()

Dim dr As Oledb.OledbDataReader = cmd.ExecuteReader()

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

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


"Tony M" <To*************@msn.comwrote in message
news:e$**************@TK2MSFTNGP04.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=MicroSoft.Jet.OLEDB.4.0; Data
source = " & Server.MapPath("Someplace.mdb"))

Dim CheckCus As New OleDbCommand("SELECT * from Cus where EMailAddress ="
& QM & Trim(txtEMail.Text) & QM, cnString)
CheckCus.Connection.Open()

If CheckCus.ExecuteNonQuery() Then
txtFirstName.Text = "Yes"
Else
txtFirstName.Text = "No " & Now
End If

CheckCus.Connection.Close()

Thanks
Tony



Jul 25 '07 #4
"Tony M" <To*************@msn.comwrote in
news:e$**************@TK2MSFTNGP04.phx.gbl:
Dim CheckCus As New OleDbCommand("SELECT * from Cus where EMailAddress
=" & QM & Trim(txtEMail.Text) & QM, cnString)
You should always use SQL Parameters for your queries. Otherwise you can be
the victim of a SQL injection attack.
Jul 25 '07 #5
Gotta ask...

What the heck is a "SQL injection attack"?

Cheers,
Johnny J.


"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
"Tony M" <To*************@msn.comwrote in
news:e$**************@TK2MSFTNGP04.phx.gbl:
>Dim CheckCus As New OleDbCommand("SELECT * from Cus where EMailAddress
=" & QM & Trim(txtEMail.Text) & QM, cnString)

You should always use SQL Parameters for your queries. Otherwise you can
be
the victim of a SQL injection attack.

Jul 25 '07 #6
"Johnny Jörgensen" <jo**@altcom.sewrote in
news:#N**************@TK2MSFTNGP02.phx.gbl:
Gotta ask...

What the heck is a "SQL injection attack"?

http://en.wikipedia.org/wiki/SQL_injection
There are .NET examples in the wikipedia article.

Jul 25 '07 #7
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" <To*************@msn.comwrote in message
news:eJ**************@TK2MSFTNGP06.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-***@nospam.nospamwrote in message
news:OH**************@TK2MSFTNGP02.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=MicroSoft.Jet.OLEDB.4.0; Data
Source = " & Server.MapPath("Someplace.mdb"))
Dim cmd As New OleDbCommand("SELECT * from Cus where EMailAddress =" &
QM & Trim(txtEMail.Text) & QM, cnString)
con.Open()

Dim dr As Oledb.OledbDataReader = cmd.ExecuteReader()

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

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


"Tony M" <To*************@msn.comwrote in message
news:e$**************@TK2MSFTNGP04.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=MicroSoft.Jet.OLEDB.4.0; Data
source = " & Server.MapPath("Someplace.mdb"))

Dim CheckCus As New OleDbCommand("SELECT * from Cus where EMailAddress
=" & QM & Trim(txtEMail.Text) & QM, cnString)
CheckCus.Connection.Open()

If CheckCus.ExecuteNonQuery() Then
txtFirstName.Text = "Yes"
Else
txtFirstName.Text = "No " & Now
End If

CheckCus.Connection.Close()

Thanks
Tony




Jul 25 '07 #8
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" <jo**@altcom.sewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Gotta ask...

What the heck is a "SQL injection attack"?

Cheers,
Johnny J.


"Spam Catcher" <sp**********@rogers.comwrote in message
news:Xn**********************************@127.0.0. 1...
>"Tony M" <To*************@msn.comwrote in
news:e$**************@TK2MSFTNGP04.phx.gbl:
>>Dim CheckCus As New OleDbCommand("SELECT * from Cus where EMailAddress
=" & QM & Trim(txtEMail.Text) & QM, cnString)

You should always use SQL Parameters for your queries. Otherwise you can
be
the victim of a SQL injection attack.


Jul 25 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
Hi, I have a sqlCommand object that I use to execute an SQL statement. I thought I could use the ExecuteNonQuery to test for the number of rows returned by the sql command but it does not seem to...
1
by: Mark | last post by:
It appears that you can only retrieve an Output parameter from a SQL Server stored procedure when using the ExecuteNonQuery of the SqlCommand class, and cannot use the ExecuteReader() method. In...
10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
5
by: Paul Aspinall | last post by:
Hi I have a Stored Proc in SQL server, which creates a record key when a record is created. I want to return the value back to my code, once the record has been created. I am using SQLHelper...
1
by: Matthew Louden | last post by:
I just tried to create a simple ASP.NET application to add record to the SQL Server database. However, it has run time error on Line 88: objCommand.ExecuteNonQuery() Any ideas?? Please help!!...
0
by: johnnymack0730 | last post by:
Hello - I am currently working on an asp.net application that needs to be able to update an access table using ExecuteNonQuery method. I have all the code setup, it runs, there are no errors...
4
by: MDW | last post by:
Hey all. I'm confused. I'm trying to add a single record into an Access 2000 database using ASP.Net. Here is the code: objConn = New OleDbConnection(strConnect) objConn.Open objCommand =...
2
by: jdb | last post by:
Hi, I am adding a record to the database using an ExecuteNonQuery, which adds without problem. Now after the record is added I run a method passing in some info as well as the curretnly opened...
3
by: keithb | last post by:
What can I put in a stored procedure to control what gets returned by command.ExecuteNonQuery()? I already tried this: param = comm.CreateParameter(); param.ParameterName = "@Success"; ...
2
by: TheSteph | last post by:
Hi I have a SQLCommand that do updates on a table. I have a loop where I set the parameters then I call ExecuteNonQuery. For . { . Set the SQLCommand's Params Values;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.