2013-07-19 102 views
-1

我正在为注册用户提供一个表单,但我不知道如何验证电子邮件和/或用户名是否已被使用。 我在aspx.cs代码:验证用户名和电子邮件是否已被使用

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

public partial class Registrazione : System.Web.UI.Page { 
    private string strcon = WebConfigurationManager.ConnectionStrings["UtentiRegistratiConnectionString1"].ConnectionString; 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection(strcon); 
     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = con; 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.CommandText = "Registrazione"; 
     cmd.Parameters.AddWithValue("@vnome", TextBox2.Text); 
     cmd.Parameters.AddWithValue("@vcognome", TextBox3.Text); 
     cmd.Parameters.AddWithValue("@vindirizzo", TextBox5.Text); 
     cmd.Parameters.AddWithValue("@vindmail", TextBox4.Text); 
     cmd.Parameters.AddWithValue("@vpassword", TextBox6.Text); 
     cmd.Parameters.AddWithValue("@vcellulare", TextBox8.Text); 
     cmd.Parameters.AddWithValue("@vcasa", TextBox9.Text); 
     cmd.Parameters.AddWithValue("@vcitta", TextBox10.Text); 
     cmd.Parameters.AddWithValue("@vcodfisc", TextBox11.Text); 
     con.Open(); 
     cmd.ExecuteNonQuery();   
     con.Close(); 
    } 
} 

与 “@vpassword” 和 “@vcodfisc” 的场均验证。我如何做到这一点?

注册的存储过程是:

CREATE PROCEDURE [dbo].[Registrazione] 
    @vnome varchar(50), 
    @vcognome varchar(50), 
    @vindirizzo varchar(50), 
    @vcellulare varchar(50), 
    @vcasa varchar(50), 
    @vcodfisc varchar(50), 
    @vindmail varchar(50), 
    @vpassword varchar(50), 
    @vcitta varchar(50) 
AS 
    insert into Utenti (Nome,Cognome,Indirizzo,Cellulare,Casa,[Codice Fiscale],[Indirizzo E-Mail],Password,Città) values (@vnome,@vcognome,@vindirizzo,@vcellulare,@vcasa,@vcodfisc,@vindmail,@vpassword,@vcitta) 

RETURN 0 

如果我做的话,它不走,为什么?

公共部分类Registrazione:System.Web.UI.Page {

私人字符串STRCON = WebConfigurationManager.ConnectionStrings [ “UtentiRegistratiConnectionString1”]的ConnectionString。

protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection(strcon); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = con; 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "Registrazione"; 
    cmd.Parameters.AddWithValue("@vnome", TextBox2.Text); 
    cmd.Parameters.AddWithValue("@vcognome", TextBox3.Text); 
    cmd.Parameters.AddWithValue("@vindirizzo", TextBox5.Text); 
    cmd.Parameters.AddWithValue("@vindmail", TextBox4.Text); 
    cmd.Parameters.AddWithValue("@vpassword", TextBox6.Text); 
    cmd.Parameters.AddWithValue("@vcellulare", TextBox8.Text); 
    cmd.Parameters.AddWithValue("@vcasa", TextBox9.Text); 
    cmd.Parameters.AddWithValue("@vcitta", TextBox10.Text); 
    cmd.Parameters.AddWithValue("@vcodfisc", TextBox11.Text); 
    if (TextBox4.Text == "SELECT * FROM Utenti WHERE [Indirizzo E-Mail] = [email protected]") 
    { 
     RequiredFieldValidator3.ErrorMessage = "Indirizzo E-Mail già esistente"; 
    } 
    else 
    { 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     Response.Redirect("~/TutteLeOfferte.aspx"); 
    } 
} 
+0

欢迎堆栈溢出!请不要只是要求我们为您解决问题。告诉我们你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。请参阅“[您尝试过什么?](http://whathaveyoutried.com/)”,以获得一篇您最近需要阅读的优秀文章。 –

+0

@约翰桑德斯我还没有尝试过一些东西,只是因为我不知道链接到数据库的语言,我不知道如何在这种情况下移动自己... – ProtoTyPus

回答

0

我只是从数据库中选择一个用户名或电子邮件是否已经存在,然后再运行您的SqlCommand。

+0

你能举个例子吗? – ProtoTyPus

1

此代码将不是就是工作,这是最好的,我可以做,因为你没有提供您的存储过程

在你的存储过程做;

SELECT COUNT(UserName) 
WHERE UserName = @vcodfisc AND Password = @vpassword 

然后在C#做

if (queryResult > 0) 
    // username exists 
else 
    //count was 0, that user name does not exist. 

这是假设qeuryResult是一个整数。这意味着你已经执行了查询,并使用SqlDatReader或其他一些机制将结果读入int。

此外,我要指出匹配密码根本没有意义。用户名必须是唯一的,密码不应该。所以,你真的应该把用户名统计为username = inputuserName。它应该返回0或1.如果它是0,登录是唯一的,你可以继续注册用户。如果它是1,那么用户名已经存在,如果它大于1,那么你通过其他方式将错误的数据放入数据库;在插入之前不执行此检查的内容。

+0

@evanmcdonald如果你解释我什么是“proc”我可以说你=) – ProtoTyPus

+0

@DanieleNekoLuciani这是一个错字,应该是“sproc”,即存储过程,这就是你正在执行的。 – evanmcdonnal

+0

我已经添加了!我希望你能更好地帮助我! – ProtoTyPus

0
SqlCommand command = new SqlCommand(); 
command.CommandText = "SELECT COUNT(UserName) WHERE UserName = '" + vcodfisc + "'"; 
int count = command.ExecuteScalar(); 

if (count == 0) 
    //username not exist 
else //username exist 

这样的事情,对不起,我不记得确切的语法

+0

现在我试试吧!非常感谢 – ProtoTyPus

相关问题