Skip to content
Loading
  • +2
    Dear Experts !! am working on Asp.net,c# using entity framework
     
    In my project, i have a Registration Form. and also i have User Table in database.
     
    So i need to create a Usernames as Unique.
     
    Suppose if the User Registered with one name and the other users should not create with this name.
     
    So , i need a Message like Already Exists with this Name, Try another name.
     
    Please Suggest me, and help me, How to do this ?
    +2
  • Jenith Thakkar
    +2
    If you checked my code properly then you notice that what I do their 

    I have matched entered username with the database username if both are same then return 1 else 0 so if it retuns a 0 then you just show you message
    +2
  • +2
    i am bit confused.can u please tell me..where should i use this senerio....
    here is my register page..
     
    Aspx code..

    Registration

    Style="top:182px; left: 774px; position: absolute; height: 26px; width: 162px"
    ControlToValidate="TxtName" ErrorMessage="Name required**" font-size="Small"
    ForeColor ="Red">
    Style="top: 224px; left: 774px; position: absolute; height: 26px; width: 162px"
    ControlToValidate="TxtUserName" ErrorMessage="Username required** " font-size="Small"
    ForeColor="Red">
    Style="top:271px; left: 774px; position: absolute; height: 26px; width: 162px"
    ControlToValidate="TxtEmailID" ErrorMessage="Valid EmailID required" font-size="Small"
    ForeColor="Red">
    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">
    Image:
    Name:
    UserName:
    EmailID:
    Password:
    Confirm Password:
    onclick="Button1_Click" />
     Aspx.cs..
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.Entity;
    using System.Data.SqlClient;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    using WalPaper.Model;
    namespace WalPaper
    {
    public partial class WebForm2 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    Model.User newusermodel = new Model.User();
    newusermodel.Image = TxtImg.ToString();
    newusermodel.Name = TxtName.Text;
    newusermodel.UserName = TxtUserName.Text;
    newusermodel.EmailID = TxtEmailID.Text;
    newusermodel.Created_Date = DateTime.Now;
    newusermodel.User_Type_Id = 3;
    newusermodel.Password = TxtPassword.Text;
    using (WallpaperEntities3 context = new WallpaperEntities3())
    {
    User user = new User();
    {
    string str = TxtImg.FileName;
    TxtImg.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));
    string Image = "~/Upload/" + str.ToString();
    string name = TxtImg.ToString();
    user.Image = newusermodel.Image;
    user.Name = newusermodel.Name;
    user.UserName = newusermodel.UserName;
    user.EmailID = newusermodel.EmailID;
    user.Created_Date = newusermodel.Created_Date;
    user.User_Type_Id = newusermodel.User_Type_Id;
    user.Password = newusermodel.Password;
    };
    var usersadd = context.Set();
    usersadd.Add(user);
    context.SaveChanges();
    string message = "Your details have been saved successfully.";
    string script = "window.onload = function(){ alert('";
    script += message;
    script += "')};";
    ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
    Response.Redirect("/Login.aspx");
    }
    }
    }
    }
     
    +2
  • +2
    its doesnot add new user but the problem is it doesnt show any message of User exists or not.
     
     
     
    var usersadd = context.Set();
    var response = context.Users.Where(u => u.UserName == newusermodel.UserName).FirstOrDefault();
    if(response == null)
    {
    usersadd.Add(user);
    context.SaveChanges();
    }
    else
    {
    string messagebox = response != null ? "New User" : "Existing Username";
    }
     
    +2
  • +2
    Its working now..i just simply take a label and print message on it..Thanks Everyone For Ur Help...
    +2
  • Jenith Thakkar
    +2
    Okay please mark as a answer if it's helpful for you so it would helpful for me also
    +2
  • Jay Krishna Reddy
    +1
    Pass the Current post value to check with the Database users table and if a user exists with the same name we won't allow saving
    1. Public string ValidateUserName(string username)  
    2. {  
    3.    string finalresult = string.Empty;  
    4.    var result = db.context.table.Where(c=>c.username.Equals(username)).firstordefault();  
    5. return finalresult = result!= null ? "username Saved" : "Already Exists ,Try Another one";  
    6. }  
    else saved.
    +1
  • Jenith Thakkar
    0
    FOr that you can check passed username parameter like

    1. public int DoOperation (UserDetails user)  
    2. {  
    3.    int userexist = 0;   
    4.    var data =db.context.users.where(u=>u.username==user.username).firstordefault();  
    5.    if(data!=null)  
    6.    {  
    7.       userexist =1;   
    8.    }   
    9.    else  
    10.    {  
    11.       userexist = 0;   
    12.    }
    13.    return userexist;   
    14. }   
    Thank you
    0