2012-03-16 125 views
0

我的Web應用程序的一部分註冊用戶,並在將數據存儲到數據庫後向其發送確認郵件。我該如何分配每個用戶一個唯一的註冊ID自動像這種模式的「MVIN-0000001」等爲每個用戶如何在用戶註冊後生成註冊號(UserId)並通過郵件發送給用戶?

//Inserting registration data of user!!! 
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString); 
string inscmd = "Insert into Registration(Username, Password,EmailAddress,FullName,CNIC,city) Values(@UserName, @Password, @EmailAddress, @FullName, @CNIC, @City)"; 
SqlCommand InsertUser = new SqlCommand(inscmd, con); 
InsertUser.Parameters.AddWithValue("@UserName", TextBoxUN.Text); 
InsertUser.Parameters.AddWithValue("@Password", TextBoxPass.Text); 
InsertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text); 
InsertUser.Parameters.AddWithValue("@FullName", TextBoxFN.Text); 
InsertUser.Parameters.AddWithValue("@CNIC", TextBoxCNIC.Text); 
InsertUser.Parameters.AddWithValue("@City", DropDownListCity.SelectedItem.ToString()); 

try 
{ 
    con.Open(); 
    //Response.Write("Trying ..."); 
    InsertUser.ExecuteNonQuery(); 
    con.Close(); 
} 
catch(SqlException ex) 
{ 
    Response.Write(ex.Message); 
    Response.Write("Failed!!! ..."); 
    Response.Write("<b>Something really bad happened .. try again later</b>"); 
} 

//send mail message after user fills registration form 
try     
{ 
    MailMessage msg = new MailMessage(); 
    msg.From = new MailAddress("[email protected]"); 
    msg.To.Add(TextBoxEA.Text); 
    msg.Subject = "Your Registration is confirmed! "; 
    msg.Body = "Dear " + TextBoxFN.Text + " Your Registration to motion voter system has been confirmed. .. Kindly note down your Voter's Identity Number(VIN) required for your login"; 
    SmtpClient Sc = new SmtpClient("smtp.gmail.com"); 
    Sc.Port = 587; 
    Sc.Credentials = new NetworkCredential("[email protected]","password"); 
    Sc.EnableSsl = true; 
    Sc.Send(msg); 
    Response.Write("Sent!!! ... "); 
    Response.Redirect("Login.aspx"); 
} 
catch (Exception ex) 
{ 
    Response.Write(ex.Message); 
} 

我想在我的添加VIN(編號) msg.Body聲明,我該怎麼做? 作爲會員之一已經建議我使用表的UserID列來生成註冊號但是 ......我目前沒有學習SQL。即時通訊只是學習c#和asp.net,但我的項目中的一部分達到我選擇數據庫的交易...我的數據庫表中的UserId列是PrimarKey ...它會隨着用戶信息在其他列,每當用戶註冊。有人可以告訴我如何使用這個UserId列得到其值,並將其存儲在一個正常的整數變量?請幫忙。 -

+0

你可以顯示你用來存儲他們的數據在數據庫中的代碼? – Robbie 2012-03-16 20:13:06

+0

@Robbie:我已經添加了我用來存儲上面的用戶信息的代碼。請查看相關代碼。 – 2012-03-17 09:35:36

回答

2

您可以返回新插入的用戶ID作爲輸出參數,並將您的「MVIN」前綴(加上填充零)與該ID連接起來。

-- after insert 
SET @NewId = scope_identity(); 

我個人不喜歡以那種方式揭露數據庫ID。我寧願使用密碼隨機數字,這樣用戶可以推斷出另一個合法號碼的機會就越小。例如,如果我的ID是「MVIN-0005」,那麼嘗試「MVIN-0004」和「MVIN-0006」是一個非常簡單的步驟。

internal static long CreateId() 
{ 
    RNGCryptoServiceProvider _crypto = new RNGCryptoServiceProvider(); 
    byte[] bytes = new byte[8]; 
    _crypto.GetBytes(bytes); 
    return BitConverter.ToInt64(bytes, 0); 
} 

生成的ID將會很大(對於用戶來說可能太多了)。您可以通過清零陣列末尾的字節來縮短編號(bytes[6] = bytes[7] = 0;

由於不能保證以這種方式生成的兩個隨機數不會發生衝突,因此將它們與用戶記錄一起存儲也可能是一個必要(這樣你可以檢查傻瓜)。

+0

先生,告訴你真相......我目前沒有學習SQL。即時通訊只是學習c#和asp.net,但我的項目中的一部分我已選擇數據庫交易...我的數據庫表中的UserId列是PrimarKey ...它隨着用戶信息在其他列中隨時增加用戶註冊。你能請求我告訴我如何使用這個UserId列**獲取**它的值並將它存儲在一個正常的整型變量中?請幫忙。 - – 2012-03-18 14:33:47