Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 11th, 2008, 04:48 PM
Authorized User
 
Join Date: Dec 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Searching gridview results

Hi

I have just finished 'Beginning ASP.NET 3.5 in C#' What a great read. I constructed the PlanetWrox site by following the book and it works great.

I'm now constructing my first web site based on everything I learned from the book.
I have a page with a gridview on bound to sqlDataSource which displays all the records in the database. I need to add a search facility to this with just a simple text box and search button and display the results of the text search in the gridview. I need to search in four columns with the text that is typed in the text box.

I have tried several times to come up with code for his but all I get is the text box and search button. When I enter the text and click search nothing happens. The grid view is wrapped in an update panel like you suggest in the book. The sorting and paging works great but not the search.

Can you suggest any code to rectify this problem that sounds so simple but i'm tearing my hair out.

Thanx in anticipation

Andy
Reply With Quote
  #2 (permalink)  
Old December 12th, 2008, 03:18 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Andy,

Good to hear you like the book so much....

Two things you can try:

1. Remove the UpdatePanel. Maybe an error occurs but the UpdatePanel is hiding it.

2. Post the source for the page (after trying 1.) , together with a description of what it is you're trying to do, how it's supposed to work and so on. Would be helpful to create a test page that exhibits the behavior against the PlanetWrox web site so I can test it out.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old December 12th, 2008, 07:38 AM
Authorized User
 
Join Date: Dec 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Searching gridview results

Hi Imar

Thanx for your prompt reply.
I think I am nearly there. I've took out the update panel as you suggested.The search feature is now working but it will only accept numbers in the text box. If I put in a name (Mark for example) I get an error saying 'Input string was not in a correct format'.

Also if there is no record to display I would like it produce a message saying "There is no record matching your search".

I have included my code for you to look at.

Thank you for your help

Andy

Markup code is:

Code:
<%@PageTitle="Search Test"Language="C#"MasterPageFile="~/MasterPages/MasterPage.master"AutoEventWireup="true"CodeFile="SearchTest.aspx.cs"Inherits="SearchTest" %>
<asp:ContentID="Content1"ContentPlaceHolderID="head"Runat="Server">
</asp:Content>
<asp:ContentID="Content2"ContentPlaceHolderID="cpMainContent"Runat="Server">
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundFieldDataField="FirstName"HeaderText="FirstName"SortExpression="FirstName"/>
<asp:BoundFieldDataField="LastName"HeaderText="LastName"SortExpression="LastName"/>
<asp:BoundFieldDataField="PinCode"HeaderText="PinCode"SortExpression="PinCode"/>
<asp:BoundFieldDataField="Filter"HeaderText="Filter"SortExpression="Filter"/>
<asp:CheckBoxFieldDataField="DeletePending"HeaderText="DeletePending"SortExpression="DeletePending"/>
</Columns>
</asp:GridView>
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
<asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="search"/>
<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:TelephonyAdminConnectionString1 %>"
SelectCommand="SELECT [FirstName], [LastName], [PinCode], [Filter], [DeletePending] FROM [PinEntries] WHERE (([FirstName] = @FirstName) OR ([LastName] = @LastName) OR ([PinCode] = @PinCode) OR ([Filter] = @Filter))">
<SelectParameters>
<asp:ControlParameterControlID="TextBox1"Name="FirstName"PropertyName="Text"Type="String"/>
<asp:ControlParameterControlID="TextBox1"Name="LastName"PropertyName="Text"Type="String"/>
<asp:ControlParameterControlID="TextBox1"Name="PinCode"PropertyName="Text"Type="Int32"/>
<asp:ControlParameterControlID="TextBox1"Name="Filter"PropertyName="Text"Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
Code behind is:

Code:

using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Linq;
using System.Data.SqlClient; 
publicpartialclassSearchTest : BasePage
{
protectedvoid Page_Load(object sender, EventArgs e)
{
 
}
protectedvoid Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.DataBind();
}
}
Reply With Quote
  #4 (permalink)  
Old December 12th, 2008, 03:46 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Because the PinCode column is an integer, you can't simply compare it with a string that doesn't represent an integer. That is, in SQL this:

WHERE PinCode = 'SomeValue'

is illegal and cause an error. The trick is to convert the PinCode column to a nvarchar before you compare it:

.. OR ([LastName] = @LastName) OR (Convert(varchar(20), [PinCode]) = @PinCode) OR ([Filter] = ...

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old December 12th, 2008, 04:03 PM
Authorized User
 
Join Date: Dec 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Searching gridview results

Hi Imar

Thanx for the reply

I tried this but still getting the same error. The PinCode works fine. Its the names that don't work. The search will only accept integers, not words. The search is expecting the input to be an integer. Do I need to convert the other three columns into an integer? Or do I need something to say only accept strings and then convert the integer?

Andy
Reply With Quote
  #6 (permalink)  
Old December 12th, 2008, 04:34 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Looks like you also need to convert the parameter to a String:

<asp:ControlParameter ControlID="TextBox1" Name="PinCode" PropertyName="Text" Type="String"/>

If you post code here, can you please add a few line breaks here and there? The page is now 3 feet wide.... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
jminatel (December 12th, 2008)
  #7 (permalink)  
Old December 12th, 2008, 04:39 PM
Authorized User
 
Join Date: Dec 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar

Sorry about the long lines.

I just tried your suggestion and it works great.

Thankyou ever so much for your help

Andy
Reply With Quote
  #8 (permalink)  
Old December 12th, 2008, 04:42 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're welcome. Glad it's working.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #9 (permalink)  
Old December 12th, 2008, 08:15 PM
jminatel's Avatar
Wrox Staff
Points: 18,940, Level: 59
Points: 18,940, Level: 59 Points: 18,940, Level: 59 Points: 18,940, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2003
Location: Indianapolis, IN, USA.
Posts: 1,945
Thanks: 73
Thanked 139 Times in 101 Posts
Default Test reply - ignore this

Testing reply with quote. Sorry for the test post.

Quote:
Originally Posted by Andy Woodward View Post
Hi Imar

Thanx for your prompt reply.
I think I am nearly there. I've took out the update panel as you suggested.The search feature is now working but it will only accept numbers in the text box. If I put in a name (Mark for example) I get an error saying 'Input string was not in a correct format'.

Also if there is no record to display I would like it produce a message saying "There is no record matching your search".

I have included my code for you to look at.

Thank you for your help

Andy

Markup code is:

Code:
<%@PageTitle="Search Test"Language="C#"MasterPageFile="~/MasterPages/MasterPage.master"AutoEventWireup="true"CodeFile="SearchTest.aspx.cs"Inherits="SearchTest" %>
<asp:ContentID="Content1"ContentPlaceHolderID="head"Runat="Server">
</asp:Content>
<asp:ContentID="Content2"ContentPlaceHolderID="cpMainContent"Runat="Server">
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundFieldDataField="FirstName"HeaderText="FirstName"SortExpression="FirstName"/>
<asp:BoundFieldDataField="LastName"HeaderText="LastName"SortExpression="LastName"/>
<asp:BoundFieldDataField="PinCode"HeaderText="PinCode"SortExpression="PinCode"/>
<asp:BoundFieldDataField="Filter"HeaderText="Filter"SortExpression="Filter"/>
<asp:CheckBoxFieldDataField="DeletePending"HeaderText="DeletePending"SortExpression="DeletePending"/>
</Columns>
</asp:GridView>
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
<asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="search"/>
<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:TelephonyAdminConnectionString1 %>"
SelectCommand="SELECT [FirstName], [LastName], [PinCode], [Filter], [DeletePending] FROM [PinEntries] WHERE (([FirstName] = @FirstName) OR ([LastName] = @LastName) OR ([PinCode] = @PinCode) OR ([Filter] = @Filter))">
<SelectParameters>
<asp:ControlParameterControlID="TextBox1"Name="FirstName"PropertyName="Text"Type="String"/>
<asp:ControlParameterControlID="TextBox1"Name="LastName"PropertyName="Text"Type="String"/>
<asp:ControlParameterControlID="TextBox1"Name="PinCode"PropertyName="Text"Type="Int32"/>
<asp:ControlParameterControlID="TextBox1"Name="Filter"PropertyName="Text"Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
Code behind is:

Code:
using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Linq;
using System.Data.SqlClient; 
publicpartialclassSearchTest : BasePage
{
protectedvoid Page_Load(object sender, EventArgs e)
{
 
}
protectedvoid Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.DataBind();
}
}
__________________
Jim Minatel
Associate Publisher, WROX - A Wiley Brand
Did someone here help you? Click on their post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter results by value in a gridview field cesemj ASP.NET 2.0 Basics 2 February 20th, 2008 11:39 PM
Edit Query Results in Results Grid druid2112 SQL Server 2005 1 June 28th, 2007 08:49 AM
Filter GridView Results by date range srinredd59 ASP.NET 2.0 Basics 0 December 27th, 2006 03:53 PM
Gridview/SqlDataSource won't show results fizzerchris ASP.NET 2.0 Professional 4 September 27th, 2006 02:35 PM
Gridview won't show results fizzerchris ASP.NET 2.0 Basics 0 September 22nd, 2006 06:20 PM



All times are GMT -4. The time now is 05:44 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.