Guest User

Guest User

  • Tech Writer
  • 271
  • 36k

show grid view on select of admin from dropdown list asp.net C# E.F

Jul 22 2020 6:33 AM
dear Experts,
 
how could i display all users when i select Admin from my dropdown and i am using entity framework..
 
here is my code..
  1. <body>  
  2. <form id="form1" runat="server">  
  3. <div>  
  4. <header id="header">  
  5. <div class="intro text-center">  
  6. <div class="overlay">  
  7. <div class="container">  
  8. <div class="row">  
  9. <div class="intro-text">  
  10. <h1> <span class="brand">Registration</span></h1>  
  11. </div>  
  12. </div>  
  13. </div>  
  14. </div>  
  15. </div>  
  16. </header>  
  17. <table class="style1">  
  18. <tr>  
  19. <td> Image:</td>  
  20. <td>  
  21. <asp:FileUpload ID="TxtImg" runat="server"></asp:FileUpload>  
  22. </td>  
  23. </tr>  
  24. <tr>  
  25. <td> Name:</td>  
  26. <td>  
  27. <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>  
  28. </td>  
  29. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  
  30. Style="top:182px; left: 774px; position: absolute; height: 26px; width: 162px"  
  31. ControlToValidate="TxtName" ErrorMessage="Name required**" font-size="Small"  
  32. ForeColor ="Red"></asp:RequiredFieldValidator>  
  33. </tr>  
  34. <tr>  
  35. <td>UserName:</td>  
  36. <td>  
  37. <asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>  
  38. </td>  
  39. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"  
  40. Style="top: 224px; left: 774px; position: absolute; height: 26px; width: 162px"  
  41. ControlToValidate="TxtUserName" ErrorMessage="Username required** " font-size="Small"  
  42. ForeColor="Red"></asp:RequiredFieldValidator>  
  43. </tr>  
  44. <tr>  
  45. <td>EmailID:</td>  
  46. <td>  
  47. <asp:TextBox ID="TxtEmailID" runat="server"></asp:TextBox>  
  48. </td>  
  49. <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"  
  50. Style="top:271px; left: 774px; position: absolute; height: 26px; width: 162px"  
  51. ControlToValidate="TxtEmailID" ErrorMessage="Valid EmailID required" font-size="Small"  
  52. ForeColor="Red"></asp:RequiredFieldValidator>  
  53. </tr>  
  54. <tr>  
  55. <td>UserRole:</td>  
  56. <td>  
  57. <asp:DropDownList ID="DdlUser" runat="server">  
  58. <asp:ListItem Value ="0">---Select Role--</asp:ListItem>  
  59. <asp:ListItem >--- Admin--</asp:ListItem>  
  60. <asp:ListItem>---User--</asp:ListItem>  
  61. <asp:ListItem >---EndUser--</asp:ListItem>  
  62. </asp:DropDownList>  
  63. </td>  
  64. </tr>  
  65. <tr>  
  66. <td>Password:</td>  
  67. <td>  
  68. <asp:TextBox ID="TxtPassword" runat="server" TextMode="Password" ></asp:TextBox>  
  69. </td>  
  70. <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" TextMode="Password"  
  71. Style="top:321px; left: 774px; position: absolute; height: 26px; width: 162px"  
  72. ControlToValidate="TxtPassword" ErrorMessage="It contains 1-special character and 0-9 numbers required" font-size="Small"  
  73. ForeColor="Red"></asp:RequiredFieldValidator>  
  74. </tr>  
  75. <tr>  
  76. <td>Confirm Password:</td>  
  77. <td>  
  78. <asp:TextBox ID="TxtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox> </td>  
  79. </tr>  
  80. <tr>  
  81. <td>  
  82. <asp:Label ID="LblMesge" runat="server" ></asp:Label>  
  83. </td>  
  84. <td> </td>  
  85. </tr>  
  86. </table>  
  87. </div>  
  88. <asp:Button ID="Button1" runat="server" Text="Save" class="btn"  
  89. onclick="Button1_Click" />  
  90. </form>  
  91. </body>  
Aspx code::
  1. public partial class WebForm2 : System.Web.UI.Page  
  2. {  
  3. protected void Page_Load(object sender, EventArgs e)  
  4. {  
  5. }  
  6. protected void Button1_Click(object sender, EventArgs e)  
  7. {  
  8. Model.User newusermodel = new Model.User();  
  9. newusermodel.Image = TxtImg.ToString();  
  10. newusermodel.Name = TxtName.Text;  
  11. newusermodel.UserName = TxtUserName.Text;  
  12. newusermodel.EmailID = TxtEmailID.Text;  
  13. newusermodel.Created_Date = DateTime.Now;  
  14. newusermodel.User_Role = DdlUser.SelectedValue.ToString();  
  15. newusermodel.User_Type_Id = 3;  
  16. newusermodel.Password = TxtPassword.Text;  
  17. using (WallpaperEntities4 context = new WallpaperEntities4())  
  18. {  
  19. User user = new User();  
  20. {  
  21. string str = TxtImg.FileName;  
  22. TxtImg.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));  
  23. string Image = "~/Upload/" + str.ToString();  
  24. string name = TxtImg.ToString();  
  25. user.Image = newusermodel.Image;  
  26. user.Name = newusermodel.Name;  
  27. user.UserName = newusermodel.UserName;  
  28. user.EmailID = newusermodel.EmailID;  
  29. user.User_Role = newusermodel.User_Role;  
  30. user.Created_Date = newusermodel.Created_Date;  
  31. user.User_Type_Id = newusermodel.User_Type_Id;  
  32. user.Password = newusermodel.Password;  
  33. };  
  34. var usersadd = context.Set<User>();  
  35. var response = context.Users.Where(u => u.UserName == newusermodel.UserName).FirstOrDefault();  
  36. if(response == null)  
  37. {  
  38. usersadd.Add(user);  
  39. context.SaveChanges();  
  40. Response.Redirect("/Login.aspx");  
  41. }  
  42. else  
  43. {  
  44. LblMesge.Text = "User exists";  
  45. }  
  46. }  
  47. }  
  48. }  

Answers (2)