我的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列得到其值,並將其存儲在一個正常的整數變量?請幫忙。 -
你可以顯示你用來存儲他們的數據在數據庫中的代碼? – Robbie 2012-03-16 20:13:06
@Robbie:我已經添加了我用來存儲上面的用戶信息的代碼。請查看相關代碼。 – 2012-03-17 09:35:36