Click here to Skip to main content
15,885,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 131: protected void Button4_Click1(object sender, EventArgs e)
Line 132: {
Line 133: if(dr.HasRows)
Line 134: {
Line 135: dr.Read();


ASP code

XML
<table><tr>
           <td>
       <asp:TextBox ID="TextBox1" runat="server" placeholder="Search id"></asp:TextBox>
               </td>
               <td>

                   <asp:Button ID="Button4" runat="server" Text="Go" OnClick="Button4_Click1" />
               </td>
           <td>
       <asp:Button ID="Button3" runat="server" Text="Delete" OnClientClick="javascript:validateCheckBoxes()" OnClick="Button3_Click"/>
       </td>
                   </tr>
           <tr>
               <td>

                   <asp:Label ID="Label1" runat="server" ForeColor="red"></asp:Label>
               </td>
           </tr>
       </table>




C# code


C#
private void bind()
    {

        cmd = new SqlCommand("select * from getezeeregister where username like '" + TextBox1.Text + "%'",con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }

    protected void Button4_Click1(object sender, EventArgs e)
    {
        if(dr.HasRows)
        {
            dr.Read();
            bind();
            GridView1.Visible = true;
            TextBox1.Text = "";
            Label1.Text = "";

        }
        else
        {
            GridView1.Visible = false;
            Label1.Visible = true;
            Label1.Text = "Searched records "+TextBox1.Text+" is not avilable";

        }

    }
Posted
Comments
Herman<T>.Instance 28-Aug-14 4:01am    
where is your exception? at line if (dr.HasRows)? Where is defined what it is. You do not read data in that method with it. I see you use DataSet and Not SqlDataReader
ChintanShukla 28-Aug-14 4:06am    
Where is dr defined?
Member 10918596 28-Aug-14 4:09am    
public partial class register : System.Web.UI.Page
{
SqlCommand cmd;
SqlConnection con = new SqlConnection(*******);
SqlDataAdapter da;
DataSet ds;
DataTable dt;
SqlDataReader dr;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}
Button3.Attributes.Add("onclick", "javascript:return Confirmationbox()");

}
[no name] 28-Aug-14 4:11am    
You need to go back to basics and review what classes are, what instantiating objects are and variable scope.
ChintanShukla 28-Aug-14 4:21am    
instantiate all objects e.g. DataSet ds = new DataSet(); and yes you need to study more.

1 solution

Use this updated Code:

C#
private void bind()
    {

        cmd = new SqlCommand("select * from getezeeregister where username like '" + TextBox1.Text + "%'",con);
        da = new SqlDataAdapter(cmd);
        //ds = new DataSet();
        //da.Fill(ds);
        //GridView1.DataSource = ds;
    //GridView1.DataMember = ds.Tables[0];
        //GridView1.DataBind();

     ds = new DataSet();
         da.Fill(ds, "getezeeregister");

         // Bind the data table to the data grid
         dataGrid1.SetDataBinding(ds, "getezeeregister");
    }

    protected void Button4_Click1(object sender, EventArgs e)
    {
        if(dr.HasRows)
        {
            dr.Read();
            bind();
            GridView1.Visible = true;
            TextBox1.Text = "";
            Label1.Text = "";

        }
        else
        {
            GridView1.Visible = false;
            Label1.Visible = true;
            Label1.Text = "Searched records "+TextBox1.Text+" is not avilable";

        }

    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900