Object reference not set to an instance of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mahyog
    New Member
    • Sep 2010
    • 9

    Object reference not set to an instance of an object

    In my web project I am using FormView to Show, Edit and Delete all database details my code is as follow.

    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FormView1.AllowPaging = true;
                FormView1.PagerSettings.PageButtonCount = 15;
    
                BindFormView();
            }
        }
    
        private void BindFormView()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NWConnString"].ToString()))
            {
                DataSet dsCategory = new DataSet();
    
                string strSelectCmd = "SELECT CategoryID,CategoryName,Description FROM Categories";
    
                SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn);
    
                conn.Open();
    
                da.Fill(dsCategory, "Category");
    
    
                FormView1.DataSource = dsCategory;
                FormView1.DataBind();
            }
        }
    
        protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {
            FormView1.PageIndex = e.NewPageIndex;
    
            BindFormView();
        }
    
        protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NWConnString"].ToString()))
            {
                SqlCommand cmd = new SqlCommand();
    
                cmd.Connection = conn;
    
                cmd.CommandText = "INSERT INTO Categories ( CategoryID, CategoryName, Description, Picture ) VALUES ( @CategoryID, @CategoryName, @Description, @Picture )";
    
                cmd.CommandType = CommandType.Text;
    
                string strCategoryID = ((TextBox)FormView1.Row.FindControl("tbCategoryID")).Text;
                string strCategoryName = ((TextBox)FormView1.Row.FindControl("tbCategoryName")).Text;
                string strDescription = ((TextBox)FormView1.Row.FindControl("tbDescription")).Text;
    
                cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = Convert.ToInt32(strCategoryID);
                cmd.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 50).Value = strCategoryName;
                cmd.Parameters.Add("@Description", SqlDbType.NText).Value = strDescription;
    
                FileUpload uploadPicture = (FileUpload)FormView1.FindControl("uploadPicture");
    
                if (uploadPicture.HasFile)
                {
                    cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = uploadPicture.FileBytes;
                }
                else
                {
                    cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = DBNull.Value;
                }
    
                conn.Open();
    
                cmd.ExecuteNonQuery();
            }
    
            FormView1.ChangeMode(FormViewMode.ReadOnly);
    
            BindFormView();
        }
    
        protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NWConnString"].ToString()))
            {
                SqlCommand cmd = new SqlCommand();
    
                cmd.Connection = conn;
    
                cmd.CommandText = "UPDATE Categories SET CategoryName = @CategoryName, Description = @Description, Picture = ISNULL(@Picture,Picture) WHERE CategoryID = @CategoryID";
    
                cmd.CommandType = CommandType.Text;
    
                string strCategoryID = ((Label)FormView1.Row.FindControl("lblCategoryID")).Text;
                string strCategoryName = ((TextBox)FormView1.Row.FindControl("tbCategoryName")).Text;
                string strDescription = ((TextBox)FormView1.Row.FindControl("tbDescription")).Text;
    
                cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = strCategoryID;
                cmd.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 50).Value = strCategoryName;
                cmd.Parameters.Add("@Description", SqlDbType.NText).Value = strDescription;
    
                FileUpload uploadPicture = (FileUpload)FormView1.FindControl("uploadPicture");
    
                if (uploadPicture.HasFile)
                {
                    cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = uploadPicture.FileBytes;
                }
                else
                {
                    cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = DBNull.Value;
                }
    
                conn.Open();
    
                cmd.ExecuteNonQuery();
            }
    
            FormView1.ChangeMode(FormViewMode.ReadOnly);
    
            BindFormView();
        }
    
        protected void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NWConnString"].ToString()))
            {
                SqlCommand cmd = new SqlCommand();
    
                cmd.Connection = conn;
    
                cmd.CommandText = "DELETE FROM Categories WHERE CategoryID = @CategoryID";
    
                cmd.CommandType = CommandType.Text;
    
                string strCategoryID = ((Label)FormView1.Row.FindControl("lblCategoryID")).Text;
    
                cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value = strCategoryID;
    
                conn.Open();
    
                cmd.ExecuteNonQuery();
            }
    
            BindFormView();
        }
    
        protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
        {
            FormView1.ChangeMode(e.NewMode);
    
            BindFormView();
        }
    and I get "Object reference not set to an instance of an object" error while Editing and Inserting record for following code.

    Code:
                string strCategoryID = ((TextBox)FormView1.Row.FindControl("tbCategoryID")).Text;
    Thanks.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    It sounds like the FindControl method failed to find a TextBox with the ID "tbCategory ID".

    Double check that the ID of TextBox in your FormView1 template is "tbCategory ID".

    -Frinny

    Comment

    • Mahyog
      New Member
      • Sep 2010
      • 9

      #3
      Yes thats the mistake I have made.
      Thanks.

      Comment

      Working...