public string toJSON(SqlDataReader o)
{
StringBuilder s = new StringBuilder();
s.Append(\"[\");
if (o.HasRows)
while (o.Read())
s.App
This can't be that hard. This is what I've done when I want to return search results to a web page as JSON.
First, have a class like this
public class SearchResult
{
public string model_no { get; set; }
public string result_text { get; set; }
public string url { get; set; }
public string image_url { get; set; }
}
and then have the code below.
string sql_text = "select * from product_master where model_no like @search_string and active=1";
SqlConnection connection = new SqlConnection(sql_constr);
SqlCommand cmd = new SqlCommand(sql_text, connection);
cmd.Parameters.AddWithValue("@search_string", "%" + search_string + "%");
connection.Open();
SqlDataReader rdr = cmd.ExecuteReader();
List searchresults = new List();
while (rdr.Read())
{
SearchResult sr = new SearchResult();
sr.model_no = rdr["model_no"].ToString();
sr.result_text = rdr["product_name"].ToString();
sr.url = rdr["url_key"].ToString();
searchresults.Add(sr);
}
connection.Close();
//build json result
return Json(searchresults, JsonRequestBehavior.AllowGet);
this works for me very well..