Dear Sir,
I want to update function in GridView with button.
While user click on update button then data updated in table
I have make this but its not working.
please help me sir.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="COMP_ID" GridLines="Vertical" OnRowUpdating="OnRowUpdating"
OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="COMP_ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("COMP_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emloyee ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("EMP_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="COMP_MESSAGE">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("COMP_MESSAGE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="COMP_LODGE_DATE">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("COMP_LODGE_DATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATUS_NAME">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assign_to">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remark">
<ItemTemplate>
<asp:TextBox ID="TextBox5" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" Text="Update" runat="server" CommandName="Save" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" Text="Closed" runat="server" CommandName="Closed" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=ocms_admin;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
refreshdata();
}
}
public void refreshdata()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from comp_box", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
DropDownList DropDownList1 = (e.Row.FindControl("DropDownList1") as DropDownList);
SqlCommand cmd = new SqlCommand("select * from complaint_status", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "STATUS_NAME";
DropDownList1.DataValueField = "STATUS_NAME";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--STATUS_NAME--", "0"));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
DropDownList DropDownList1 = (e.Row.FindControl("DropDownList2") as DropDownList);
SqlCommand cmd = new SqlCommand("select * from Assign_to", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "STATUS_NAME";
DropDownList1.DataValueField = "STATUS_NAME";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--SELECT_NAME--", "0"));
}
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int COMP_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string STATUS_ID = (row.Cells[5].Controls[0] as TextBox).Text;
string CLOSING_DATE = (row.Cells[6].Controls[0] as TextBox).Text;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE comp_box SET STATUS_ID = @STATUS_ID, CLOSING_DATE = @CLOSING_DATE WHERE COMP_ID = @COMP_ID"))
{
cmd.Parameters.AddWithValue("@STATUS_ID", STATUS_ID);
cmd.Parameters.AddWithValue("@CLOSING_DATE", CLOSING_DATE);
cmd.Parameters.AddWithValue("@CLOSING_DATE", CLOSING_DATE);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
//this.BindGrid();
}
}