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.SqlClient.SqlConnection =
GetConnection() 'See function below
Dim cmdCommand As New SqlCommand(SQL, conConnection)
Dim objResults As Object
Dim sReturn As String = String.Empty
Try
cmdCommand.Connection.Open()
objResults = cmdCommand.ExecuteScalar 'NOTE: Do not use
CommandBehavior.CloseConnection here.
If TypeOf objResults Is System.DBNull Then
sReturn = String.Empty
Else
sReturn = CType(objResults, String)
End If
Catch sqlex As SqlException
Throw New Exception(sqlex.Message)
Catch ex As Exception
If ex.InnerException Is Nothing Then
Throw New Exception(ex.Message)
Else
Throw New Exception(ex.InnerException.Message)
End If
Finally
cmdCommand.Dispose()
conConnection.Close()
conConnection.Dispose()
End Try
Return sReturn
End Function
Public Function GetConnection() As System.Data.SqlClient.SqlConnection
Return New System.Data.SqlClient.SqlConnection(YouConnectionS tringHere)
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("SUBJECT").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=@theLastName and
firstname=@theFirstName
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**********************************@microsof t.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!