473,785 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 21 '07 #1
8 4586
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" <To************ *@msn.comwrote in message
news:e$******** ******@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

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=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
_____
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.nosp amwrote in message
news:OH******** ******@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" <To************ *@msn.comwrote in message
news:e$******** ******@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



Jul 25 '07 #4
"Tony M" <To************ *@msn.comwrote in
news:e$******** ******@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.
Jul 25 '07 #5
Gotta ask...

What the heck is a "SQL injection attack"?

Cheers,
Johnny J.


"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
"Tony M" <To************ *@msn.comwrote in
news:e$******** ******@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.

Jul 25 '07 #6
"Johnny Jörgensen" <jo**@altcom.se wrote in
news:#N******** ******@TK2MSFTN GP02.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******** ******@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-***@nospam.nosp amwrote in message
news:OH******** ******@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" <To************ *@msn.comwrote in message
news:e$******* *******@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




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.se wrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
Gotta ask...

What the heck is a "SQL injection attack"?

Cheers,
Johnny J.


"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
>"Tony M" <To************ *@msn.comwrote in
news:e$******* *******@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.


Jul 25 '07 #9

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

Similar topics

2
7347
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 work My code is below.. Dim sSQL As String = "SELECT Blah FROM Table WHERE BlahID = 5" Dim oCOmmand As New SqlClient.SqlCommand(sSQL, openDatabase)
1
25269
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 the code sample below, it bombs on the line with three *** when using the ExecuteReader, but works just fine when you use the ExecuteNonQuery. When using the ExecuteReader, the Output parameter appears to return a value of Null. The stored...
10
3401
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 AddMinimunWageStipen(string payrollid,double amount) { System.Data.SqlClient.SqlConnection cn = null; System.Data.SqlClient.SqlCommand cm = null;
5
10969
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 (Data Application Blocks for SQLServer), and calling the ExecuteNonQuery method in the SQLHelper class...
1
4274
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!! Thanks!! Sub AddNewRecord() Dim objConnection As SqlConnection Dim strConnection As String = "Data Source=20.11.12.91;Network Library=DBMSSOCN;Initial Catalog=testtable;User ID=sa;Password=iw;"
0
3078
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 thrown, yet...nothing is being updated. The only thing I can see is the ExecuteNonQuery returns 0 when the update is run. Can anyone help please? Here is some of the code:
4
6506
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 = New OleDbCommand("INSERT INTO LOGIN_MASTER (LOGIN_ID, PWD, F_NAME, L_NAME, TYPE_ID) VALUES ('" & strEmail & "','" & strPwd & "','" &
2
1528
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 connection (byRef cn as OleDbConnection). Am using an Access 2000 database. Now in this new method I create a command object using the passed in connection object. I then create a DataReader to read the database that has the record added in the...
3
3416
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"; param.Value = Convert.ToInt32(1); param.DbType = DbType.Int32;
2
2112
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
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9949
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.