Reputation: 61
I am trying insert some data into a database table using sql commands. I keep getting this error:
Must declare the scalar variable "@MC"
Code:
void CreateModule() {
Query = "INSERT INTO TblModules (Module_Code, Module_Name, Date,Start_Time, Duration) values ( @MC, @MN, @DATE, @ST, @DURATION)";
theReader = dc.ExecuteStatement(Query);
command.Parameters.AddWithValue("@MC", ModuleCodeTxt.Text);
command.Parameters.AddWithValue("@MN", ModulenameTxt.Text);
command.Parameters.AddWithValue("@DATE", MyDateTimePicker.Value);
command.Parameters.AddWithValue("@ST", comboBoxTime.SelectedItem);
command.Parameters.AddWithValue("@DURATION", comboBoxDuration.SelectedItem);
MessageBox.Show(" Module Created");
conn.CloseConnection();
}
Upvotes: 0
Views: 89
Reputation: 101731
You should give value to your parameters before you execute the query
command.CommandText = Query; // <-- don't forget to set command text
command.Parameters.AddWithValue("@MC", ModuleCodeTxt.Text);
command.Parameters.AddWithValue("@MN", ModulenameTxt.Text);
command.Parameters.AddWithValue("@DATE", MyDateTimePicker.Value);
command.Parameters.AddWithValue("@ST", comboBoxTime.SelectedItem);
command.Parameters.AddWithValue("@DURATION", comboBoxDuration.SelectedItem);
//execute the query here, maybe you want this?
theReader = command.ExecuteReader();
By the way where is your command
? your Query
variable is just a string literal that holds command text.I don't know what is your method doing but I think you should pass your command
to your ExecuteStatement
and change your method ofcourse.
Upvotes: 7