The Simplest CRUD Operations for Lookup Tables in ASP.NET - The SQLHelper: continued
(Page 3 of 7 )
This is the successive part from the previous section. Proceeding further, we have the following:
PublicFunction getDataRow(ByVal sqlSELECT As String) As
System.Data.DataRow
Dim da As SqlDataAdapter
Try
Dim dt As New DataTable
da = New SqlDataAdapter(sqlSELECT, _ConnectionString)
da.Fill(dt)
da.Dispose()
If dt.Rows.Count = 0 Then Return Nothing Else Return
dt.Rows(0) 'return only first row
Catch ex As Exception
Try
da.Dispose()
Catch e As Exception
'do nothing...if still error persists
End Try
Throw New Exception(ex.Message & ". SQL Statement: "
& sqlSELECT)
End Try
End Function
The above is mainly used to get a single row from the database. Proceeding further, we have the following:
PublicFunction getRowValue(ByVal sqlSELECT As String) As String
Dim ConnAs SqlConnection
Dim cmd As SqlCommand
Dim value As String = ""
Try
Conn= New SqlConnection(_ConnectionString)
cmd = New SqlCommand(sqlSELECT, Conn)
With cmd
.Connection.Open()
value = .ExecuteScalar() & "" 'concatenating an
empty string..to eliminate null or nothing
.Connection.Close()
.Dispose()
End With
Return value
Catch ex As Exception
Try
If cmd.Connection.State = ConnectionState.Open
Then
cmd.Connection.Close()
cmd.Dispose()
End If
Catch e As Exception
'do nothing...if still error persists
End Try
Throw New Exception(ex.Message & ". SQL Statement: "
& sqlSELECT)
End Try
End Function
The above method is mainly used to retrieve a single value from the database. To use all of the above methods, simply declare the following line, just before any page load event (or in a global module):
Dim db As New SQLHelper
Next: Retrieving information from the database >>
More ASP.NET Articles
More By Jagadish Chaterjee