I'm getting a exception when my code runs. below is the error to my problem.
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Web_31dpeqc2
StackTrace:
at Customers.Page_Load(Object sender, EventArgs e) in f:\Aj's Documents\CPDM\IT-5284 ASP.NET\ASP Website\Customers.aspx.cs:line 38
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
Below is the page that generated that error. The error comes when the page loads. The exception error occurs in under the page_load at the lvCustomers.DataSource = dt;
Before putting the updatepanel everything worked correctly now I'm getting this error. What I want the page to do is update only the data table when something is put into the txtSearch box onClick of the Search button.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Customers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
Conn.Open();
string sql = "SELECT LastName + ', ' + FirstName AS 'Customer Name',Email,City,State,Phone, CustomerID FROM Customer ORDER BY LastName, FirstName";
SqlDataAdapter da = new SqlDataAdapter(sql, Conn);
DataTable dt = new DataTable();
da.Fill(dt);
lvCustomers.DataSource = dt;
lvCustomers.DataBind();
dt.Dispose();
Conn.Close();
da.Dispose();
}
protected void btnAddNewCustomer_Click(object sender, EventArgs e)
{
Response.Redirect("CustomerEdit.aspx?Mode=New");
}
protected void btnSearch_Click(object sender, EventArgs e)
{
string stext = txtSearch.Text;
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
Conn.Open();
string sql = "SELECT * FROM CUSTOMER WHERE FirstName LIKE '%@stext%' OR LastName LIKE '%@stext%' OR City LIKE '%@stext%' OR State LIKE '%@stext%'";
SqlCommand cmd = new SqlCommand(sql, Conn);
cmd.Parameters.AddWithValue("@stext", stext);
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
lblConnectionResults.Text = sql;
lvCustomers.DataSource = dt;
lvCustomers.DataBind();
dt.Dispose();
Conn.Close();
da.Dispose();
}
}