Adding a new row with a button in footer in Gridview
I am trying to add new rows to Grid view using a button in footer, So that I have to see newly added data in grid view and must be saved in database. When I am executing the following code , no errors are coming but nothing is happening. After clicking the button to add with data in text boxes , it is loading and giving me a page with only master page contents, I can't see any grid view over there. If I find the solution for this I can use it for update button also, which there in each row.Following is my code :
Aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="922px"
CellPadding="4" Height="231px" ShowFooter="True" EnableViewState="False" BackColor="White"
BorderColor="#3366CC" BorderStyle="Double" BorderWidth="1px" OnRowDataBound="OnRowDataBound"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField HeaderText="Id" DataField="ID" />
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%# Eval("DeptID") %>' runat="server"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_dept" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="vdept" runat="server" ControlToValidate="txt_dept"
Text="?" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Location") %>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_loc" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="vloc" runat="server" ControlToValidate="txt_loc"
Text="?" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Inactive") %>' Visible="false" />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddl_inactive" runat="server">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Active</asp:ListItem>
<asp:ListItem>Inactive</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Created Date" DataField="CreatedDate" />
<asp:BoundField HeaderText="Created By" DataField="CreatedById" />
<asp:BoundField HeaderText="Updated Date" DataField="UpdatedDate" />
<asp:BoundField HeaderText="Updated By" DataField="UpdatedByID" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" CommandName="EditRow" Text="Save" CommandArgument='<%# Eval("DeptID") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" CommandName="AddNew" Text="Add New Record">
</asp:Button>
</FooterTemplate>
</asp:TemplateField>
</Columns>
Code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txt_dept = (TextBox)GridView1.FooterRow.FindControl("txt_dept");
TextBox txt_loc = (TextBox)GridView1.FooterRow.FindControl("txt_loc");
//DropDownList ddl_inactive = (DropDownList)GridView1.FooterRow.FindControl("ddl_inactive");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "Insert Into dbo.tblTBT_Depts(DeptID,Location) Values(@DeptID,@Location),con";
cmd.Parameters.Add("@DeptID", SqlDbType.VarChar, 15);
cmd.Parameters["@DeptID"].Value = txt_dept.Text;
//cmd.Parameters.Add("@Inactive", SqlDbType.VarChar, 50);
//cmd.Parameters["@Inactive"].Value = ddl_inactive.SelectedValue;
cmd.Parameters.Add("@Location", SqlDbType.VarChar, 50);
cmd.Parameters["@Location"].Value = txt_loc.Text;
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
FillGrid();
lblmsg.Text = txt_dept.Text + " Added successfully...... ";
}
else
{
lblmsg.Text = txt_dept.Text + " Error while adding row.....";
}
}