Hello Sir,
I am using below code to update a gridview column, but its not working.
Gridview rowupdating event database is not getting updated
public partial class payroll_view : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["crm_db"].ConnectionString);
DataBase_Con obj = new DataBase_Con();
update obj_update = new update();
int name, roleID;
int idd;
string name1;
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
public void BindGrid()
{
DataTable dt = new DataTable();
dt = obj_select.select_all_staff_payroll();
GridView1.DataSource = dt; // give data to GridView
GridView1.DataBind();
con.Close();
}
public string GetUrl(object payrollId, object staffname)
{
string role = obj.Encrypt(Convert.ToString(roleID));
string url =
"~/staff_payrollreport.aspx?ID=" + role + "&payrollId=" + Server.UrlEncode(payrollId.ToString());
return url;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string status = (row.Cells[2].Controls[0] as TextBox).Text;
using (SqlCommand cmd = new SqlCommand("UPDATE crm_tbl_staff_payroll SET status = @status WHERE payrollId = @Id "))
{
cmd.Parameters.AddWithValue("@payrollId ", id);
cmd.Parameters.AddWithValue("@status", status);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
GridView1.EditIndex = -1;
BindGrid();
}
}
<asp:updatepanel id="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="row">
<div class="table-responsive">
<asp:GridView ID="GridView1" runat="server" EmptyDataText="No Record Found" class="table table-bordered table-striped table-hover"
AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging"
AllowPaging="True" Width="1024px" CellPadding="3" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" PagerSettings-Mode="NumericFirstLast" AutoGenerateEditButton="true"
OnRowEditing="GridView1_RowEditing" DataKeyNames="payrollId" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating" PageSize="2" RowStyle-HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="payrollId" HeaderText="Id" SortExpression="payrollId" />
<asp:TemplateField HeaderText="Staff Name">
<ItemTemplate>
<asp:HyperLink ID="hlDetails2" runat="server" Text='<%# Eval("staffname") %>'
NavigateUrl='<%# GetUrl(Eval("payrollId"),Eval("staffname"))%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Status" DataField="status" SortExpression="status" />
</Columns>
</asp:GridView>
</div>
</div>
</ContentTemplate>
</asp:updatepanel>