ASP.net Visual Studio 2013 C# 用户已存在

ASP.net Visual Studio 2013 C# user already exists

我有这段代码要测试以防用户已经存在于数据库中

if (IsPostBack)
{
    SqlConnection conn = new SqlConnection(
         ConfigurationManager.ConnectionStrings["AssignmentDBConnectionString"]
         .ConnectionString);
    conn.Open();

    //selects count from userdata and checks if username exists in the database
    string checkUser = "select count(*) from [AsTable] where Username ='" 
         + TextBoxUsername.Text + "'";

    SqlCommand com = new SqlCommand(checkUser, conn);
    com.ExecuteNonQuery();
    int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
    if (temp > 0)
    {
        Response.Write("User Already Exists");
    }
    conn.Close();
}

然而,当我尝试注册一个已经存在的用户时,它不会显示用户已经存在的消息并抛出此错误

InvalidOperationException was unhandled by user code An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code
Additional information: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

这是堆栈跟踪

[InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.]

System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +5356096 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) +146 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) +16 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) +94 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +110 System.Data.SqlClient.SqlConnection.Open() +96 Registration.Page_Load(Object sender, EventArgs e) in c:\Users\Michalis\Documents\Visual Studio 2013\WebSites\Assignment - ASP\Registration.aspx.cs:40 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 System.Web.UI.Control.OnLoad(EventArgs e) +92 System.Web.UI.Control.LoadRecursive() +54 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

试试这个:

 if (IsPostBack)
 {
 int result = 0;
 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["AssignmentDBConnectionString"].ConnectionString);
     conn.Open();

//selects count from userdata and checks if username exists in the database
string checkUser = "select count(*) from [AsTable] where Username = @username";

SqlCommand com = new SqlCommand(checkUser, conn);
com.Parameters.AddWithValue("@username", TextBoxUsername.Text);
result = (int)com.ExecuteScalar()
if (result > 0)
{
    Response.Write("User Already Exists");
}
conn.Close();
}

请使用return;在检查它是否 return 存在之后它将停止执行你的代码的其余部分..祝你好运.

找到了!!我在 page_load 代码上方声明了 temp 并像这样修改它

int temp = 0;

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["AssignmentDBConnectionString"].ConnectionString);
        conn.Open();
        string checkUser = "select count(*) from [AsTable] where Username ='" + TextBoxUsername.Text + "'";
        SqlCommand com = new SqlCommand(checkUser, conn);
        com.ExecuteNonQuery();
        temp = Convert.ToInt32(com.ExecuteScalar().ToString());
        if (temp != 0)
        {
            Response.Write("User Already Exists");
        }
        conn.Close();
    }
}
 .
 .

后面的代码!!!