Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我使用的是VisualStudio,我有一个SQL Server表,但是当我插入数据时,我得到了一个错误_C#_Sql Server - Fatal编程技术网

C# 我使用的是VisualStudio,我有一个SQL Server表,但是当我插入数据时,我得到了一个错误

C# 我使用的是VisualStudio,我有一个SQL Server表,但是当我插入数据时,我得到了一个错误,c#,sql-server,C#,Sql Server,我试图将数据插入SQL Server表中,但它不允许我这样做,并抛出一个错误。我认为这个错误是因为我不知道如何修复它;我需要你的帮助,谢谢 这是我正在使用的成员表: CREATE TABLE [dbo].[Member] ( [Member_Username] NVARCHAR (50) NOT NULL, [Password] NVARCHAR (25) NOT NULL, [Role] NVARCHAR (10) NULL,

我试图将数据插入SQL Server表中,但它不允许我这样做,并抛出一个错误。我认为这个错误是因为我不知道如何修复它;我需要你的帮助,谢谢

这是我正在使用的成员表:

CREATE TABLE [dbo].[Member] 
(
     [Member_Username] NVARCHAR (50) NOT NULL,
     [Password]        NVARCHAR (25) NOT NULL,
     [Role]            NVARCHAR (10) NULL,
     [FirstName]       NVARCHAR (50) NOT NULL,
     [LastName]        NVARCHAR (50) NOT NULL,
     [Gender]          NVARCHAR (8)  NOT NULL,
     [Email]           NVARCHAR (50) NULL,
     [DateOfBirth]     DATE          NOT NULL,

     PRIMARY KEY CLUSTERED ([Member_Username] ASC)
);
这就是我在表中插入值时遇到的错误:

System.Data.SqlClient.SqlException:
参数化查询“(@memberU-nvarchar(1),@pwd-nvarchar(1),@role-nvarchar(4000),@fna”需要未提供的参数“@role”

这是用于在数据库表中插入用户的成员类:

public void AddMember()
{
    // Open database connection
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = Config.GetConnectionStr();
    conn.Open();

    // Prepare SQL command with parameters
    string sql = "INSERT INTO Member VALUES (@memberU, @pwd, @role, @fname, @lname, @gender, @email, @dob)";

    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.AddWithValue("memberU", this.Member_Username);
    cmd.Parameters.AddWithValue("pwd", this.Password);
    cmd.Parameters.AddWithValue("role", this.Role);
    cmd.Parameters.AddWithValue("fname", this.FirstName);
    cmd.Parameters.AddWithValue("lname", this.LastName);
    cmd.Parameters.AddWithValue("email", this.Email);

    // handling null values for gender and date of birth column
    if (this.Gender != null)
    {
        cmd.Parameters.AddWithValue("gender", this.Gender);
    }
    else
    {
        cmd.Parameters.AddWithValue("gender", DBNull.Value);
    }

    if (this.DateofBirth != null)
    {
        cmd.Parameters.AddWithValue("dob", this.DateofBirth);
    }
    else
    {
        cmd.Parameters.AddWithValue("dob", DBNull.Value);
    }

    // Execute command
    cmd.ExecuteNonQuery();
}
这是注册按钮:

protected void btnSignUp_Click(object sender, EventArgs e)
{
        if (Page.IsValid)// assuming you have done validations using validation controls
        {// c create a new object of type member and set all it's properties to values from controls
            Members user = new Members();
            //reading required values
            user.FirstName = txtFirstName.Text;
            user.LastName = txtLastName.Text;
            user.Member_Username = txtUserName.Text;
            user.Password = txtPassword.Text;
            user.Email = txtEmail.Text;
            user.Gender = rdoGender.SelectedValue;

            //reading values that allow null in the database (date of birth)
            if (string.IsNullOrEmpty(txtDOB.Text))
            {
                user.DateofBirth = null;

            }
            else
            {
                user.DateofBirth = DateTime.Parse(txtDOB.Text);
            }

            //call the addMember method
            user.AddMember();

            //redirect the user to homePage
            Response.Redirect("Login.aspx");
        }
}

当您添加诸如(cmd.parameters.addwithvalue(“@role”,value)之类的参数时,是否可以尝试.

请阅读并接受。您所有的帖子的分数都是0或更低,这将很快给您带来麻烦。您从未将任何内容分配给
user.Role
,因此该值为
null
,失败在
注册
方法中,为
user.Role
分配一个值。这与html有什么关系?实际上与MySql sinc有什么关系看起来您正在使用Sql?请只添加适当的标记。您应该签出并停止使用
.AddWithValue()
-这可能会导致意外的结果。。。