I have been stuck on this for weeks, and i've been all over forums trying solutions to the same problem that other people had, but so far it still won't work. I'm not sure what i'm doing wrong.
I'm making a user-interactive page that gets data and submits it / inserts it into the SQL database when the button is clicked to save the data.
EmpID is the data coming from a textbox that generates the users employee ID. EmpID is then stored in the SQL database as the EmployeeID value.
I keep getting the error when I run it and try to save the data;
"Must declare scalar variable "@EmpID"
void UpdateInternal0()
{
try
{
using (SqlConnection con = new SqlConnection(Properties.Settings.Default.adminConnectionString))
{
con.Open();
foreach (DataRowView drv in InternalGrid0.SelectedItems)
{
DataRow row = drv.Row;
bool isSelected = Convert.ToBoolean(drv.Row[0]);
if (isSelected)
{
string sqlString = "INSERT INTO ChecklistTransitionTable (RelatedEmployeeID, RelatedDocIdx) " +
"VALUES (@EmpID, @DOC_Number)";
using (SqlCommand cmd = new SqlCommand(sqlString, con))
{
for (int i = 0; i < InternalGrid0.Columns.Count; i++)
{
int DocIdx = (int)drv.Row[i];
int EmployeeID = Convert.ToInt32(EmpID.Text);
cmd.Parameters.AddWithValue("@EmpID", EmployeeID);
cmd.Parameters.AddWithValue("@DOC_Number", DocIdx);
cmd.ExecuteScalar(); cmd.Parameters.Clear();
int result = cmd.ExecuteNonQuery();
Console.WriteLine("Test");
//Check Error
if (result < 0)
{
System.Windows.Forms.MessageBox.Show("Error inserting data into database!");
}
}
}
}
}
}
System.Windows.Forms.MessageBox.Show("Employee data successfully updated.");
}
catch (SqlException ex)
{
System.Windows.Forms.MessageBox.Show(string.Format("\nMessage ---\n{0}", ex.Message));
}
}
private void BtnGridUpdate_Click(object sender, EventArgs e)
{
UpdateInternal0();
}