if (e.Row.RowType == DataControlRowType.DataRow)
            {

                ImageButton edit_btn = new ImageButton();
                edit_btn.ID = "editbtn" + e.Row.RowIndex + "_" + gridColumnNames_mastercodes;
                edit_btn.ImageUrl = "images/edit.png";
                edit_btn.CommandName = "Edit";
                //edit_btn.Click += new ImageClickEventHandler(grd_mastercodes_EditRow);
                e.Row.Cells[0].Controls.Add(edit_btn);
}

public void grd_mastercodes_EditRow(object sender, GridViewEditEventArgs e)
    {
        try
        {
            grd_mastercodes.EditIndex = e.NewEditIndex;
            binddata();
        }
        catch (Exception ex)
        {
            string msg = ex.Message.ToString();
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('" + msg + "');", true);
        }
    }

If i click the edit button it's showing as "Object Reference not set to an instant object"

But i need as "When i click edit button it should come to edit mode"

protected void updateRow(object sendedr, GridViewUpdateEventArgs e)
    {
        try
        {
            var Vendor = grd_mastercodes.DataKeys[e.RowIndex].Value;

            GridViewRow row = grd_mastercodes.Rows[e.RowIndex] as GridViewRow;

            TextBox tSearchTerm = row.FindControl("txt_searchterm") as TextBox;
            TextBox tvendorname = row.FindControl("txt_vendorname") as TextBox;
            TextBox tstreet = row.FindControl("txt_street") as TextBox;
            TextBox city = row.FindControl("txt_city") as TextBox;
            TextBox tcountry = row.FindControl("txt_country") as TextBox;
            TextBox tpostbox = row.FindControl("txt_postbox") as TextBox;
            TextBox tcurrency = row.FindControl("txt_currency") as TextBox;
            TextBox tsalesperson = row.FindControl("txt_salesperson") as TextBox;
            TextBox tphnum = row.FindControl("txt_telephnum") as TextBox;
            TextBox text2 = row.FindControl("txt_ext1") as TextBox;
            TextBox text1 = row.FindControl("txt_ext2") as TextBox;
            TextBox tminval = row.FindControl("txt_minval") as TextBox;
            TextBox tpostcode = row.FindControl("txt_postcode") as TextBox;
            TextBox tgroup = row.FindControl("txt_group") as TextBox;

            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                string sql = "UPDATE ArR_VendorMaster SET [SearchTerm] = @Searchterm, [Vendor_Name] = @vendorname, [Street] = @street, [City] = @city, [Country] = @country, [PO_Box] = @postbox, [Currency] = @currency, [Sales_Person] = @salesperson, [Telephone_Number] = @telephoneNumber, [Ext_01] = @ext1, [Ext_02] = @ext2, [Minimum_Value] = @minimumValue, [Post_Code] = @postcode, [Group] = @group" + " WHERE Vendor = @vendor";

                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@Searchterm", tSearchTerm.Text.Trim());
                    cmd.Parameters.AddWithValue("@vendorname", tvendorname.Text.Trim());
                    cmd.Parameters.AddWithValue("@street", tstreet.Text.Trim());
                    cmd.Parameters.AddWithValue("@city", city.Text.Trim());
                    cmd.Parameters.AddWithValue("@country", tcountry.Text.Trim());
                    cmd.Parameters.AddWithValue("@postbox", tpostbox.Text.Trim());
                    cmd.Parameters.AddWithValue("@currency", tcurrency.Text.Trim());
                    cmd.Parameters.AddWithValue("@salesperson", tsalesperson.Text.Trim());
                    cmd.Parameters.AddWithValue("@telephoneNumber", tphnum.Text.Trim());
                    cmd.Parameters.AddWithValue("@ext1", text1.Text.Trim());
                    cmd.Parameters.AddWithValue("@ext2", text2.Text.Trim());
                    cmd.Parameters.AddWithValue("@minimumValue", tminval.Text.Trim());
                    cmd.Parameters.AddWithValue("@postcode", tpostcode.Text.Trim());
                    cmd.Parameters.AddWithValue("@group", tgroup.Text.Trim());
                    cmd.Parameters.AddWithValue("@vendor", Vendor);

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }


            grd_mastercodes.EditIndex = -1;
            this.binddata();
        }

        catch (Exception ex)
        {
            string msg = ex.Message.ToString();
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('" + msg + "');", true);
        }
    }

And also i don't no how to add update button if a add means it displaying both a button same time

<asp:GridView ID="grd_mastercodes" runat="server" HeaderStyle-BackColor="#f79646" HeaderStyle-ForeColor="White"
            HeaderStyle-Font-Bold="false" Font-Names="Calibri"
            Font-Size="Medium" RowStyle-BackColor="White" HeaderStyle-BorderColor="Black"
            AutoGenerateColumns="False" RowStyle-BorderColor="Black" DataKeyNames="Vendor" OnRowDataBound="grd_mastercodes_DataBound" OnRowCreated="grd_mastercodes_RowCreated" OnRowUpdating="updateRow" OnRowEditing="grd_mastercodes_EditRow" >
        </asp:GridView>

is there any solution to do like below

if (e.row.rowstate == datacontrolrowstate.edit)

Same problem facing while try to add new data in footer template as "Object reference not set to an instant of object"

Related posts

Recent Viewed