Sooraj Verma

Sooraj Verma

  • NA
  • 45
  • 1.1k

delete a row using view state variable its not working

Sep 8 2018 2:03 AM
//client side
<asp:UpdatePanel ID="UpdatePanel1" ViewStateMode="Enabled" UpdateMode="Always" runat="server">
<ContentTemplate>
<asp:GridView ID="gvResumeManage" Width="1050px" CssClass="GetGrid" runat="server" BackColor="White" OnRowCommand="OnRowCommand" AutoGenerateColumns="false"
GridLines="none" CellPadding="5" PageSize="15" AllowPaging="true">
<Columns>
<asp:TemplateField HeaderText = "S.N" ItemStyle-Width="70">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ResumeUserId" ItemStyle-Width="120" HeaderText="Id" />
<asp:BoundField DataField="ResumeName" ItemStyle-Width="220" HeaderText="Name" />
<asp:BoundField DataField="ResumeEmail" ItemStyle-Width="130" HeaderText="Email" />
<asp:BoundField DataField="ResumeContact" ItemStyle-Width="80" HeaderText="Contact" />
<asp:BoundField DataField="ResumeDate" ItemStyle-Width="130" HeaderText="Time" />
<asp:TemplateField ItemStyle-Width="80" HeaderText="Manage">
<ItemTemplate>
<asp:Button ID="btndelete" CssClass="delete" CommandName="delete" CommandArgument='<%# Eval("ResumeId") %>' ToolTip="Delete" runat="server" Text="" />
<asp:Button ID="btndownload" CssClass="view" CommandName="download" CommandArgument='<%# Eval("ResumeFileName") %>' ToolTip="Download" runat="server" Text="" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="GridHeader" />
<AlternatingRowStyle BackColor="#f1f1f1"/>
<PagerStyle CssClass="GridPager" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
 
 
 
 
 
 
//server side 
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
myConn.Open();
SqlCommand cmd = myConn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "tblResumeUploadABind";
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(dt);
ViewState["resume"] = dt;
gvResumeManage.DataSource = dt;
gvResumeManage.DataBind();
}
}
 
 
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
DataTable dtt = (DataTable)ViewState["resume"];
if (e.CommandName == "delete")
{
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int RowIndex = row.RowIndex;
int id = Convert.ToInt32(e.CommandArgument);
if (ViewState["resume"] != null)
{
myConn.Open();
SqlCommand cmd = myConn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "tblResumeUploadADelete";
cmd.Parameters.Add(new SqlParameter("@id", Convert.ToString(id)));
cmd.ExecuteNonQuery();
myConn.Close();
dtt = (DataTable)ViewState["resume"];
dtt.Rows.Remove(dtt.Rows[RowIndex]);
gvResumeManage.DataSource = dtt;
gvResumeManage.DataBind();
ViewState["resume"] = dtt;
}
}
}
 

Answers (1)