In my web project I am using FormView to Show, Edit and Delete all database details my code is as follow.
and I get "Object reference not set to an instance of an object" error while Editing and Inserting record for following code.
Thanks.
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(); }
Code:
string strCategoryID = ((TextBox)FormView1.Row.FindControl("tbCategoryID")).Text;
Comment