插入sql表的问题

时间:2012-09-06 13:58:42

标签: c# sql

我正在开发一个网站,当用户创建一个帐户时,我想存储一些我在CreateUserWizard中收集的其他信息。当我运行代码时,我在尝试将数据写入我创建的表时出错。我在ASPNETDB.mdf数据库(默认创建的数据库)中创建了一个名为UserProfile的表,并且包含了我要写入的所有列。在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” 如果我注释掉代码,它将不会向数据库表写入任何内容。我做错了什么?

2 个答案:

答案 0 :(得分:4)

您有myCommand.Parameters.AddWithValue("@Role", UserRole);,但我认为您应该myCommand.Parameters.AddWithValue("@UserRole", UserRole);

答案 1 :(得分:0)

对于任何关心的人......这是工作代码:

protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
    {
            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 == ""))
            {
                if (!User.IsInRole("manager"))
                {
                Roles.AddUserToRole(UserName, "manager");
                Role = "manager";
                }
            }
            if (PartsList == "SF")
            {
                if (!User.IsInRole("user_sf"))
                {
                    Roles.AddUserToRole(UserName, "user_sf");
                    Role = "user_sf";
                }
            }
            if (PartsList == "TL")
            {
                if (!User.IsInRole("user_tl"))
                {
                    Roles.AddUserToRole(UserName, "user_tl");
                    Role = "user_tl";
                }
            }
            if ((Company != "OurCompany" || Company != "OurCompany, Inc.") && PartsList == "")
            {
                if (!User.IsInRole("user"))
                {
                    Roles.AddUserToRole(UserName, "user");
                    Role = "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, Role)" +
                               "VALUES(@FirstName, @LastName, @Company, @Partslist, @UserName, @Password, @Email, @Question, @Answer, @Role)";

            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", Role);

                myCommand.ExecuteNonQuery();
                myConnection.Close();                    
            }
    }