注册时为用户提供唯一的用户名 [英] Providing unique username to a user while registration

查看:107
本文介绍了注册时为用户提供唯一的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sir / mam

我的名字是hemanth我是一个新鲜的

我的要求是当用户第一次输入他们的详细信息时他们应该收到一条消息(你已成功注册)如果任何其他用户输入相同的用户名(其他用户已经),他应该收到用户名存在的消息,请选择其他用户。

这是我的要求。



i编写的代码如



Sir/mam
my name is hemanth i'am a fresher
and my requirement is when user enter their details for first time they should get a message (you have successfully registered)if any other user enter same username (which other user has already) he should get the message like username exists please select other one.
this is my requirement.

i have written the code like

using System.Data.SqlClient;
using System.Configuration;
public partial class Reg : System.Web.UI.Page
{
    string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            con.Open();
            string cmdstr = "select count(*) from tblregistration where UserId ='" + txtuserid.Text + "'";
            SqlCommand existinguser = new SqlCommand(cmdstr, con);
            int temp = Convert.ToInt32(existinguser.ExecuteScalar().ToString());
            if (temp == 1)
            {
                Response.Write("UserId already Exists select other one");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
        con.Open();
        string insert = "insert into tblregistration(Name,UserId,Email,Password,Confirmpassword) values (@Name,@UserId,@Email,@Password,@Confirmpassword)";
        SqlCommand cmd = new SqlCommand(insert, con);
    
        cmd.Parameters.AddWithValue("@Name", txtname.Text);
        cmd.Parameters.AddWithValue("@UserId", txtuserid.Text);
        cmd.Parameters.AddWithValue("@Email", txtemail.Text);
        cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
        cmd.Parameters.AddWithValue("@Confirmpassword", txtcpassword.Text);


        try
        {
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Newlogin.aspx");
        }
        catch (Exception Ex)
        {
            Response.Write("Some thing Wrong");
        }
        finally
        {
 
        }
    }
}



当我调试我的解决方案数据时正在存储,但它没有检测到现有的用户名。



提前谢谢


when i debugging my solution data is storing but it's not detecting the existing username.

Thanks in advance

推荐答案

只需做一个验证方法检查它是否是有效用户然后将数据插入数据库



Just do one validation method check if it is a valid user then make a insert data to database

bool validation()
  {
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
      con.Open();
      string cmdstr = "select count(*) from tblregistration where UserId ='" + txtuserid.Text + "'";
      SqlCommand existinguser = new SqlCommand(cmdstr, con);
      int temp = Convert.ToInt32(existinguser.ExecuteScalar().ToString());
      if (temp == 1)
      {
          Response.Write("UserId already Exists select other one");
          return false;
      }
      else return true;
  }
  protected void Button1_Click(object sender, EventArgs e)
  {//If your validation is perfect then insert data to database
      if (validation() == true)
      {
          SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
          con.Open();
          string insert = "insert into tblregistration(Name,UserId,Email,Password,Confirmpassword) values (@Name,@UserId,@Email,@Password,@Confirmpassword)";
          SqlCommand cmd = new SqlCommand(insert, con);

          cmd.Parameters.AddWithValue("@Name", txtname.Text);
          cmd.Parameters.AddWithValue("@UserId", txtuserid.Text);
          cmd.Parameters.AddWithValue("@Email", txtemail.Text);
          cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
          cmd.Parameters.AddWithValue("@Confirmpassword", txtcpassword.Text);

          try
          {
              cmd.ExecuteNonQuery();
              con.Close();
              Response.Redirect("Newlogin.aspx");
          }
          catch (Exception Ex)
          {
              Response.Write("Some thing Wrong");
          }
          finally
          {

          }
      }
  }


这篇关于注册时为用户提供唯一的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆