473,569 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

syntax to see if record exists in SQL!! (ASAP!)

ST
Hi,
I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to
write the syntax to check a sql table to see if the record already exists
based on firstname and lastname text fields (will match to firstname and
lastname in SQL table). I can't figure out the syntax!!! I would like the
Error msg to just display and exit the sub if the row exists. help!!!!
thanks!
Nov 22 '05 #1
5 6161
JV
try a sql statement something like:

SELECT count(*) from myTable where lastname=@theLa stName and
firstname=@theF irstName

where the parameters with the @ signs are where you plug in the values you
are searching on. Then you will know not only if one exists, but how many.

You can also use the EXISTS clause if you like.

If you are using MS Sql Server, I can recommend the online help in the
Query Analyzer function. Just look at "Transact SQL" help, which is also
accessible from your Windows START menu
"ST" <ST@discussions .microsoft.com> wrote in message
news:84******** *************** ***********@mic rosoft.com...
Hi,
I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to
write the syntax to check a sql table to see if the record already exists
based on firstname and lastname text fields (will match to firstname and
lastname in SQL table). I can't figure out the syntax!!! I would like
the
Error msg to just display and exit the sub if the row exists. help!!!!
thanks!

Nov 22 '05 #2
ST
that sounds great! but what I need help with is developing the vb.net code
to run the check...ie, i'm a little confused when to use data adaptors (da)
dataset( ds) and datarow (dr)

i was trying something like this

'Check if subject already exists
Dim comm As String = "Select * from Subject s WHERE
s.subject_lname = '" & txtLN.Text & "' AND s.subject_fname = '" & txtFN.Text
& "'"

Dim da As New SqlDataAdapter( comm, connStr)
Dim ds As New DataSet

dachk.Fill(ds, "SUBJECT")
Dim dr As DataRow
For Each dr In ds.Tables("SUBJ ECT").Rows
If sql.rows.count > 0 Then ****THIS PART DOESN"T WORK, Can't
figure out the code to perform this operation!
lblchk.Visible = True
Exit Sub
Else
End If
Next

"JV" wrote:
try a sql statement something like:

SELECT count(*) from myTable where lastname=@theLa stName and
firstname=@theF irstName

where the parameters with the @ signs are where you plug in the values you
are searching on. Then you will know not only if one exists, but how many.

You can also use the EXISTS clause if you like.

If you are using MS Sql Server, I can recommend the online help in the
Query Analyzer function. Just look at "Transact SQL" help, which is also
accessible from your Windows START menu
"ST" <ST@discussions .microsoft.com> wrote in message
news:84******** *************** ***********@mic rosoft.com...
Hi,
I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to
write the syntax to check a sql table to see if the record already exists
based on firstname and lastname text fields (will match to firstname and
lastname in SQL table). I can't figure out the syntax!!! I would like
the
Error msg to just display and exit the sub if the row exists. help!!!!
thanks!


Nov 22 '05 #3
Hi ST,

VB.Net code isn't my strongest side, but what is 'sql'?
Also, the if statement in your 'for each' won't be reached if there are no rows in the DataTable

Furthermore, DataAdapter.Fil l returns the number of rows affected, so

[C#]
if( da.Fill(ds, "SUBJECT") > 0 )
// record found

Now, if you don't need to use the records you might be better off using
SqlCommand.Exec uteNonQuery instead.

using(SqlComman d sc = new SqlCommand(quer yText, conn))
{
if(sc.ExecuteNo nQuery() > 0)
// record exists)
}

You may need to encapsulate the query with sc.Open() and sc.Close().
On Fri, 24 Jun 2005 20:29:01 +0200, ST <ST@discussions .microsoft.com> wrote:
that sounds great! but what I need help with is developing the vb.net code
to run the check...ie, i'm a little confused when to use data adaptors (da)
dataset( ds) and datarow (dr)

i was trying something like this

'Check if subject already exists
Dim comm As String = "Select * from Subject s WHERE
s.subject_lname = '" & txtLN.Text & "' AND s.subject_fname = '" & txtFN.Text
& "'"

Dim da As New SqlDataAdapter( comm, connStr)
Dim ds As New DataSet

dachk.Fill(ds, "SUBJECT")
Dim dr As DataRow
For Each dr In ds.Tables("SUBJ ECT").Rows
If sql.rows.count > 0 Then ****THIS PART DOESN"T WORK, Can't
figure out the code to perform this operation!
lblchk.Visible = True
Exit Sub
Else
End If
Next

"JV" wrote:
try a sql statement something like:

SELECT count(*) from myTable where lastname=@theLa stName and
firstname=@theF irstName

where the parameters with the @ signs are where you plug in the values you
are searching on. Then you will know not only if one exists, but how many.

You can also use the EXISTS clause if you like.

If you are using MS Sql Server, I can recommend the online help in the
Query Analyzer function. Just look at "Transact SQL" help, which is also
accessible from your Windows START menu
"ST" <ST@discussions .microsoft.com> wrote in message
news:84******** *************** ***********@mic rosoft.com...
> Hi,
> I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to
> write the syntax to check a sql table to see if the record already exists
> based on firstname and lastname text fields (will match to firstname and
> lastname in SQL table). I can't figure out the syntax!!! I would like
> the
> Error msg to just display and exit the sub if the row exists. help!!!!
> thanks!
>
>



--
Happy coding!
Morten Wennevik [C# MVP]
Nov 22 '05 #4
"ST" wrote:
For Each dr In ds.Tables("SUBJ ECT").Rows
If sql.rows.count > 0 Then ****THIS PART DOESN"T WORK, Can't
figure out the code to perform this operation!
lblchk.Visible = True
Exit Sub
Else
End If
Next


Where is the "sql.rows.count " object coming from? "dr" is a row object so if
you get past the "For Each dr In ds.Tables("SUBJ ECT").Rows" line then you
have a populated row object (dr), meaning you have some data. Not the ideal
way of doing it but it would work.

Since you're in a hurry you can try this:
For Each dr In ds.Tables("SUBJ ECT").Rows
lblchk.Visible = True
Exit Sub
Next

When you get some time, check out the ExecuteScalar function of the
System.Data.Sql Client.SqlComma nd.Command object. That will bring back a
single value for you rapidly.

Good luck!
Nov 22 '05 #5
Here's a function for you when you get some time to review.

Public Function GetSingleValue( ByVal SQL As String) As String
Dim conConnection As System.Data.Sql Client.SqlConne ction =
GetConnection() 'See function below
Dim cmdCommand As New SqlCommand(SQL, conConnection)
Dim objResults As Object
Dim sReturn As String = String.Empty

Try
cmdCommand.Conn ection.Open()
objResults = cmdCommand.Exec uteScalar 'NOTE: Do not use
CommandBehavior .CloseConnectio n here.
If TypeOf objResults Is System.DBNull Then
sReturn = String.Empty
Else
sReturn = CType(objResult s, String)
End If

Catch sqlex As SqlException
Throw New Exception(sqlex .Message)
Catch ex As Exception
If ex.InnerExcepti on Is Nothing Then
Throw New Exception(ex.Me ssage)
Else
Throw New Exception(ex.In nerException.Me ssage)
End If
Finally
cmdCommand.Disp ose()
conConnection.C lose()
conConnection.D ispose()
End Try

Return sReturn
End Function

Public Function GetConnection() As System.Data.Sql Client.SqlConne ction
Return New System.Data.Sql Client.SqlConne ction(YouConnec tionStringHere)
End Function

"ST" wrote:
that sounds great! but what I need help with is developing the vb.net code
to run the check...ie, i'm a little confused when to use data adaptors (da)
dataset( ds) and datarow (dr)

i was trying something like this

'Check if subject already exists
Dim comm As String = "Select * from Subject s WHERE
s.subject_lname = '" & txtLN.Text & "' AND s.subject_fname = '" & txtFN.Text
& "'"

Dim da As New SqlDataAdapter( comm, connStr)
Dim ds As New DataSet

dachk.Fill(ds, "SUBJECT")
Dim dr As DataRow
For Each dr In ds.Tables("SUBJ ECT").Rows
If sql.rows.count > 0 Then ****THIS PART DOESN"T WORK, Can't
figure out the code to perform this operation!
lblchk.Visible = True
Exit Sub
Else
End If
Next

"JV" wrote:
try a sql statement something like:

SELECT count(*) from myTable where lastname=@theLa stName and
firstname=@theF irstName

where the parameters with the @ signs are where you plug in the values you
are searching on. Then you will know not only if one exists, but how many.

You can also use the EXISTS clause if you like.

If you are using MS Sql Server, I can recommend the online help in the
Query Analyzer function. Just look at "Transact SQL" help, which is also
accessible from your Windows START menu
"ST" <ST@discussions .microsoft.com> wrote in message
news:84******** *************** ***********@mic rosoft.com...
Hi,
I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to
write the syntax to check a sql table to see if the record already exists
based on firstname and lastname text fields (will match to firstname and
lastname in SQL table). I can't figure out the syntax!!! I would like
the
Error msg to just display and exit the sub if the row exists. help!!!!
thanks!


Nov 22 '05 #6

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

Similar topics

5
854
by: ST | last post by:
Hi, I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to write the syntax to check a sql table to see if the record already exists based on firstname and lastname text fields (will match to firstname and lastname in SQL table). I can't figure out the syntax!!! I would like the Error msg to just display and exit the sub...
1
2396
by: David C. Barber | last post by:
I'm trying to determine if any matching records exist on a LIKE query performing a partial match of last names to a remote back-end database in the most efficient manner possible. LAN Traffic appears to be the bottleneck. This column is already indexed, and all I'm trying to determine is whether no records, or at least one record, exists. ...
6
1998
by: Opie | last post by:
What would be a more efficient way for me to determine if a record in an SQL DB table exists? Right now, I have a try/catch like this: try { if(checkcom.ExecuteScalar().ToString()==tbBillNumber.Text) { .... } }
20
2120
by: Bryan | last post by:
hello all... im trying to add a record to an sql db on ms sql server 2000, using vb.net. seems to be working.. except for one thing, one of the columns in the database is a bit datatype, and though i get no syntax errors when compiling, i get an error indicated that the data would be truncated. the field is login_status. ive tried in...
1
1075
by: mcasaurabhsumit | last post by:
Hello friend, i have difficulty to find out last record in sql server 2000? could u plz help me?
6
8140
by: Matt | last post by:
I need some guidance on how to handle an issue. I have an .asp page that correctly queries a table and returns data if a 'job number' and week ending date exist and the user can update the information that is there. What I need to do is, if a record does not exist the page needs to create one and then refresh/requery so the user can edit the...
1
6566
by: mcasaurabhsumit | last post by:
Hi friend, I am unable to get last record in sql server 2000. How to find last record in sql server 2000?
6
3596
by: Helena666 | last post by:
Hi Its been a while since I have built a database using access and vba and am a bit rusty. I am using a command button on a form to write a record to a table, using an append query. However I need to test if a "live" record exists in that table and if it does to let the user know that the record has not been written. The append query will only...
1
1120
by: parag100 | last post by:
hello there is a sinple data entry form in VB.NET. there r 5 text filds n labels. two buttons SAVE N EXIT USE DATAREADER. how i insert record in sql server table. assume i have connected coding on form load. what be on save button. table EMP filds :ename,empno,sal,deptno,mgr.
7
3397
by: djpaul | last post by:
Hello! I want to check if a record exists in the last 30 records that were added. For example, i have a table wich contains about 34000 records by now. But i want to check if the songID is in the last 30 records. Is this somehow possible? Now i select the last 30 records and put them in an array, and let php check if this songID is in the...
0
7700
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...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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. ...
0
8125
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...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
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...
1
5513
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...
1
2114
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
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.