Guest User

Guest User

  • Tech Writer
  • 271
  • 32.4k

edit data in gridview asp.net entity framework

Aug 25 2020 4:50 AM
hello..
 
i am trying to edit the data in gridview....
 
my issues are:
 
redirect to same page and also i am geting a id which record i want to edit. but i want in fileupload which i already save image to be selected and also in dropdown the selected category shown when i redirect...
  1. <form id="form1" runat="server">  
  2. <div>  
  3. <table class="style1">  
  4. <tr>  
  5. <td>  
  6. <asp:FileUpload ID="fileUpload1" runat="server" />  
  7. </td>  
  8. </tr>  
  9. <tr>  
  10. <td>Category:</td>  
  11. <td>  
  12. <asp:DropDownList runat="server" ID="ddlWallCategry" OnSelectedIndexChanged="ddlWallCategry_SelectedIndexChanged"  
  13. AutoPostBack="true" >  
  14. </asp:DropDownList></td>  
  15. </tr>  
  16. </table>  
  17. </div>  
  18. <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CssClass="btsave" />  
  19. <asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click" CssClass="btsave" />  
  20. <asp:GridView ID="GridViewCategory" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"  
  21. AllowPaging="True"  
  22. OnPageIndexChanging="GridView1_PageIndexChanging"  
  23. OnRowEditing="GridViewCategory_RowEditing"  
  24. OnRowUpdating="GridViewCategory_RowUpdating"  
  25. OnRowDeleting ="GridViewCategory_RowDeleting">  
  26. <Columns>  
  27. <asp:TemplateField HeaderText="CategoryName" ItemStyle-Width="150">  
  28. <ItemTemplate>  
  29. <asp:Label ID="lblCategoryName" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label>  
  30. </ItemTemplate>  
  31. <EditItemTemplate>  
  32. <asp:TextBox ID="TxtCategoryName" runat="server" Text='<%# Eval("CategoryName") %>'></asp:TextBox>  
  33. </EditItemTemplate>  
  34. </asp:TemplateField>  
  35. <asp:ImageField DataImageUrlField="Thumbnail" HeaderText="Thumbnail">  
  36. <ControlStyle Height="100px" Width="100px" />  
  37. </asp:ImageField>  
  38. <asp:TemplateField HeaderText="Action">  
  39. <ItemTemplate>  
  40. <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit"  
  41. ImageUrl="~/img/edit.png" PostBackUrl= '<%# "WallPaperRecord.aspx?id="+ Eval("Id") %>'  
  42. Height="32px" Width="32px"/>  
  43. <asp:ImageButton ID="imgbtndel" runat="server" CommandName="delete"  
  44. ImageUrl="~/img/delete.png" Height="32px" Width="32px"/>  
  45. </ItemTemplate>  
  46. </asp:TemplateField>  
  47. </Columns>  
  48. </asp:GridView>  
  49. </form>  
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. Int32 ID = Convert.ToInt32(Request.QueryString["Id"]);  
  4. if (ID == null || ID == 0)  
  5. {  
  6. }  
  7. else  
  8. {  
  9. editmode();  
  10. }  
  11. if (!IsPostBack)  
  12. {  
  13. bindUsingCategory(); //bind dropdown from database  
  14. bindCategory(); //bind gridview  
  15. }  
  16. }  
  17. public void bindUsingCategory() //bind dropdown from database  
  18. {  
  19. using (WallpaperEntities13 context = new WallpaperEntities13())  
  20. {  
  21. ddlWallCategry.DataSource = (from r in context.WallPaperCategries select new { CategoryName = r.CategoryName, Id = r.Id }).ToList();  
  22. ddlWallCategry.DataTextField = "CategoryName";  
  23. ddlWallCategry.DataValueField = "Id";  
  24. ddlWallCategry.DataBind();  
  25. ddlWallCategry.Items.Insert(0, new System.Web.UI.WebControls.ListItem());  
  26. }  
  27. }  
  28. private void bindCategory() //bind gridview  
  29. {  
  30. using (WallpaperEntities13 context = new WallpaperEntities13())  
  31. {  
  32. GridViewCategory.DataSource = context.WallpaperRecords.ToList();  
  33. GridViewCategory.DataBind();  
  34. }  
  35. }  
  36. protected void ddlWallCategry_SelectedIndexChanged(object sender, EventArgs e)  
  37. {  
  38. using (WallpaperEntities13 context = new WallpaperEntities13())  
  39. {  
  40. var Category = (from em in context.WallpaperRecords  
  41. where em.CategoryId == ddlWallCategry.SelectedValue  
  42. select new  
  43. {  
  44. em.Id,  
  45. em.CategoryName,  
  46. em.Thumbnail,  
  47. }).ToList();  
  48. ddlWallCategry.DataTextField = "CategoryName";  
  49. ddlWallCategry.DataValueField = "Id";  
  50. GridViewCategory.DataSource = Category;  
  51. GridViewCategory.DataBind();  
  52. }|  
  53. }  
  54. protected void editmode()  
  55. {  
  56. Int32 ID = Convert.ToInt32(Request.QueryString["CategoryName"]);  
  57. using (WallpaperEntities13 ctx = new WallpaperEntities13())  
  58. {  
  59. btnupdate.Visible = true;  
  60. btnSave.Visible = false ;  
  61. var Category = int.Parse(Request["Id"]);  
  62. var query = (from c in ctx.WallpaperRecords  
  63. where c.Id == Category  
  64. select new  
  65. {  
  66. c.CategoryName,  
  67. c.Id,  
  68. c.CategoryId,  
  69. c.Thumbnail  
  70. }).FirstOrDefault();  
  71. if (query != null)  
  72. {  
  73. lblCategoryName = query.CategoryName.ToString();  
  74. ddlWallCategry.SelectedValue = query.CategoryId;  
  75. ctx.SaveChanges();  
  76. GridViewCategory.EditIndex = -1;  
  77. ddlWallCategry. DataBind(); //getting error in this line...ddlWallCategry' has a SelectedValue which is invalid because it does not exist in the list of items.  
  78. }  
  79. ddlWallCategry.SelectedValue = "";  
  80. }  
  81. }  
  82. protected void btnupdate_Click(object sender, EventArgs e)  
  83. {  
  84. using (var context = new WallpaperEntities13())  
  85. {  
  86. var Categorys = int.Parse(Request["Id"]);  
  87. string Category = Convert.ToString(Session["CategoryName"]);  
  88. var std = (from x in context.WallpaperRecords  
  89. where x.CategoryName == Category  
  90. select x).FirstOrDefault();  
  91. if (std != null)  
  92. {  
  93. std.CategoryName = lblCategoryName.ToString();  
  94. context.SaveChanges();  
  95. GridViewCategory.EditIndex = -1;  
  96. DataBind();  
  97. }  
  98. }  
  99. }  

Answers (1)