Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 2nd, 2003, 05:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default Function Question

i have links that are returned and look like this:
test.aspx?loc=2&h=5

5 is the id

Is there any way to write a function that returns the value based on id and then use that value in another function?


 
Old December 2nd, 2003, 05:41 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Public Function getHvalue() As Integer
    Return CType(Request.QueryString("h"), Integer)
End Function

Might want to add some error handling to it but that's the basics.
 
Old December 2nd, 2003, 05:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Can that be then used to query the database based on id to return the value associated with that id?

 
Old December 2nd, 2003, 09:53 PM
Registered User
 
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You wouldn't even need the function if you just want to query a database based on the value of h in the querystring.

Dim conn As New SqlConnection (<connection string>)
Dim cmd As SqlCommand
Dim dtr As SqlDataReader

cmd = new SqlCommand ("SELECT * FROM TABLE WHERE ID = @ID", conn)
cmd.Parameters.Add ("@ID", Request.QueryString ("h"))
conn.Open ()
dtr = cmd.ExecuteReader ()
dtr.Read ()
dtr.Close ()
conn.Close ()
 
Old December 3rd, 2003, 03:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, in general I think you have to bit careful with the use of query string variables directly in SQL queries, what if I did something like this...
Code:
test.aspx?loc=2&h=;drop%20database;
Or had other bad intensions! The point being... think it through, and format the input from the query string so that nothing can go wrong.

Jacob.
 
Old December 3rd, 2003, 09:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Are there any good books that deal with functions or web sites with tutorials?
 
Old December 4th, 2003, 10:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I've written this:
Public Function getHvalue() As Integer
        Return CType(Request.QueryString("h"), Integer)
End Function
Public Function getTdata()
        Dim strConn As String = ConfigurationSettings.AppSettings("mytabledb")
        Dim objConn As New OleDbConnection(strConn)
        Dim strSQL As String = "SELECT * FROM Table3 WHERE (id=" & getHvalue() & ");"

End Function

I'm unsure how to return the value in the second Function. Can this work?
 
Old December 4th, 2003, 10:38 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Typically, when you want to return some kind of data structure from a function, you need to use a dataset and return it or a data table from it. If you try to use a datareader, it'll break, because the datareader is only filled with data as long as the connection is open. Seeing as you should close the connection before you exit the function, you'll loose the state of the reader. Here's what I'd do:

Public Function getTdata() As DataTable
    Dim strConn As String = ConfigurationSettings.AppSettings("mytabledb")
    Dim objConn As New OleDbConnection(strConn)
    Dim strSQL As String = "SELECT * FROM Table3 WHERE (id=" & getHvalue() & ");"
    Dim objCommand As New OleDbCommand(strSQL, objConn)
    Dim objDataSet As New DataSet
    objConn.Open
    objCommand.Fill(objDataSet,"Table3")
    objConn.Close
    Return objDataSet.Tables(0)
End Function

I'm sure I'm missing something, but this is the general idea.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 4th, 2003, 12:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Here is how I did it with classic asp 3.0 and I tried to use it with asp.net but I'm getting a weird response.
----------------------
Dim objConn
objConn = Server.CreateObject("ADODB.Connection")
objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Dat a Source=C:/data/mytable.mdb")

Dim rS
Dim strSQL = "SELECT * FROM table3 WHERE id=" & Request.QueryString("h") & ""

rS = Server.CreateObject("ADODB.Recordset")
rS.Open(strSQL, objConn)

Response.Write(rS("prod_name"))
-------------------------------
Here is what's being returned: System.__ComObject

I have aspcompat="true" in the page directive. Can this be converted to asp.net?
 
Old December 4th, 2003, 12:29 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What's your goal here? Do you want return a single value from a query? If that's the case, use the "ExecuteScalar" method to return a single value. You'll also need to change the query to only select the one field you are interested in.

How about this?

Public Function getTdata() As String
    Dim strConn As String = ConfigurationSettings.AppSettings("mytabledb")
    Dim objConn As New OleDbConnection(strConn)
    Dim strSQL As String = "SELECT prod_name FROM Table3 WHERE (id=" & getHvalue() & ");"
    Dim objCommand As New OleDbCommand(strSQL, objConn)
    Dim sResult As String
    objConn.Open
    sResult = CType(objCommand.ExecuteScalar(), String)
    objConn.Close
    Return sResult
End Function

Again, this suggestion lacks error handling and actual testing so you will need to tweak it.

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Another Document ( ) Function Question bonekrusher XSLT 2 August 4th, 2006 10:45 AM
FormatCurrency Function Question kwilliams VBScript 0 May 12th, 2006 09:17 AM
Question on the ARRAY function savoym VBScript 6 May 31st, 2005 06:53 AM
Shell Function Question SerranoG Access VBA 4 November 12th, 2003 07:57 PM
Body Onload Function Question apd8x Javascript 2 July 10th, 2003 02:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.