用户名,密码和3个按钮——登陆,注册,重置,目前无大问题了,就是注册时注册同名用户怎么让它说已注册,请换个名字,现在是注册同名不同密码也可以,但这个名字登陆失败,这代码不是我写的,只是大概能看懂,伸手党求代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (getInfo(txb_USERID.Text.ToString()).Tables[0].Rows.Count > 0)
        {
            DataRow DR = getInfo(txb_USERID.Text.ToString()).Tables[0].Rows[0];
            if (DR["PWD"].ToString() == txb_PWD.Text.ToString())
            {
                lbl_INFO.Text = "登录成功";
            }
            else
            {
                lbl_INFO.Text = "登录失败";
            }
        }
        else
        {
            lbl_INFO.Text = "无该人员信息";
        }
    }    protected void Button2_Click(object sender, EventArgs e)
    {
        string USERID = txb_USERID.Text.ToString();
        string PWD = txb_PWD.Text.ToString();
        bool insertOK = InsertInfo(USERID, PWD);
        if (insertOK)
        {
            lbl_INFO.Text = "注册成功";
        }
        else 
        {
            lbl_INFO.Text = "注册失败";
        }
    }
    protected DataSet getInfo(string USERID)
    {
        string strCon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\liniannian\Documents\Visual Studio 2005\Projects\WebSite99\SqlDataSource.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        SqlConnection con = new SqlConnection(strCon);
        string str = "select * from SQLTable where USERID='" + USERID + "'";
        con.Open();
        try
        {
            SqlDataAdapter sda = new SqlDataAdapter(str, con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }
        catch
        {
            throw new Exception(str);
        }
        finally
        {
            con.Close();
        }    }
     protected bool InsertInfo(string USERID,string PWD)
    {
        string strCon =  @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\liniannian\Documents\Visual Studio 2005\Projects\WebSite99\SqlDataSource.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        SqlConnection con = new SqlConnection(strCon);
        string str = "insert into SQLTable (USERID,PWD) VALUES ('" + USERID + "','" + PWD + "')";
        SqlCommand cmd = new SqlCommand(str, con);
        con.Open();
        try
        {
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (System.Data.SqlClient.SqlException e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            cmd.Dispose();
            con.Close();
        }    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        txb_PWD.Text = "";
        txb_USERID.Text = "";
        lbl_INFO.Text = "";    }
    protected void txb_USERID_TextChanged(object sender, EventArgs e)
    {    }
    protected void txb_PWD_TextChanged(object sender, EventArgs e)
    {    }
protected void  SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{}
}

解决方案 »

  1.   

    你要把验证信息返回给页面显示,或者输出一段js提示脚本建议你使用ajax异步请求验证
    http://kb.cnblogs.com/a/1579126/
    这个比较原始
      

  2.   


    protected void Button2_Click(object sender, EventArgs e)
      {
      string USERID = txb_USERID.Text.ToString();
      string PWD = txb_PWD.Text.ToString();
    DataSet ds =getInfo(USERID );
    if(dds.Tables[0].Rows.Count()>0)
    {
     lbl_INFO.Text = "用户名已存在!";
    }
    else
    {
    bool insertOK = InsertInfo(USERID, PWD);
      if (insertOK)
      {
      lbl_INFO.Text = "注册成功";
      }
      else  
      {
      lbl_INFO.Text = "注册失败";
      }
    }
      
      }
      

  3.   

    有错误,第六行是dds改为ds吗?我改后是这样——编译器错误消息: CS0118: “System.Data.InternalDataCollectionBase.Count”是“属性”,但此处被当做“方法”来使用
    行 43:         if (ds.Tables[0].Rows.Count() > 0)
      

  4.   


    protected void Button2_Click(object sender, EventArgs e)
      {
      string USERID = txb_USERID.Text.ToString();
      string PWD = txb_PWD.Text.ToString();
    DataSet ds =getInfo(USERID );
    if( ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0) //sorry 更正
    {
     lbl_INFO.Text = "用户名已存在!";
    }
    else
    {
    bool insertOK = InsertInfo(USERID, PWD);
      if (insertOK)
      {
      lbl_INFO.Text = "注册成功";
      }
      else  
      {
      lbl_INFO.Text = "注册失败";
      }
    }
      
      }