2013-07-19 171 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

相關問題