Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 October 26th, 2006, 01:50 PM
d5t d5t is offline
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Searching through a database

I'm trying to bring up data for a specific person when I search for their first and last name displayed in table. I'm getting a blank data table when I search-- only the rows names appear. What am I doing incorrectly?

Code:
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script language="C#" runat="server" >

    void Page_Load()
    {

     if (Page.IsPostBack)
     {
    OleDbConnection objConnection;
    OleDbDataAdapter objCommand;
    String strConnect;
    String strCommand;
    DataSet DataSet1 = new DataSet ();

    strConnect = @"Provider=Microsoft.Jet.OLEDB.4.0;";
    strConnect += @"Data Source= D:\Reservations.mdb;";
    FirstName.Text = "";
    LastName.Text = "";
    strCommand = "SELECT Date, RoomNo, TimeIn, TimeOut 
FROM Contacts, Reserves WHERE Contacts.ContactID 
= Reserves.ContactID AND FirstName 
= ' " + FirstName.Text + 
" ' AND LastName = ' " + LastName.Text + " ' ";

    objConnection = new OleDbConnection(strConnect);
    objCommand = new OleDbDataAdapter(strCommand, objConnection);
    objCommand.Fill(DataSet1, "Reserves");
    DataGrid1.DataSource = DataSet1.Tables["Reserves"].DefaultView;
    DataGrid1.DataBind();

    }
    }
</script>
<html>
<head><title></title>
<form runat="server">

   Enter first name: <asp:TextBox id="FirstName" runat="server"></asp:TextBox>
   Enter last name: <asp:TextBox id="LastName" runat="server"></asp:TextBox><br />
   <br />
   <input type="submit" value="Search" /><br /><br />
   <td> </td>
   <asp:DataGrid id="DataGrid1" EnableViewState="False" runat="server"></asp:DataGrid>

</head>
<body>
</form>
</body>
</html>
 
Old October 26th, 2006, 02:20 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

This is a SQL problem. *I think*

SELECT Date, RoomNo, TimeIn, TimeOut
FROM Contacts, Reserves WHERE Contacts.ContactID
= Reserves.ContactID AND FirstName
= ' " + FirstName.Text +
" ' AND LastName = ' " + LastName.Text + " ' ";

Which of the above columns exist in the Contacts and Reserves table? I am making an assumption that all the columns belong to the Reserves table.

SELECT
r.Date, r.RoomNo, r.TimeIn, r.TimeOut
FROM
Reserves r
INNER JOIN Contacts c
ON c.ContactID = r.ContactID
WHERE
c.FirstName = ' " + FirstName.Text +
" ' AND c.LastName = ' " + LastName.Text + " ' ";

On a more personal note, I like my where clauses to be based on an ID as opposed to a name (You can have duplicate names in your database, IDs are unique)

In anycase that should work for you.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 26th, 2006, 02:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

firstname and lastname are empty strings, do you have contacts with no name in the table?? (not null, just empty strings)...

HTH

Gonzalo
 
Old October 26th, 2006, 06:14 PM
d5t d5t is offline
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, all the contacts have names--nothing is empty. Reserves holds the LastName and FirstName, maybe I'm doing something wrong with the DataSource and Bind. I'll try the different SQL command and see if it works.

I have three tables in the database Reserves:

Reserves
Contacts <--- has first and last name
ROOMS
 
Old October 26th, 2006, 09:24 PM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

d5t:
Matching firstname and lastname has always been a headache unless you have control over how the names are created and maintained in the database. The first- and last- names much match exactly. For example if the name fields are in all caps in the database, i.e., M T PROMISES, and in the prompts the user enters, mt Promises or m t Promises, you will not get a match. There is also a problem with the name MC Call or McCall. Then also if you have several names that are the same, when this happens an initial is thrown in with the first name. A possible solution: Match on the primary and foreign key fields and display the info accordingly.

Hope this helps.
 
Old October 27th, 2006, 12:11 AM
d5t d5t is offline
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the tip peace95, I'll try debugging it tomorrow. Throw in any other advice.

 
Old October 27th, 2006, 07:10 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

As I said, I prefer searching based on ID as opposed to name since an ID is (or should) be unique.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature





Similar Threads
Thread Thread Starter Forum Replies Last Post
searching a database mark jonas MySQL 13 January 19th, 2007 10:01 AM
help with Database searching europhreak Classic ASP Basics 1 February 8th, 2006 07:30 AM
Searching the database...HELP! Zedman BOOK: Beginning ASP 3.0 6 September 30th, 2004 09:52 AM
Searching trough a database toni_montana PHP How-To 2 September 25th, 2004 09:17 AM
searching a database shabazzk Classic ASP Databases 2 March 10th, 2004 06:56 PM





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