如果用户已经存在下面是如何使用代码登录是我的代码,但它不起作用任何人都可以帮助我 [英] how to use a code for login if user already exist below is my code but it's not working any one can help me

查看:44
本文介绍了如果用户已经存在下面是如何使用代码登录是我的代码,但它不起作用任何人都可以帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 System.Data;

public partial class _Default:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
if (IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ LEARNINGConnectionString]。ConnectionString);
con.Open();
string checkUser = select count(* )来自login,其中username =' + TextBox1.Text + ';
SqlCommand cod = new SqlCommand(checkUser,con);
int temp = Convert.ToInt32(cod.ExecuteScalar()。ToString());
con.Close();
if (temp == 1
{
回复。写( 用户已存在);
}

}
}
受保护 void Button1_Click( object sender,EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [ LEARNINGConnectionString]的ConnectionString)。
con.Open();
string InsertQuery = insert into login(用户名,密码)值(@ Username,@ password);
SqlCommand cmd = new SqlCommand(InsertQuery,con);
cmd.Parameters.AddWithValue( @ Username,TextBox1.Text);
cmd.Parameters.AddWithValue( @ password,TextBox2.Text);
cmd.ExecuteNonQuery();
Response.Redirect( Admin.aspx);
Response.Write( 注册成功);
con.Close();



}
catch (例外情况)
{
回复.Write( error + ex.ToString());

}
}
}

解决方案

错误是什么你来了吗?有例外吗?或者代码似乎没有在Page_Load中进行检查?



我猜你在Page_Load中的检查工作正常。但是,您没有代码可以阻止Button1_Click事件触发。当您发现用户已经存在时,您需要停止处理Button1_Click事件。



一个选项是调用Response.End()方法:

< br /> 
if(temp == 1)< br />
{< br />
响应。写(用户已存在);< br />
Response.End();< br />
}< br />





你可以通过在(temp == 1)时在表单上设置一些变量(bool _userExists),然后让Button1_Click事件检查该值 - if(_userExists)返回


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LEARNINGConnectionString"].ConnectionString);
            con.Open();
            string checkUser = "select count(*) from login where username='" + TextBox1.Text + "'";
            SqlCommand cod = new SqlCommand(checkUser, con);
            int temp = Convert.ToInt32(cod.ExecuteScalar().ToString());
            con.Close();
            if (temp == 1)
            {
                Response.Write("User already exist");
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LEARNINGConnectionString"].ConnectionString);
            con.Open();
            string InsertQuery = "insert into login (Username,password) values (@Username,@password)";
            SqlCommand cmd = new SqlCommand(InsertQuery, con);
            cmd.Parameters.AddWithValue("@Username", TextBox1.Text);
            cmd.Parameters.AddWithValue("@password", TextBox2.Text);
            cmd.ExecuteNonQuery();
            Response.Redirect("Admin.aspx");
            Response.Write(" registration sucessfully");
            con.Close();
            
            
            
        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());

        }
    }
}

解决方案

What errors are you getting? Are there exceptions? Or does the code just not seem to be doing the check in the Page_Load?

I would guess that your check in the Page_Load is working. However, you have no code to stop the Button1_Click event from firing. When you find that the user already exists, you need to stop the Button1_Click event from processing.

One option for this is calling the Response.End() method:

<br />
if (temp == 1)<br />
{<br />
   Response.Write("User already exists");<br />
   Response.End();<br />
}<br />



You could do this by setting some variable (bool _userExists) on the form when the (temp == 1) and then have the Button1_Click event check that value -- if (_userExists) return


这篇关于如果用户已经存在下面是如何使用代码登录是我的代码,但它不起作用任何人都可以帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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