Skip to content
Loading
show grid view on select of admin from dropdown list asp.net C# E.F  
  •   
  •   
  • class="style1">  
  •   
  •  Image:  
  •   
  • "TxtImg" runat="server">  
  •   
  •   
  •   
  •  Name:  
  •   
  • "TxtName" runat="server">  
  •   
  • "RequiredFieldValidator1" runat="server"  
  • Style="top:182px; left: 774px; position: absolute; height: 26px; width: 162px"  
  • ControlToValidate="TxtName" ErrorMessage="Name required**" font-size="Small"  
  • ForeColor ="Red">  
  •   
  •   
  • UserName:  
  •   
  • "TxtUserName" runat="server">  
  •   
  • "RequiredFieldValidator2" runat="server"  
  • Style="top: 224px; left: 774px; position: absolute; height: 26px; width: 162px"  
  • ControlToValidate="TxtUserName" ErrorMessage="Username required** " font-size="Small"  
  • ForeColor="Red">  
  •   
  •   
  • EmailID:  
  •   
  • "TxtEmailID" runat="server">  
  •   
  • "RequiredFieldValidator3" runat="server"  
  • Style="top:271px; left: 774px; position: absolute; height: 26px; width: 162px"  
  • ControlToValidate="TxtEmailID" ErrorMessage="Valid EmailID required" font-size="Small"  
  • ForeColor="Red">  
  •   
  •   
  • UserRole:  
  •   
  • "DdlUser" runat="server">  
  • "0">---Select Role--  
  • --- Admin--  
  • ---User--  
  • ---EndUser--  
  •   
  •   
  •   
  •   
  • Password:  
  •   
  • "TxtPassword" runat="server" TextMode="Password" >  
  •   
  • "RequiredFieldValidator5" runat="server" TextMode="Password"  
  • Style="top:321px; left: 774px; position: absolute; height: 26px; width: 162px"  
  • ControlToValidate="TxtPassword" ErrorMessage="It contains 1-special character and 0-9 numbers required" font-size="Small"  
  • ForeColor="Red">  
  •   
  •   
  • Confirm Password:  
  •   
  • "TxtConfirmPassword" runat="server" TextMode="Password">   
  •   
  •   
  •   
  • "LblMesge" runat="server" >  
  •   
  •    
  •   
  •   
  •   
  • "Button1" runat="server" Text="Save" class="btn"  
  • onclick="Button1_Click" />  
  •   
  •   
  • 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();  
    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. }  

    2 Replies

    Know the answer? Post it — somebody with the same question will find it here.