tehon3299 Posted February 9, 2003 Posted February 9, 2003 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! Quote Thanks, Tehon
Moderators Robby Posted February 9, 2003 Moderators Posted February 9, 2003 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 Quote Visit...Bassic Software
tehon3299 Posted February 10, 2003 Author Posted February 10, 2003 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 Quote Thanks, Tehon
tehon3299 Posted February 10, 2003 Author Posted February 10, 2003 What is 'Nz' in the While drSqlReader.Read?? Thanks Quote Thanks, Tehon
Leaders quwiltw Posted February 10, 2003 Leaders Posted February 10, 2003 It's a function that evaluaties the parameter to see if it's null, and if it is, it returns the value of the second parameter. Quote --tim
tehon3299 Posted February 10, 2003 Author Posted February 10, 2003 Is this function already written?? Quote Thanks, Tehon
Leaders quwiltw Posted February 10, 2003 Leaders Posted February 10, 2003 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. Quote --tim
tehon3299 Posted February 10, 2003 Author Posted February 10, 2003 Is there an easier way to get information out of an SQL table and store it in variables?? Quote Thanks, Tehon
Moderators Robby Posted February 10, 2003 Moderators Posted February 10, 2003 Sorry about the NZ(), it's my own function. Quote Visit...Bassic Software
tehon3299 Posted February 10, 2003 Author Posted February 10, 2003 Is it really necessary?? Quote Thanks, Tehon
Moderators Robby Posted February 10, 2003 Moderators Posted February 10, 2003 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) Quote Visit...Bassic Software
tehon3299 Posted February 10, 2003 Author Posted February 10, 2003 Will messagebox.show(sResults) work on an ASPX page?? Quote Thanks, Tehon
Moderators Robby Posted February 10, 2003 Moderators Posted February 10, 2003 The NZ was to check if the field is null so you don't get an error, once you get this working and understand it, I'll spring that on you. (not that it's very complicated) Quote Visit...Bassic Software
tehon3299 Posted February 10, 2003 Author Posted February 10, 2003 Alright I see. Does that messagebox.show show a message box on an ASPX page? Quote Thanks, Tehon
Moderators Robby Posted February 10, 2003 Moderators Posted February 10, 2003 messagebox.show will not show on an aspx, step-through your code or use response.write(sResults) Quote Visit...Bassic Software
tehon3299 Posted February 10, 2003 Author Posted February 10, 2003 OK...Great...I'll try that and get back to you...Thanks a lot! Quote Thanks, Tehon
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.