Hello everyone, i have done everything fine but still cant update my gridview data when i click on edit button Why ?
error showing is that - Object reference not set to an instance of an object on this line
string id1 =((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text;
Here are my HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" Height="344px" Width="911px"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtedititemname" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
C# code
string str = null;
protected void Page_Load(object sender, EventArgs e)
{
bindgrid();
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select catid,catname from Category_Update", con);
DropDownList1.DataTextField = "catname";
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataValueField = "catid";
DropDownList1.DataBind();
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
str = "~/Product_Images_Update/" + FileUpload1.FileName;
FileUpload1.SaveAs(MapPath(str));
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Admin_Product_Update(id,name,description,price,image)values(@proid,@proname,@prodescrp,@proprice,@str)", con);
cmd.Parameters.AddWithValue("@proid", txtpid.Text);
cmd.Parameters.AddWithValue("@proname", txtpname.Text);
cmd.Parameters.AddWithValue("@prodescrp", txtpdescript.Text);
cmd.Parameters.AddWithValue("@proprice", txtpprice.Text);
cmd.Parameters.AddWithValue("@str", str);
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
lblmsg.Text = "succesful";
txtpname.Text = "";
txtpdescript.Text = "";
txtpprice.Text = "";
txtpid.Text = "";
}
else
{
lblmsg.Text = "Error";
}
bindgrid();
}
else
{
lblmsg.Text = "please select image";
}
}
void bindgrid()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Admin_Product_Update", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindgrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var index = e.RowIndex;
GridView row = GridView1.Rows[index];
string id1 = ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text;
string id = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtedititemname")).Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
con.Open();
string update = "update Admin_Product_Update set name='" + name + "' where id='"+id+"'";
SqlCommand cmd = new SqlCommand(update, con);
DataTable dt = new DataTable();
cmd.ExecuteNonQuery();
GridView1.DataSource = dt;
GridView1.EditIndex = -1;
bindgrid();
con.Close();
}