Pramod Ramadand Pramod Ramadand - 3 months ago 31
MySQL Question

Search functionality in Restful Web services

I am developing a search functionality in restful web service,from code i am passing searching string parameter to a method. here i am not able get the matched string text where i sending string from request.while showing null value in

dr.read()
and skipping the while loop.

public MemberEntity Search(string prefix)
{
try
{
MemberEntity ObjMember = new MemberEntity();
string sql = string.Format(@"select first_name, last_name from member_master where first_name like ('@prefix')");


using (MySqlConnection conn = new MySqlConnection(UtilityHelper.getConn()))
{
//using (MySqlCommand cmd = new MySqlCommand(string.Format("select first_name, last_name from member_master where first_name like ('@prefix%')"), conn))
using (MySqlCommand cmd = new MySqlCommand(sql.ToString(), conn))
{
conn.Open();
cmd.Parameters.AddWithValue("@prefix", prefix);
cmd.CommandType = CommandType.Text;
using (MySqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
ObjMember.Name = string.Format("{0} {1}", dr["first_name"].ToString(), dr["last_name"].ToString());
}
}
}
}
return ObjMember;
}
catch (Exception ex)
{
throw ex;
}
#endregion
}

Answer

Your sql should be like this.

string sql = string.Format(@"select first_name, last_name from member_master where first_name like '" + @prefix + "%'");