检查用户名或用户电子邮件地址已存在 [英] checking user name or user email already exists

查看:211
本文介绍了检查用户名或用户电子邮件地址已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个简单的注册页面,用户无法进入同一个用户名或电子邮件的工作,我做了一个code是prevent从输入用户名用户和它的工作,但是当我试着以prevent从entring相同的用户名或电子邮件没有工作的用户。

和我的问题是,我怎样才能添加另一个条件,用户无法进入已经存在的电子邮件吗?

我试图做这个code,但我以前不工作:

 保护无效Button_Click(对象发件人,EventArgs的发送)
{
  SqlConnection的CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [的ConnectionString]的ConnectionString);
  CMD1的SqlCommand =新的SqlCommand(从表中选择1,其中名称= @用户名,CON);
  的SqlCommand CMD2 =新的SqlCommand(从表中选择1,其中电子邮件= @ USEREMAIL,CON);  con.Open();
  cmd1.Parameters.AddWithValue(@用户名,Name_id.Text);
  cmd2.Parameters.AddWithValue(@ USEREMAIL,Email_id.Text);  使用(VAR DR1 = cmd1.ExecuteReader())
  {
    如果(dr1.HasRows)
    {
      Label1.Text =用户名已经存在;
    }
    使用(VAR DR2 = cmd2.ExecuteReader())
    {
      如果(dr2.HasRows)
      {
        Label1.Text =电子邮件已存在;
      }
      其他
      {
        dr1.Close();
        dr2.Close();
        //添加新用户
        con.Close();
      }
    }
  }
}

但我得到这个错误:


  

有已经使用此命令,必须先关闭相关联的打开的DataReader。



解决方案

就像我说在我的评论你的设计是坏的!

首先,你应该有数据访问层。这应该是大的解决方案项目,但在你的情况,你可以把它像新的目录。在这个目录下创建SqlManager类这里是code:

 公共类SqlManager
{    公共静态字符串的ConnectionString
    {
        得到
        {
            返回ConfigurationManager.ConnectionStrings [DevConnString]的ConnectionString。
        }
    }    公共静态的SqlConnection GetSqlConnection(CMD的SqlCommand)
    {
        如果(cmd.Connection == NULL)
        {
            康涅狄格州的SqlConnection =新的SqlConnection(ConnectionString中);            conn.Open();            cmd.Connection =康恩;            康涅狄格州返回;
        }        返回cmd.Connection;
    }    公共静态INT的ExecuteNonQuery(CMD的SqlCommand)
    {
        康涅狄格州的SqlConnection = GetSqlConnection(CMD);        尝试
        {
            返回cmd.ExecuteNonQuery();
        }
        抓住
        {
            扔;
        }
        最后
        {
            conn.Close();
        }
    }    公共静态对象的ExecuteScalar(CMD的SqlCommand)
    {        康涅狄格州的SqlConnection = GetSqlConnection(CMD);        尝试
        {
            返回cmd.ExecuteScalar();
        }
        抓住
        {
            扔;
        }
        最后
        {
            conn.Close();
        }
    }    公共静态数据集GetDataSet(CMD的SqlCommand)
    {
        返回GetDataSet(CMD,表);
    }    公共静态数据集GetDataSet(CMD的SqlCommand,串defaultTable)
    {
        康涅狄格州的SqlConnection = GetSqlConnection(CMD);        尝试
        {
            数据集resultDst =新的DataSet();            使用(SqlDataAdapter的适配器=新SqlDataAdapter的(CMD))
            {
                adapter.Fill(resultDst,defaultTable);
            }            返回resultDst;
        }
        抓住
        {
            扔;
        }
        最后
        {
            conn.Close();
        }
    }
    公共静态的DataRow GetDataRow(CMD的SqlCommand)
    {
        返回GetDataRow(CMD,表);
    }    公共静态的DataRow GetDataRow(CMD的SqlCommand,串defaultTable)
    {
        康涅狄格州的SqlConnection = GetSqlConnection(CMD);        尝试
        {
            数据集resultDst =新的DataSet();            使用(SqlDataAdapter的适配器=新SqlDataAdapter的(CMD))
            {
                adapter.Fill(resultDst,defaultTable);
            }            如果(resultDst.Tables.Count大于0&放大器;&放大器; resultDst.Tables [0] .Rows.Count大于0)
            {
                返回resultDst.Tables [0] .Rows [0];
            }
            其他
            {
                返回null;
            }
        }
        抓住
        {
            扔;
        }
        最后
        {
            conn.Close();
        }
    }
}

之后,你应该有业务层对象。在更大的解决方案是在您的案件目录中的项目。如果您在页面TaxesEdit.aspx,你应该添加在BO(业务对象)Tax.cs类。

为类方法例如,对于第一个按钮:

 公共数据集GetTaxesByUserName(用户名字符串)
{
     CMD的SqlCommand =新的SqlCommand(@        从表1中选择,其中名称= @用户名);      cmd.Parameters.AddWithValue(@用户名,用户名);      返回DA.SqlManager.GetDataSet(CMD);
}

您获取所有数据集中所需的数据。之后,你做检查,像taxesDst.Tables [0] .Rows.Count> 0(或== 0)

有关插入你可以有这样的方法:

 公共虚拟无效插入(params对象[] colValues​​)
    {
        如果(colValues​​ == NULL || colValues​​.Length%2!= 0)
            抛出新的ArgumentException(传递无效的列值预计公司对(的ColumnName,ColumnValue)。);        CMD的SqlCommand =新的SqlCommand(插入+表名+({0})VALUES({1}));        字符串insertCols =的String.Empty;
        字符串insertParams =的String.Empty;        的for(int i = 0; I< colValues​​.Length;我+ = 2)
        {
            串隔板=,;
            如果(我== colValues​​.Length - 2)
                隔板=;            字符串参数=@P+我;            insertCols + = colValues​​ [I] +分隔符;
            insertParams + =参数+分隔符;            cmd.Parameters.AddWithValue(参数,colValues​​第[i + 1]);
        }        cmd.CommandText =的String.Format(cmd.CommandText,insertCols,insertParams);        DA.SqlManager.ExecuteNonQuery(CMD);
    }

有关这一点,你需要在目前的BO类属性表名。

在这种情况下,这种方法可以用于任何地方,你需要code的只有一行来调用它们,像你这样没有问题会发生。

I am working in a simple registration page where the user can't enter the same user name or email, I made a code that prevent the user from entering the username and it worked but when I tried to prevent the user from entring the same username or email it didn't work.

and my question is, "How can I add another condition where the user can't enter email that already exists?"

I tried to do it in this code, but it did't work:

protected void Button_Click(object sender, EventArgs e)
{
  SqlConnection con = new SqlConnection( ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString );
  SqlCommand cmd1 = new SqlCommand("select 1 from Table where Name =@UserName", con);
  SqlCommand cmd2 = new SqlCommand("select 1 from Table where Email=@UserEmail", con);

  con.Open();
  cmd1.Parameters.AddWithValue("@UserName", Name_id.Text);
  cmd2.Parameters.AddWithValue("@UserEmail", Email_id.Text); 

  using (var dr1 = cmd1.ExecuteReader())
  {
    if (dr1.HasRows)
    {
      Label1.Text = "user name already exists";
    }
    using (var dr2 = cmd2.ExecuteReader())
    {
      if (dr2.HasRows)
      {
        Label1.Text = "email already exists";
      }
      else
      {
        dr1.Close();
        dr2.Close();
        //add new users
        con.Close();
      }
    }
  }  
}

but i get this error:

There is already an open DataReader associated with this Command which must be closed first.

解决方案

Like I said in my comment your design is bad !

First you should have Data Access Layer. This should be project in big solutions but in your case you can put it like new directory. In this directory you create SqlManager class here is the code:

public class SqlManager
{

    public static string ConnectionString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["DevConnString"].ConnectionString;
        }
    }

    public static SqlConnection GetSqlConnection(SqlCommand cmd)
    {
        if (cmd.Connection == null)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);

            conn.Open();

            cmd.Connection = conn;

            return conn;
        }

        return cmd.Connection; 
    }

    public static int ExecuteNonQuery(SqlCommand cmd)
    {
        SqlConnection conn = GetSqlConnection(cmd);

        try
        {
            return cmd.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    }

    public static object ExecuteScalar(SqlCommand cmd)
    {

        SqlConnection conn = GetSqlConnection(cmd);

        try
        {
            return cmd.ExecuteScalar();
        }
        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    }

    public static DataSet GetDataSet(SqlCommand cmd)
    {
        return GetDataSet(cmd, "Table");
    }

    public static DataSet GetDataSet(SqlCommand cmd, string defaultTable)
    {
        SqlConnection conn = GetSqlConnection(cmd);

        try
        {
            DataSet resultDst = new DataSet();

            using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
            {
                adapter.Fill(resultDst, defaultTable);
            }

            return resultDst;
        }
        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    }


    public static DataRow GetDataRow(SqlCommand cmd)
    {
        return GetDataRow(cmd, "Table");
    }

    public static DataRow GetDataRow(SqlCommand cmd, string defaultTable)
    {
        SqlConnection conn = GetSqlConnection(cmd);

        try
        {
            DataSet resultDst = new DataSet();

            using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
            {
                adapter.Fill(resultDst, defaultTable);
            }

            if (resultDst.Tables.Count > 0 && resultDst.Tables[0].Rows.Count > 0)
            {
                return resultDst.Tables[0].Rows[0];
            }
            else
            {
                return null;
            }
        }
        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    }
}

After that you should have Business Object Layer. In bigger solution is project in your case directory. If you are in the page TaxesEdit.aspx, you should add Tax.cs class in the BO(business object).

Example of methods for the class, for your first button:

public DataSet GetTaxesByUserName(string userName)
{
     SqlCommand cmd = new SqlCommand(@"

        select 1 from Table where Name =@UserName");

      cmd.Parameters.AddWithValue("@UserName", userName);

      return DA.SqlManager.GetDataSet(cmd);
}

You fetch all the needed data in datasets. After that you make checks like taxesDst.Tables[0].Rows.Count > 0 (or == 0)

For Insert you can have method like this:

    public virtual void Insert(params object[] colValues)
    {
        if (colValues == null || colValues.Length % 2 != 0)
            throw new ArgumentException("Invalid column values passed in. Expects pairs (ColumnName, ColumnValue).");

        SqlCommand cmd = new SqlCommand("INSERT INTO " + TableName + " ( {0} ) VALUES ( {1} )");

        string insertCols = string.Empty;
        string insertParams = string.Empty;

        for (int i = 0; i < colValues.Length; i += 2)
        {
            string separator = ", ";
            if (i == colValues.Length - 2)
                separator = "";

            string param = "@P" + i;

            insertCols += colValues[i] + separator;
            insertParams += param + separator;

            cmd.Parameters.AddWithValue(param, colValues[i + 1]);
        }

        cmd.CommandText = string.Format(cmd.CommandText, insertCols, insertParams);

        DA.SqlManager.ExecuteNonQuery(cmd);
    }

For this you need to have property TableName in the current BO class.

In this case this methods can be used everywhere and you need only one line of code to invoke them and no problems like yours will happen.

这篇关于检查用户名或用户电子邮件地址已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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