Here is my code
public string LeaderIdLookup(string leadername)
{
string step = null;
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select EmpId,Fullname from Employee where FullName like '@LeaderName'";
cmd.Parameters.Add(new SqlParameter("LeaderName", SqlDbType.VarChar));
cmd.Parameters["LeaderName"].Value = leadername.Trim();
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
step = "assigning the value from datareader to the variable lookup as a string (leaderidlookup) ";
if (dr.HasRows)
Lookup = dr[0].ToString();
else
Lookup = "no rows found";
dr.Close();
return Lookup;
}
catch (SqlException ex)
{
Lasterror = string.Format("step {0} threw sql error {1}", step, ex.Message);
Debug.Print(Lasterror);
return string.Empty;
}
catch (Exception ex)
{
Lasterror = string.Format("step {0} threw error {1}", step, ex.Message);
Debug.Print(Lasterror);
return string.Empty;
}
}
The problem is that SqlDataReader does not return any rows I have a hunch that it has to do with the Parameter substitution because when i hardcode a name in there instead of using a parameter it works perfectly
I can not seem to figure out where im going wrong.
new SqlParameter("@LeaderName", SqlDbType.VarChar));