Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 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 30th, 2007, 12:18 AM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Searching a Grid View

I am trying to search a GridView by ID to allow the user to edit information they entered into a database if they have their correct ID number.

I receive the error that this line of code in the back end:
Code:
SqlDataSource2.SelectParameters["ID"].DefaultValue = ((TextBox)form1.FindControl("search")).Text;
System.NullReferenceException: Object reference not set to an instance of an object.

I am kinda at a loss of what to do

The following is the front end:
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="rsvp.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RSVP</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
<asp:TextBox ID="search" runat="server"></asp:TextBox><br>
            <asp:Button ID="Button1" UseSubmitBehavior="True" runat="server" Text="Button" OnClick="Button1_Click" /><br>

            <br>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
                <Columns>
                    <asp:CommandField ShowSelectButton="True" />
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                    <asp:BoundField DataField="Attending" HeaderText="Attending" SortExpression="Attending" />
                    <asp:BoundField DataField="NumberAttending" HeaderText="NumberAttending" SortExpression="NumberAttending" />
                    <asp:BoundField DataField="Reception" HeaderText="Reception" SortExpression="Reception" />
                    <asp:BoundField DataField="DateEdited" HeaderText="DateEdited" SortExpression="DateEdited" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:tsharpConnectionString %>"
                SelectCommand="SELECT * FROM [RSVP] WHERE ([ID] LIKE '%' + ? + '%')">
                <SelectParameters>
                    <asp:ControlParameter ControlID="search" DefaultValue="" Name="forename" PropertyName="Text"
                        Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
        </div>
    </form>
</body>
</html>
And my back end:

Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataSource2.SelectParameters["ID"].DefaultValue = ((TextBox)form1.FindControl("search")).Text;
        GridView1.DataBind();

    }

}
 
Old November 30th, 2007, 08:08 AM
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

well.. the parameter name is forename, no ID... but probably the error is that you are not finding that textbox named "search"

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old November 30th, 2007, 12:20 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Both of those were correct, but I ended up binding the text box to a control parameter through the Visual Web Developer GUI. Somehow it all worked wonderfully!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Grid view roopeshmkatlive.in ASP.NET 1.0 and 1.1 Professional 1 August 1st, 2008 01:03 PM
Help Grid View Drop Down List, view all jskinner123 ASP.NET 2.0 Basics 0 November 25th, 2007 06:25 PM
grid view komalpriya .NET Framework 2.0 3 November 7th, 2007 10:34 AM
grid view MunishBhatia ASP.NET 2.0 Professional 2 June 5th, 2007 12:15 AM
grid view MunishBhatia ASP.NET 2.0 Professional 1 May 25th, 2007 07:26 AM





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