Guest User

Guest User

  • Tech Writer
  • 271
  • 36.6k

bind dropdown selected Id in asp.net

Aug 7 2020 5:01 AM
Hello..
 
Admin = 1
User= 2
EndUser= 3
 
i already give values to them and bind it.
 
now when someone is register usertypeid = selected value of dropdown..
 
here is my code..
  1. <table class="style1">  
  2. <tr>  
  3. <td> Image:</td>  
  4. <td>  
  5. <asp:FileUpload ID="FileImg" runat="server"></asp:FileUpload>  
  6. </td>  
  7. </tr>  
  8. <tr>  
  9. <td> Name:</td>  
  10. <td>  
  11. <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>  
  12. </td>  
  13. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  
  14. Style="top:182px; left: 774px; position: absolute; height: 26px; width: 162px"  
  15. ControlToValidate="TxtName" ErrorMessage="Name required**" font-size="Small"  
  16. ForeColor ="Red"></asp:RequiredFieldValidator>  
  17. </tr>  
  18. <tr>  
  19. <td>UserName:</td>  
  20. <td>  
  21. <asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>  
  22. </td>  
  23. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"  
  24. Style="top: 224px; left: 774px; position: absolute; height: 26px; width: 162px"  
  25. ControlToValidate="TxtUserName" ErrorMessage="Username required** " font-size="Small"  
  26. ForeColor="Red"></asp:RequiredFieldValidator>  
  27. </tr>  
  28. <tr>  
  29. <td>EmailID:</td>  
  30. <td>  
  31. <asp:TextBox ID="TxtEmailID" runat="server"></asp:TextBox>  
  32. </td>  
  33. <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"  
  34. Style="top:271px; left: 774px; position: absolute; height: 26px; width: 162px"  
  35. ControlToValidate="TxtEmailID" ErrorMessage="Valid EmailID required" font-size="Small"  
  36. ForeColor="Red"></asp:RequiredFieldValidator>  
  37. </tr>  
  38. <tr>  
  39. <td>UserRole:</td>  
  40. <td>  
  41. <asp:dropdownlist runat="server" id="ddlDropdown" OnSelectedIndexChanged="ddlDropdown_SelectedIndexChanged" >  
  42. </asp:dropdownlist></td>  
  43. </tr>  
  44. <tr>  
  45. <td>Password:</td>  
  46. <td>  
  47. <asp:TextBox ID="TxtPassword" runat="server" TextMode="Password" ></asp:TextBox>  
  48. </td>  
  49. <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" TextMode="Password"  
  50. Style="top:321px; left: 774px; position: absolute; height: 26px; width: 162px"  
  51. ControlToValidate="TxtPassword" ErrorMessage="It contains 1-special character and 0-9 numbers required" font-size="Small"  
  52. ForeColor="Red"></asp:RequiredFieldValidator>  
  53. </tr>  
  54. <tr>  
  55. <td>Confirm Password:</td>  
  56. <td>  
  57. <asp:TextBox ID="TxtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox> </td>  
  58. </tr>  
  59. <tr>  
  60. <td>  
  61. <asp:Label ID="LblMesge" runat="server" ></asp:Label>  
  62. </td>  
  63. <td> </td>  
  64. </tr>  
  65. </table>  
  66. </div>  
  67. <asp:Button ID="Button1" runat="server" Text="Save" class="btn"  
  68. onclick="Button1_Click" />  
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. bindUsingEntityFrameWork();  
  4. }  
  5. public void bindUsingEntityFrameWork()  
  6. {  
  7. using (WallpaperEntities5 context = new WallpaperEntities5())  
  8. {  
  9. ddlDropdown.DataSource = (from r in context.UserTypes select new {Role = r.Role, Role_Id = r.Role_Id }).ToList();  
  10. ddlDropdown.DataTextField = "Role";  
  11. ddlDropdown.DataValueField = "Role_Id";  
  12. ddlDropdown.DataBind();  
  13. ddlDropdown.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Option""0"));  
  14. }  
  15. }  
  16. protected void Button1_Click(object sender, EventArgs e)  
  17. {  
  18. Model.User newusermodel = new Model.User();  
  19. string str = FileImg.FileName;  
  20. FileImg.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));  
  21. string Image = "~/Upload/" + str.ToString();  
  22. newusermodel.Image = Image;  
  23. newusermodel.Name = TxtName.Text;  
  24. newusermodel.UserName = TxtUserName.Text;  
  25. newusermodel.EmailID = TxtEmailID.Text;  
  26. newusermodel.Created_Date = DateTime.Now;  
  27. newusermodel.User_Type_Id =  
  28. newusermodel.Password = TxtPassword.Text;  
  29. using (WallpaperEntities5 context = new WallpaperEntities5())  
  30. {  
  31. User user = new User();  
  32. {  
  33. string name = FileImg.ToString();  
  34. user.Image = newusermodel.Image;  
  35. user.Name = newusermodel.Name;  
  36. user.UserName = newusermodel.UserName;  
  37. user.EmailID = newusermodel.EmailID;  
  38. user.Created_Date = newusermodel.Created_Date;  
  39. user.User_Type_Id = newusermodel.User_Type_Id;  
  40. user.Password = newusermodel.Password;  
  41. };  
  42. var usersadd = context.Set<User>();  
  43. var response = context.Users.Where(u => u.UserName == newusermodel.UserName).FirstOrDefault();  
  44. if (response == null)  
  45. {  
  46. usersadd.Add(user);  
  47. context.SaveChanges();  
  48. Response.Redirect("/Login.aspx");  
  49. }  
  50. }  
  51. }  

Answers (6)