<asp:GridView ID="gvDetails" runat="server" DataKeyNames="CategoryID,Name" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" HeaderStyle-BackColor="#61A6F8" HeaderStyle-ForeColor="White" ShowFooter="true" Height="282px" OnRowCancelingEdit="gvDetails_RowCancelingEdit" OnRowCommand="gvDetails_RowCommand" OnRowDeleting="gvDetails_RowDeleting" OnRowEditing="gvDetails_RowEditing" OnRowUpdating="gvDetails_RowUpdating" Width="600px" PageSize="5" ForeColor="White" OnPageIndexChanging="gvDetails_PageIndexChanging"> <Columns> <asp:TemplateField> <EditItemTemplate> <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" /> <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" /> </EditItemTemplate> <ItemTemplate> <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" /> <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" /> </ItemTemplate> <FooterTemplate> <asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" /> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CategoryID"> <EditItemTemplate> <asp:TextBox ID="txtCatID" runat="server" Text='<%#Eval("CategoryID")%>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblCategoryID" runat="server" Text='<%#Eval("CategoryID") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtfCategoryID" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <EditItemTemplate> <asp:TextBox ID="txtName" runat="server" Text='<%#Eval("Name")%>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtfname" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Label ID="Label1" runat="server" Font-Bold="true"></asp:Label>this is the code for .aspx.cs filepublic partial class AddItems : System.Web.UI.Page { SqlConnection con; SqlCommand ecmd, acmd, dcmd; string str = ConfigurationManager.ConnectionStrings["connect"].ConnectionString; LoginView lv; protected void Page_Load(object sender, EventArgs e) { lv = (LoginView)Master.FindControl("HeadLoginView"); if (!lv.Page.User.Identity.IsAuthenticated) { Response.Redirect("~/Account/Login.aspx?ReturnUrl=~/AddItems.aspx"); } else { string lname = lv.Page.User.Identity.Name; if (!lname.Equals("amit")) { Response.Redirect("Default.aspx"); } } if (!IsPostBack) { showDataItem(); } } protected void showDataItem() { con = new SqlConnection(str); con.Open(); SqlCommand cmd = new SqlCommand("select * from Items", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); gvItems.DataSource = ds; gvItems.DataBind(); con.Close(); } protected void gvItems_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("AddNew")) { lblMsg.Text = ""; con = new SqlConnection(str); con.Open(); int IcatID = Convert.ToInt32(((TextBox)gvItems.FooterRow.FindControl("txtfCategoryID")).Text); string Iname = ((TextBox)gvItems.FooterRow.FindControl("txtfname")).Text; FileUpload IImag = (FileUpload)gvItems.FooterRow.FindControl("fuAddImg"); if (IImag.HasFile) { IImag.SaveAs(Server.MapPath("~/Images/Items/") + IImag.FileName); acmd = new SqlCommand("insert into Items(CategoryID,Name,ImageUrl) values (@a,@b,@g)", con); acmd.Parameters.AddWithValue("@a", IcatID); acmd.Parameters.AddWithValue("@b", Iname); acmd.Parameters.AddWithValue("@g", "~/Images/Items/" + IImag.FileName); acmd.ExecuteNonQuery(); con.Close(); } else { lblMsg.Text = "Item category requires an Image"; lblMsg.ForeColor = Color.Red; } showDataItem(); } } protected void gvItems_RowDeleting(object sender, GridViewDeleteEventArgs e) { int getItemID = Convert.ToInt32(gvItems.DataKeys[e.RowIndex].Values["ItemID"].ToString()); con = new SqlConnection(str); con.Open(); dcmd = new SqlCommand("delete from Items where ItemID=@a", con); dcmd.Parameters.AddWithValue("@a", getItemID); int flag = dcmd.ExecuteNonQuery(); if (flag == 1) { lblMsg.Text = "Record Deleted Successfully"; lblMsg.ForeColor = Color.Green; } else { lblMsg.Text = "Deletion UnSuccessfull"; lblMsg.ForeColor = Color.Red; } con.Close(); showDataItem(); } protected void gvItems_RowEditing(object sender, GridViewEditEventArgs e) { gvItems.EditIndex = e.NewEditIndex; } protected void gvItems_RowUpdating(object sender, GridViewUpdateEventArgs e) { } protected void gvItems_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvItems.EditIndex = -1; showDataItem(); } protected void gvItems_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvItems.PageIndex = e.NewPageIndex; } }
public partial class AddItems : System.Web.UI.Page { SqlConnection con; SqlCommand ecmd, acmd, dcmd; string str = ConfigurationManager.ConnectionStrings["connect"].ConnectionString; LoginView lv; protected void Page_Load(object sender, EventArgs e) { lv = (LoginView)Master.FindControl("HeadLoginView"); if (!lv.Page.User.Identity.IsAuthenticated) { Response.Redirect("~/Account/Login.aspx?ReturnUrl=~/AddItems.aspx"); } else { string lname = lv.Page.User.Identity.Name; if (!lname.Equals("amit")) { Response.Redirect("Default.aspx"); } } if (!IsPostBack) { showDataItem(); } } protected void showDataItem() { con = new SqlConnection(str); con.Open(); SqlCommand cmd = new SqlCommand("select * from Items", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); gvItems.DataSource = ds; gvItems.DataBind(); con.Close(); } protected void gvItems_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("AddNew")) { lblMsg.Text = ""; con = new SqlConnection(str); con.Open(); int IcatID = Convert.ToInt32(((TextBox)gvItems.FooterRow.FindControl("txtfCategoryID")).Text); string Iname = ((TextBox)gvItems.FooterRow.FindControl("txtfname")).Text; FileUpload IImag = (FileUpload)gvItems.FooterRow.FindControl("fuAddImg"); if (IImag.HasFile) { IImag.SaveAs(Server.MapPath("~/Images/Items/") + IImag.FileName); acmd = new SqlCommand("insert into Items(CategoryID,Name,ImageUrl) values (@a,@b,@g)", con); acmd.Parameters.AddWithValue("@a", IcatID); acmd.Parameters.AddWithValue("@b", Iname); acmd.Parameters.AddWithValue("@g", "~/Images/Items/" + IImag.FileName); acmd.ExecuteNonQuery(); con.Close(); } else { lblMsg.Text = "Item category requires an Image"; lblMsg.ForeColor = Color.Red; } showDataItem(); } } protected void gvItems_RowDeleting(object sender, GridViewDeleteEventArgs e) { int getItemID = Convert.ToInt32(gvItems.DataKeys[e.RowIndex].Values["ItemID"].ToString()); con = new SqlConnection(str); con.Open(); dcmd = new SqlCommand("delete from Items where ItemID=@a", con); dcmd.Parameters.AddWithValue("@a", getItemID); int flag = dcmd.ExecuteNonQuery(); if (flag == 1) { lblMsg.Text = "Record Deleted Successfully"; lblMsg.ForeColor = Color.Green; } else { lblMsg.Text = "Deletion UnSuccessfull"; lblMsg.ForeColor = Color.Red; } con.Close(); showDataItem(); } protected void gvItems_RowEditing(object sender, GridViewEditEventArgs e) { gvItems.EditIndex = e.NewEditIndex; } protected void gvItems_RowUpdating(object sender, GridViewUpdateEventArgs e) { } protected void gvItems_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvItems.EditIndex = -1; showDataItem(); } protected void gvItems_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvItems.PageIndex = e.NewPageIndex; } }
Attachment: code.rar