Retrieving database data

  • 12 years ago

    At the moment l'm designing this web page that search's the database based on criteria. When an user enters information into the criteria fields and clicks on the search button, specific information appears into a table.


    Well obviously i need to use SQL in ASP. Example SELECT from appropriate and so on. But the problem l'm facing is that i don't now what code i should used to retireve data from the database. And should i use a javascript code behind the search button to call the asp code or can i call the asp code once i click on the search button. Confused !!


    Please Help - Urgent.


    thanks  

  • 12 years ago

    you should use ADO.Net Objects


    here's the code that you should add to your Button's Click event




    '-------this code should be added outside the Button's Click event handler, but this can added inside the Button's
    '---Click event handler ------------


    Dim Cn as new System.Data.SqlConnection(Your Connection)
    Dim sampleAdapter as new System.Data.SqlDataAdapter("SELECT * FROM Table WHERE Column='" & SearchString.ToString() & "'",Cn)
    Dim sampleDataset as new Dataset()


    '------this code should be added inside the Button's Click event handler -----


    Try
         sampleAdapter.Fill(sampleDataset,"AnyName")
         DataGrid1.DataSource = sampleDataSet.Tables("AnyName")
         DataGrid1.DataBind()
    Catch exc As Exception
         Response.Write(exc.toString())
    End Try


    '-----this could work but this is not the best code it could be-------

  • 12 years ago

    If you click here, you'll be shown a ASP tutorial for beginners (I used it myself to learn!) that includes how to retrieve data from a database.

  • 12 years ago

    This may help a little also.
    I'm guessing you are NOT using the dot net frameworks. So you must be using vbscript. Now I don't know is if when the user input his search information. If you are sending that search back to the same page and are you using the (Get) or (Post) method or are you sending them to another page to display the information. Either way they are just about the same. Also is there more then one type of search. What I mean is are they going to be searching by Like = > < and so on. I am going to say you are sending them to another page to display the information. That you are using the Get method in your form that you are using the Like in your sql statement and you want to return all the information from any hits.



    Processing page.


    <%@ Language=VBScript %>
    <% Option Explicit %>
    <%
    Dim search
    search = Request.QueryString("search")


    If search = "" Then
    'You can do what ever you want either have them use the back button or use a
    'redirect to send them back.
    Response.Write("You did not input a search word. <br>")
    Response.Write("Please use the back button to return to the search page")
    Else
    Dim strSQL
    'The first thing I am going to do is build my sql statement
    strSQL = "Select * From YourTableName Where YourColumnName Like '%" & search & "%';"
    ‘Now we open you database with whatever connection type you want.
    Dim objConn, objRS, x
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.ConnectionString= "Driver={Microsoft Access Driver (*.mdb)};" & _
    "DBQ=" & Server.MapPath("db/YourDatabase.mdb")
    objConn.Open


    'Open recordset
    Set objRS = objConn.Execute(strSQL)
    If objRS.EOF Then
    Response.Write("No records were found with this search <br>")
    Response.Write("Please use the back button to try a new search")
    objRS.Close
    Set objRS = Nothing
    objConn.Close
    Set objConn = Nothing
    Else
    %>
    'We build a table to put the information into
    <table cellspacing="2" cellpadding="2" border="1" width="100%">
    <%Do Until objRS.EOF%>
    <tr>
    <%For Each x In objRS.Fields%>
    <td><%Response.Write(x.value)%>   </td>
    <%Next
    objRS.MoveNext
    %>
    </tr>
    <%
    Loop
    objRS.Close
    Set objRS = Nothing
    objConn.Close
    Set objConn = Nothing
    End If
    End If
    %>
    </table>


    Hope this helps some.


    Edit: I forgot to objRS.Close so I put that in sorry.

  • 12 years ago

    I think you should study first for the ASP objects and codes. Then you will not need to ask questions like this.


    And I dont think you can understant the code the other guys sent you here.



Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In theory, theory and practice are the same. In practice, they're not.”