I have only just started using SQL Server in C# but I am getting an error
Must Declare Scalar Variable
when it tries to run. Obviously I have read other posts where this was the problem and I have tried to fix it using the information from the posts, however none were successful.
public static void AddUser(ProgressBar progressBar1, string FirstName, string LastName, string CurrentWorkingGrade, string PredictedGrade, string year)
{
try
{
///Increases the ID Key
int IDVal = 7;
///Uses Connection String from DB to form local connection
SqlConnection Connect = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\Chris\Documents\LogInFormMK2\LogInFormMK2\Students.mdf; Integrated Security = True");
///Opens Connection
Connect.Open();
///Initialises the AddRecord
SqlCommand AddRecord = new SqlCommand();
///Sets all variables passed from main to a parameters to avoid SQl injection
AddRecord.Parameters.AddWithValue("@FN", FirstName);
AddRecord.Parameters.AddWithValue("@LN", LastName);
AddRecord.Parameters.AddWithValue("@CWG", CurrentWorkingGrade);
AddRecord.Parameters.AddWithValue("@PG", PredictedGrade);
AddRecord.Parameters.AddWithValue("@YR", year);
///Creates the query
AddRecord = new SqlCommand("INSERT INTO dbo.StudentINFO (Id, FirstName, LastName, DateOfBirth, CurrentWorkingGrade, PredictedGrade, Year) VALUES (IDVal, + @FN, + @LN, '2001/05/21', + @CWG, + @PG, + @YR)");
AddRecord.Connection = Connect;
AddRecord.ExecuteNonQuery();
MessageBox.Show("Student added succesfully");
IDVal = IDVal + 1;
}
catch(Exception ex)
{
MessageBox.Show("Sorry there was an error adding this student. Error Code" + ex);
}
}