System.Data.SqlClient.SqlException: 'Must declare the scalar variable'

馋奶兔 提交于 2019-12-25 18:53:48

问题


I have checked other threads with related errors, but none of them pertained to my problem on a specific level.

I am trying to update data via TextBox edits in a WPF form, submitted via an Update Button to the SQL database. The initial / current data is loaded when the form opens and a user is selected. It will show all the information that pertains to them on the SQL database in their corresponding TextBoxes, the problem comes into play when I want to change that loaded information and update it.

At the line "da.UpdateCommand.ExecuteNonQuery();" it is throwing the error that the scalar varaible for @FirstName has not been declared, but when commenting that out it just moves on to the next in line and will keep going. So it's not related specifically to any one parameter.

I have double checked that all the data types are correct with the database and made sure there were no typing errors (though there's some possibility I missed one).

Any ideas on what could be causing this would be appreciated, I keep hitting a wall with it.

void UpdateEmployee()
    {
        using (SqlConnection con = new SqlConnection(Properties.Settings.Default.adminConnectionString))
        { 
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter
            {
                UpdateCommand = new SqlCommand("UPDATE dbo.EMPLOYEES " +
                "SET FirstName = @FirstNameCL, " +
                "InsuranceDate = @InsuranceDateCL, " +
                "InsurancePlan = @PlanCL, " +
                "InsuranceStatus = @StatusCL, " +
                "InsuranceElig = @EligibleCL" +
                "Department = @Dpt, " +
                "LastName = @LastNameCL, " +
                "DateOfFullTime = @FullTimed, " +
                "DateOfHire = @startdate, " +
                "Passcode = @PasswordCL, " +
                "Phone1 = @HomeCL" +
                "Phone2 = @CellCL, " +
                "Address = @AddressCL, " +
                "IsTempEmp = @Istemp," +
                "Comments = @CommentsCL" +

                "WHERE employeeID = EmpID", con)
            };

            {
                da.UpdateCommand.Parameters.Add("@FirstName",SqlDbType.NVarChar).Value = FirstNameCL.Text;
                da.UpdateCommand.Parameters.Add("@InsuranceDate", SqlDbType.Date).Value = InsuranceDateCL.SelectedDate;
                da.UpdateCommand.Parameters.Add("@InsurancePlan", SqlDbType.VarChar).Value = PlanCL.Text;
                da.UpdateCommand.Parameters.Add("@InsuranceStatus", SqlDbType.VarChar).Value = StatusCL.Text;
                da.UpdateCommand.Parameters.Add("@InsuranceElig", SqlDbType.VarChar).Value = EligibleCL.Text;
                da.UpdateCommand.Parameters.Add("@Depeartment", SqlDbType.VarChar).Value = Dpt.Text;
                da.UpdateCommand.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = LastNameCL.Text;
                da.UpdateCommand.Parameters.Add("@DateOfFullTime", SqlDbType.Date).Value = fulltimed.SelectedDate;
                da.UpdateCommand.Parameters.Add("@DateofHire", SqlDbType.Date).Value = startdate.SelectedDate;
                da.UpdateCommand.Parameters.Add("@Passcode", SqlDbType.VarChar).Value = PasswordCL.Text;
                da.UpdateCommand.Parameters.Add("@Phone1", SqlDbType.VarChar).Value = HomeCL.Text;
                da.UpdateCommand.Parameters.Add("@Phone2", SqlDbType.VarChar).Value = CellCL.Text;
                da.UpdateCommand.Parameters.Add("@Address", SqlDbType.VarChar).Value = AddressCL.Text;
                da.UpdateCommand.Parameters.Add("@IsTempEmp", SqlDbType.VarChar).Value = Istemp.Text;
                da.UpdateCommand.Parameters.Add("@Comments", SqlDbType.NVarChar).Value = CommentsCL.Text;

                con.Open();
                da.UpdateCommand.ExecuteNonQuery();
                con.Close();

            }
        }
    }


    #region Update Button

    private void BtnUpdate_Click(object sender, EventArgs e)
    {
        UpdateEmployee();
        System.Windows.Forms.MessageBox.Show("Employee data successfully updated.");

    }
    #endregion

回答1:


"SET FirstName = @FirstNameCL, " 

should be

"SET FirstName = @FirstName, " 

depending on if the following:

            da.UpdateCommand.Parameters.Add("@FirstName",SqlDbType.NVarChar).Value = FirstNameCL.Text;

is the correct variable name.




回答2:


The first parameter of Parameters.Add is the parameters name:

public System.Data.SqlClient.SqlParameter Add (string parameterName, System.Data.SqlDbType sqlDbType);

da.UpdateCommand.Parameters.Add("@FirstName",SqlDbType.NVarChar).Value = FirstNameCL.Text;

@FirstName does not match your sql command



来源:https://stackoverflow.com/questions/57978014/system-data-sqlclient-sqlexception-must-declare-the-scalar-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!