i'm facing the error while i'm developing search againest database with the help of autocompletion ,when user enters the name and clicks the go button i'm trying to pull the data bbut i have this error...none of my google results solve my problem .can any one solve this
Null reference exception was handled by user code
" Object reference not set to an instance of an object."
public partial class search1 : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from Candidate1 where name = '"+Session["name"].ToString()+"'", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
}
Thanks and Regards
Loading
Posted Aug 3, 2013, 10:15 AM
Just wanted to know, exactly where're you assigning the username into the session.
In your code, i couldn't see that lin.
I'm expecting something like,
Session["name"] =
Before passing the name into the sql,it is safer to a null check.
string name = Convert.ToString(Session["name"]);
if (name == "" or string.IsNullOrEmpty(name))
throw new Exception("Name not found");
Hope this helps you !
Iftikar HussainPosted Aug 3, 2013, 9:43 AM
if you can share your screen, then use this
https://join.me/
Regards,
Iftikar
aditya immadiPosted Aug 3, 2013, 9:12 AM
sorry for the inconviniance...i'm ssendig the picture please check the attachment
Iftikar HussainPosted Aug 3, 2013, 9:00 AM
Regards,
Iftikar
aditya immadiPosted Aug 3, 2013, 8:49 AM
this is mmy generic handler i.e; search.ashx
<%@ WebHandler Language="C#" Class="Search" %>
using System;
using System.Data.SqlClient;
using System.Text;
using System.Web;
public class Search : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
string searchText = context.Request.QueryString["q"];
SqlConnection con = new SqlConnection("Data Source=FOODASURA-PC\\MYSQLAPCAMP;Initial Catalog=Apcampaign;Persist Security Info=True;User ID=sa;Password=123");
con.Open();
SqlCommand cmd = new SqlCommand("select id,name,image from Candidate1 where name Like @Search + '%'", con);
cmd.Parameters.AddWithValue("@Search",searchText);
StringBuilder sb = new StringBuilder();
using(SqlDataReader dr=cmd.ExecuteReader())
{
while(dr.Read())
{
sb.Append(string.Format("{0},{1}{2}", dr["name"], dr["image"], Environment.NewLine));
}
}
con.Close();
context.Response.Write(sb.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}
and my search.aspx is
<%@ Page Title="Search Results" Language="C#" MasterPageFile="~/main.Master" AutoEventWireup="true" CodeBehind="search.aspx.cs" Inherits="ApComapaign.search1" %>
and my search.aspx.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ApComapaign
{
public partial class search1 : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
object temp = Session["name"];
SqlDataAdapter da = new SqlDataAdapter("select * from Candidate1 where name = '"+Session["name"].ToString()+"'", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
}
}
thanksand and Regards
Iftikar HussainPosted Aug 3, 2013, 8:44 AM
object temp=Session["name"];
Regards,
Iftikar
aditya immadiPosted Aug 3, 2013, 8:41 AM
Iftikar HussainPosted Aug 3, 2013, 8:16 AM
object temp=Session["name"];
Regards,
Iftikar
aditya immadiPosted Aug 3, 2013, 7:45 AM
aditya immadiPosted Aug 3, 2013, 5:55 AM
the error is
Object reference not set to an instance of an object.
i think everythig is fie
{
public partial class search1 : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from Candidate1 where name = '"+Session["name"].ToString()+"'", cn); at this line
Object reference not set to an instance of an object.
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
}
}
Thanks and Regards
Iftikar HussainPosted Aug 3, 2013, 3:44 AM
namespace ApComapaign
{
public partial class master : System.Web.UI.MasterPage
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_search_Click(object sender, EventArgs e)
{
Session["name"]=txtSearch.Text;
Response.Redirect("search.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}
Regards,
Iftikar
aditya immadiPosted Aug 3, 2013, 3:36 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ApComapaign
{
public partial class master : System.Web.UI.MasterPage
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_search_Click(object sender, EventArgs e)
{
//txtSearch.Text = Session["name"].ToString();
Response.Redirect("search.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
}
and i need search results in separate page so i take another page as search.aspx
in search.aspx we have
namespace ApComapaign
{
public partial class search1 : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from Candidate1 where name = '"+Session["name"].ToString()+"'", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
}
}
Iftikar HussainPosted Aug 3, 2013, 3:18 AM
Where you have assigned the session variable Session["name"]? Can you share the complete code?
Regards,
Iftikar
Sanjeeb LenkaPosted Aug 3, 2013, 3:18 AM
Your datalist is present in child page or in master page.
as my code is written for child page and both textbox and button should present in child page then it will work.
why you use textbox in master page..
aditya immadiPosted Aug 3, 2013, 3:11 AM
my search box is in masterpage .actually i used generic handler for this ,the handler accepts request from JQuery in a Query string parameter "q". By passing query string parameter as search text and get user details based on that search text.
when i tried your code
it shows error as datalist1 is ot ffound in current contet
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
Thanks and Regards
Sanjeeb LenkaPosted Aug 1, 2013, 2:19 PM
try like this as you have to search on button click try like this. here i am providing code for search if user enter any name it will search and bind data if not it will bind all the data. 1st on pageload it will bind all the data.
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.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindName();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
BindName();
}
private void BindName()
{
SqlDataAdapter da;
if (txtName.Text != string.Empty)
da = new SqlDataAdapter("select * from Candidate1 where name = '" + txtName.Text + "'", cn);
else
da = new SqlDataAdapter("select * from Candidate1", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
}
try this code and i am little bit confuse why you use session for search as you have to search on textbox value
Prashant YadavPosted Aug 1, 2013, 1:30 PM