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 November 17th, 2003, 02:23 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default Checking for a entry in access

Hi guys,

A person can serach my database via a form, the problem is when the thing that the person is searching for is not in the database. When this happens, all the records are shown in my datagrid. I dont want this to happen. If the thing is not in the database I want the person to be told this. How would I go about doing this.
Here is my code: -

private void Button_Click(object sender, System.EventArgs e)
    {
       String strAuthor = Author.Text;
       String strTitle = Title.Text;

       Connect();
       String strSearch = "SELECT * FROM Computing";
       OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSearch, objConnection);
       DataSet ds = new DataSet();
       objAdapter.Fill(ds, "dtFilteredComputing");
       Disconnect();

       DataView objDataView = new DataView(ds.Tables["dtFilteredComputing"]);
       objDataView.RowFilter = "BookAuthor LIKE '" + strAuthor + "' OR BookName LIKE '" + strTitle + "'";

       dgComputing.DataSource = objDataView;
       dgComputing.DataBind();
       }
    }

Thanks



Adz - Portsmouth Massive
__________________
Adz - Learning The J2EE Ways.
 
Old November 17th, 2003, 02:53 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Because this is an ASP.Net application and you are making a call to the DB every time you search, I would recommend putting the search criteria into you SQL statement:

private void Button_Click(object sender, System.EventArgs e)
    {
       String strAuthor = Author.Text;
       String strTitle = Title.Text;

       Connect();
       String strSearch = String.Format("SELECT * FROM Computing WHERE BookAuthor LIKE '{0}' OR BookName LIKE '{1}'", strAuthor, strTitle);
       OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSearch, objConnection);
       DataSet ds = new DataSet();
       objAdapter.Fill(ds, "dtFilteredComputing");
       Disconnect();

       DataView objDataView = new DataView(ds.Tables["dtFilteredComputing"]);

       dgComputing.DataSource = objDataView;
       dgComputing.DataBind();
       }
    }

I would further recommend looking into use a parameterized query using the Parameters collection of a command object. This takes care of the escape charactesr for you and makes building queries a bit cleaner. I'd offer up an example, but I haven't work with the OLEDB .Net classes. Sorry.

If you aren't familiar with the String.Format method, look into it. It's handy to use when you need to build small string concatenations (3 or less parts to add in).

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 17th, 2003, 06:26 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

Peter,

why would you have to use Parameterization with Command objects when the select statement does all the filtering. Is there any need for it??




Adz - Portsmouth Massive
 
Old November 17th, 2003, 06:43 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

Peter,

I left out a line after creating the dataview object: -

DataView objDataView = new DataView(ds.Tables["dtFilteredComputing"]);
objDataView.RowFilter = "BookAuthor LIKE '" + strAuthor + "' OR BookName LIKE '" + strTitle + "'";

Is this the type of thing you were refering to. Could you please show me an eample using SQLClient objects??




Adz - Portsmouth Massive
 
Old November 17th, 2003, 10:49 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Even though you are letting SQL do all the work, you still need to provide it with something useful to filter, hence the parameters.

Here's an example of parameters:

Dim objConn As New SqlConnection(sMyConnString)
Dim objCommand As New SqlCommand
Dim objDataReader As SqlDataReader

objConn.Open()
objCommand.Connection = objConn
objCommand.CommandType = CommandType.Text
objCommand.CommandText = "SELECT * FROM Users WHERE Username=@sUserName AND Password=@sPassword"
objCommand.Parameters.Add("@sUserName", SqlDbType.VarChar).Value = sUserName
objCommand.Parameters.Add("@sPassword", SqlDbType.VarChar).Value = sPassword
objDataReader = objCommand.ExecuteReader
If objDataReader.Read() Then
    'Found a record ...
End If


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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking for duplicate entry in Database sharon5469 ASP.NET 1.0 and 1.1 Basics 1 November 18th, 2007 06:08 PM
Always a New entry scandalous Access 3 January 26th, 2007 04:02 PM
Checking for duplicate primary key in Access Indo77 ASP.NET 2.0 Basics 2 November 29th, 2006 12:19 PM
Checking A Value In Access DB marcopolo_8 Classic ASP Databases 1 January 5th, 2005 10:47 PM
Text Entry to Access DB hoffmann Access ASP 1 December 21st, 2003 07:42 PM





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