0
 public class theatresController : ApiController
   {
    [HttpGet]
   public static DataTable GetAlltheatredet()
     {
       try
         {
      string connString = "Server=localhost;database=mytable;uid=root;";
      string query = "SELECT TheatreName FROM `mytable`.`theatredetails`";
      MySqlDataAdapter ma = new MySqlDataAdapter(query, connString);
      DataSet DS = new DataSet();
      ma.Fill(DS);
      return DS.Tables[0];
         }
catch (MySqlException e)
        {
            throw new Exception(e.Message);
        }
      }

I am fetching my table data from DB by using datatable. How can I convert all these datas to json

I tried by using this in an html file

$(document).ready(function ()
    {
      $("#Theaterdet").click(function ()
        {
            alert("in document submit");
            $.ajax({
                type: "GET",
                url: "/api/theatres/",
                dataType: 'json',
success: function (data) 
                {

                },
                error: function () 
                {
                    alert("Error while invoking the Web API");
                }
            });
        });
    });

But I got error... How can I implement this functionality?

jarlh
  • 42,561
  • 8
  • 45
  • 63
hakkeem
  • 185
  • 1
  • 4
  • 13
  • i have been using this method `public Stream ConvertDataSetToJson(DataSet ds) { try { byte[] resultBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ds, Formatting.Indented)); WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; return new MemoryStream(resultBytes); } catch (Exception ex) { throw ex; } }` – Krsna Kishore Oct 06 '15 at 10:27
  • [Please don't return a DataTable from a web service](http://stackoverflow.com/questions/25874224/why-returning-dataset-or-data-table-from-wcf-service-is-not-a-good-practice-wha). Use an ORM and/or return a DTO. If you don't want that and want help with your current approach, at least share the error you get. – CodeCaster Oct 06 '15 at 10:27
  • @CodeCaster:Error while invoking the Web API.The control can't go to the apicontroller – hakkeem Oct 06 '15 at 10:30
  • The `error` function of an `$.ajax()` request has parameters. Using those you can obtain the actual error. – CodeCaster Oct 06 '15 at 10:31
  • @CodeCaster:what i need to write in success function? – hakkeem Oct 06 '15 at 10:32

0 Answers0