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();
}
}