p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 11th, 2008, 04:48 PM
Authorized User
Points: 45, Level: 1
Points: 45, Level: 1 Points: 45, Level: 1 Points: 45, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old December 12th, 2008, 03:18 AM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 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
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old December 12th, 2008, 07:38 AM
Authorized User
Points: 45, Level: 1
Points: 45, Level: 1 Points: 45, Level: 1 Points: 45, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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();
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old December 12th, 2008, 03:46 PM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 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
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old December 12th, 2008, 04:03 PM
Authorized User
Points: 45, Level: 1
Points: 45, Level: 1 Points: 45, Level: 1 Points: 45, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old December 12th, 2008, 04:34 PM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 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
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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
Points: 45, Level: 1
Points: 45, Level: 1 Points: 45, Level: 1 Points: 45, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old December 12th, 2008, 04:42 PM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
Default

You're welcome. Glad it's working.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #9 (permalink)  
Old December 12th, 2008, 08:15 PM
jminatel's Avatar
Wrox Staff
Points: 7,285, Level: 36
Points: 7,285, Level: 36 Points: 7,285, Level: 36 Points: 7,285, Level: 36
Activity: 17%
Activity: 17% Activity: 17% Activity: 17%
 
Join Date: May 2003
Location: Indianapolis, IN, USA.
Posts: 1,349
Thanks: 27
Thanked 49 Times in 40 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
Wiley Technology Publishing
WROX Press
Blog: http://p2p.wrox.com/content/blogs/jminatel
Wrox online library: http://wrox.books24x7.com
Wrox on Twitter: http://twitter.com/wrox
Did someone here help you? Click on their post!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 09: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 03:35 PM
Gridview won't show results fizzerchris ASP.NET 2.0 Basics 0 September 22nd, 2006 07:20 PM



All times are GMT -4. The time now is 01:13 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc