Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Alright guys -

 

I am looking to query a table in my database in SQL and return the value that it finds into a variable. I am saying:

 				SQL="SELECT Balance FROM tmTransactions WHERE UserRecord = '" & user & "'"

. This will ALWAYS only return ONE value. How can I make it so the value that it finds can be stored in a variable? Then I am going to put that variable in my global.asax file??

 

Thanks!

Thanks,

Tehon

  • Moderators
Posted

You can change some of the variables to hard-coded if you wish...

   Private Function GetTableValue(ByVal nID As Integer, ByVal sField As String, ByVal sTable As String) As Integer
       Dim drSqlReader As SqlDataReader
       Dim SqlCMD As SqlCommand
       Dim SqlCN As New SqlConnection(Conn)
       Dim strSql As String, intTemp As Integer
       Try
           If SqlCN.State = ConnectionState.Closed Then SqlCN.Open()
           strSql = "SELECT " & sField & " FROM " & sTable & " WHERE ID = " & nID
           SqlCMD = New SqlCommand(strSql, SqlCN)
           drSqlReader = SqlCMD.ExecuteReader()

           While drSqlReader.Read
               intTemp = CType(Nz(drSqlReader.Item(sField), ""), Integer)
           End While
       Catch
           intTemp = -1
       Finally
           If Not SqlCN.State = ConnectionState.Closed Then SqlCN.Close()
           If Not drSqlReader.IsClosed Then drSqlReader.Close()
           If Not SqlCMD Is Nothing Then SqlCMD.Dispose()
       End Try
       Return intTemp
   End Function

Visit...Bassic Software
Posted

Well what I am trying to do is query my table for a username in the table and I want to store that username in a variable say, User. How can I set the return value of the query to my variable??

 

Thanks

Thanks,

Tehon

  • Leaders
Posted
I don't think so in VB.NET it's built in to Access. My guess is that Robby has it in some utility module that he's implemented himself. Otherwise maybe I've just not found it in .NET yet. Should be simple enough to write though.
--tim
  • Moderators
Posted

try this...

Private Function GetTableValue(ByVal strVal As String, ByVal sField As String, ByVal sTable As String) As string
       Dim drSqlReader As SqlDataReader
       Dim SqlCMD As SqlCommand
       Dim SqlCN As New SqlConnection(Conn)
       Dim strSql As String, sTemp As string =""
       Try
           If SqlCN.State = ConnectionState.Closed Then SqlCN.Open()
           strSql = "SELECT " & sField & " FROM " & sTable & " WHERE User = '" & strVal & "'"
           SqlCMD = New SqlCommand(strSql, SqlCN)
           drSqlReader = SqlCMD.ExecuteReader()

           While drSqlReader.Read
               sTemp = drSqlReader.Item(sField)
           End While
       Catch
          sTemp = "error found"
       Finally
           If Not SqlCN.State = ConnectionState.Closed Then SqlCN.Close()
           If Not drSqlReader.IsClosed Then drSqlReader.Close()
           If Not SqlCMD Is Nothing Then SqlCMD.Dispose()
       End Try
       Return sTemp
   End Function

'and call it like this...
dim sResults as string = GetTableValue("somename","User", "tmTransactions")
messagebox.show(sResults)

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...