简体   繁体   English

尝试以c#格式将数据插入数据库,但在按下插入按钮时收到此错误消息。 不提供参数

[英]Trying to insert data to database in c# forms but getting this error message when insert button is hit. Parameter not supplied

Connecting my program to the database but running into this problem. 将我的程序连接到数据库,但遇到此问题。

Error message: 错误信息:

The parameterized query '@first_name nvarchar(1), @last_name nvarchar(1), @email nvarchar(' expects the parameter '@username', which is not supplied. 参数化查询'@first_name nvarchar(1),@ last_name nvarchar(1),@ email nvarchar(')需要未提供的参数'@username'。

Absolutely lost not really sure what the error message is saying. 完全不知道该错误消息在说什么。

Here's my code to insert the data into SQL Server database tables. 这是将数据插入SQL Server数据库表中的代码。 Any advice is appreciated. 任何建议表示赞赏。

public bool Insert(userBLL u)
{
    bool Success = false;

    SqlConnection conn = new SqlConnection(myconnstrng);

    try
    {
        String sql = "INSERT INTO tbl_users (first_name, last_name, email, username, password, contact, address, gender, user_type, added_date, added_by) VALUES (@first_name, @last_name, @email, @username, @password, @contact, @address, @gender, @user_type, @added_date, @added_by)";

        SqlCommand cmd = new SqlCommand(sql, conn);

        cmd.Parameters.AddWithValue("@first_name", u.first_name);
        cmd.Parameters.AddWithValue("@last_name", u.last_name);
        cmd.Parameters.AddWithValue("@email", u.email);
        cmd.Parameters.AddWithValue("@username", u.username);
        cmd.Parameters.AddWithValue("@password", u.password);
        cmd.Parameters.AddWithValue("@contact", u.contact);
        cmd.Parameters.AddWithValue("@address", u.address);
        cmd.Parameters.AddWithValue("@gender", u.gender);
        cmd.Parameters.AddWithValue("@user_type", u.user_type);
        cmd.Parameters.AddWithValue("@added_date", u.added_date);
        cmd.Parameters.AddWithValue("@added_by", u.added_by);

        conn.Open();

        int rows = cmd.ExecuteNonQuery();

        // value of rows will be greater than 0 or else
        if (rows > 0)
        {
            Success = true; 
        }
        else
        {
            Success = false;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        conn.Close();
    }

    return Success;
}

After observing your code, In region( #region Insert data to database ) that you shared, I think there was no problem in your code. 观察代码后,在您共享的region( #region Insert data to database )中,我认为您的代码没有问题。

Make sure userBLL object is not null that you passed in function Insert . 确保在函数Insert传递的userBLL对象不为null。 So debug your code one more time. 因此,再调试一次代码。

By observing your code the only reason for this error looks like the userbill object has null value for username . 通过观察代码,此错误的唯一原因似乎是userbill对象的username值为null。 So debug it one time and then share 因此,调试一次然后共享

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM