I am trying to show Employee Name on basis of Personal_No but while doing I got error
The variable name '@Personnel_Number' has already been declared. Variable names must be unique within a query batch or stored procedure
My code:
protected void txtEmployeeNumber_TextChanged(object sender, EventArgs e)
{
string EmployeeNo = "";
string constring = ConfigurationManager.ConnectionStrings["SQLDBConnection"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("select Employee_Name from [138.201.225.134].[iProfile].[dbo].[tbl_Employee] WHERE Personnel_Number= @Personnel_Number", con);
cmd.CommandType = CommandType.Text;
foreach (GridViewRow row in grdRegister.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
EmployeeNo = (row.Cells[1].FindControl("txtEmployeeNumber") as TextBox).Text;
}
cmd.Parameters.AddWithValue("@Personnel_Number", EmployeeNo);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
(row.Cells[2].FindControl("txtEmployeeName1") as TextBox).Text = dt.Rows[0]["Employee_Name"].ToString();
}
}
}
If i tried to keep (row.Cells[2].FindControl("txtEmployeeName1") as TextBox).Text = dt.Rows[0]["Employee_Name"].ToString(); out side of foreach loop then error occurs on row
Please guide me what should i change in this code I am getting error on cmd.ExecuteNonQuery(); while my loop runs 2nd time. My text box are inside the Grid view