我有一个正在开发的网站,当用户创建一个帐户时,我想存储一些我在 CreateUserWizard 期间收集的附加信息。当我运行代码时,尝试将数据写入我创建的表时出现错误。我在名为 UserProfile 的 ASPNETDB.mdf 数据库(默认创建的数据库)中创建了表,并包含了我要写入的所有列。在 web.config 文件中,我有:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
当用户创建帐户时,这是我在 Creating_User 事件中的代码。
protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
{
//try
//{
string trimUserName = RegisterUser.UserName.Trim();
if (RegisterUser.UserName.Length != trimUserName.Length)
{
accountlbl.Text = "The username cannot contain leading or trailing spaces.";
accountlbl.Visible = true;
//Cancel the created user info
e.Cancel = true;
}
if (RegisterUser.Password.IndexOf(RegisterUser.UserName, StringComparison.OrdinalIgnoreCase) > 0)
{
accountlbl.Text = "The username may not appear anywhere in the password.";
accountlbl.Visible = true;
//Cancel the created user info
e.Cancel = true;
}
String FirstName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
String LastName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
String Company = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CompanyName")).Text;
String PartsList = ((DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("companyddl")).SelectedValue;
String UserName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).Text;
String Password = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Password")).Text;
String Email = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Email")).Text;
String Question = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Question")).Text;
String Answer = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Answer")).Text;
// Insert a new record into User_Profile
if ((Company == "OurCompany" || Company == "OurCompany, Inc.") && PartsList == "")
{
Roles.AddUserToRole(UserName, "manager");
UserRole = "manager";
}
if (PartsList == "SF")
{
Roles.AddUserToRole(UserName, "user_sf");
UserRole = "user_sf";
}
if (PartsList == "TL")
{
Roles.AddUserToRole(UserName, "user_tl");
UserRole = "user_tl";
}
if ((Company != "OurCompany" || Company != "OurCompany, Inc.") && PartsList == "")
{
Roles.AddUserToRole(UserName, "user");
UserRole = "user";
}
// Get your Connection String from the web.config. ApplicationServices is the name I have in my web.config
string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
string insertSql = "INSERT INTO UserProfile(FirstName, LastName, Company, PartsList, UserName, Password, Email, Question, Answer, UserRole)" +
"VALUES(@FirstName, @LastName, @Company, @Partslist, @UserName, @Password, @Email, @Question, @Answer, @UserRole)";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.Parameters.AddWithValue("@FirstName", FirstName);
myCommand.Parameters.AddWithValue("@LastName", LastName);
myCommand.Parameters.AddWithValue("@Company", Company);
myCommand.Parameters.AddWithValue("@PartsList", PartsList);
myCommand.Parameters.AddWithValue("@UserName", UserName);
myCommand.Parameters.AddWithValue("@Password", Password);
myCommand.Parameters.AddWithValue("@Email", Email);
myCommand.Parameters.AddWithValue("@Question", Question);
myCommand.Parameters.AddWithValue("@Answer", Answer);
myCommand.Parameters.AddWithValue("@Role", UserRole);
myCommand.ExecuteNonQuery(); //ERROR HERE!!!//
myConnection.Close();
}
}
我在 myComman.ExecuteNonQuery(); 上收到错误消息 声明——必须声明标量变量“@UserRole”。
如果我注释掉代码,它不会向数据库表写入任何内容。我究竟做错了什么?