0

I have a datatable object which returns me 1 one based on some condition. What I want is, I want to fill those datatable values in the control from the aspx side. For that I want to convert those datatable into json. How to convert it and bind to the aspx page?

Here is the code I tried:

protected void GET_VSAT_FORM_DATA(string SapId, string CandidateId)
{ 
    try
    {
        DataTable dtGetData = new DataTable();
        CommonDB CDB = new CommonDB();
        dtGetData = CDB.GET_VSAT_DATA(SapId, CandidateId);
    }
    catch (Exception)
    {   
        throw;
    }
}

UPDATE

<span>CIRCLE :</span>
    <input type="text" id="txtCircle" style="width: auto;" readonly="true" />
    <br />

    <span>CANDIDATE ID :</span>
    <input type="text" id="txtCandidate" style="width: auto;" readonly="true" />
    <br />

    <span>SITE ID :</span>
    <input type="text" id="txtSiteId" style="width: auto;" readonly="true" />
    <br />

    <span>PRIORITY ID :</span>
    <input type="text" id="txtPriority" style="width: auto;" readonly="true" />
    <br />

    <span>SITE NAME :</span>
    <input type="text" id="txtSiteName" style="width: auto;" readonly="true" />
    <br />

    <span>SAP ID :</span>
    <input type="text" id="txtSapId" style="width: auto;" readonly="true" />
    <br />
James Z
  • 12,209
  • 10
  • 24
  • 44
Nad
  • 4,605
  • 11
  • 71
  • 160
  • you can do something like this: https://stackoverflow.com/questions/11138035/convert-datatable-to-json-with-key-per-row#answers-header – Dot_NET Pro Jun 14 '17 at 11:28
  • @Dot_NETPro: while installing `JSON.NET` i am getting installation error as `'Newtonsoft.Json' already has a dependency defined for 'Microsoft.CSharp'.` – Nad Jun 14 '17 at 11:29
  • @Dot_NETPro: Resolved that issue and converted into `json`. now how to bind it in aspx side? – Nad Jun 14 '17 at 11:36
  • 1
    How you want your data to be displayed? Like table? Like list? – Chetan Jun 14 '17 at 11:40
  • @ChetanRanpariya: Hi Chetan, i want to bind it to the controls, which is in html format in the aspx side – Nad Jun 14 '17 at 11:42
  • Possible duplicate of [How to convert datatable to json string using json.net?](https://stackoverflow.com/questions/2979922/how-to-convert-datatable-to-json-string-using-json-net) – VDWWD Jun 14 '17 at 12:40
  • @VDWWD: i have one more requirement here on how to bind it to html controls ? please let me know how to achieve that. – Nad Jun 14 '17 at 12:41
  • Why? you can bind a datatable to Controls directly. This sounds like databinding with extra steps. And to "bind" json as you put it you need javascript/jquery. – VDWWD Jun 14 '17 at 12:43
  • @VDWWD: i have no idea how to bind. please let me know if you have any clue, it would be great. – Nad Jun 14 '17 at 12:44
  • You use jquery/javascript for that. There are plenty of tutorials out there. – VDWWD Jun 14 '17 at 21:06

1 Answers1

0

You can use below method for convert dataTable to JSON, Source

public string DataTableToJSONWithJavaScriptSerializer(DataTable table) 
{  
      JavaScriptSerializer jsSerializer = new JavaScriptSerializer();  
      List < Dictionary < string, object >> parentRow = new List < 
      Dictionary < string, object >> ();  
      Dictionary < string, object > childRow;  
      foreach(DataRow row in table.Rows) 
      {  
         childRow = new Dictionary < string, object > ();  
         foreach(DataColumn col in table.Columns) 
         {  
              childRow.Add(col.ColumnName, row[col]);  
         }  
         parentRow.Add(childRow);  
      }  

     return jsSerializer.Serialize(parentRow);  
} 

this will return you the JSON string.place above method in some where else in your class and just call below method like this

var convertedJSON = DataTableToJSONWithJavaScriptSerializer(dtGetData);
Shakir Ahamed
  • 1,290
  • 3
  • 16
  • 39
  • my method is protected. Do I need to create another method and return it ? – Nad Jun 14 '17 at 11:43
  • i mean how to call `DataTableToJSONWithJavaScriptSerializer` in my `GET_VSAT_FORM_DATA` method ? – Nad Jun 14 '17 at 11:44
  • good one shakir, Now variable `convertedJSON`. how to use this to bind the control from aspx side.. any idea? – Nad Jun 14 '17 at 11:49
  • I mean, suppose I have one textbox with this variable. one of those values I want to fill with that textbox. how will I do this ? – Nad Jun 14 '17 at 11:51
  • The text box is a server control or html input control? – Chetan Jun 14 '17 at 11:56
  • @ChetanRanpariya: it is an html control – Nad Jun 14 '17 at 12:03
  • @ShakirAhamed: i have updated my question for html part. and which `script` you are talking about ? – Nad Jun 14 '17 at 12:03
  • @ShakirAhamed Please do specify the source from where you have copied the code, credit to the original author: http://www.c-sharpcorner.com/UploadFile/9bff34/3-ways-to-convert-datatable-to-json-string-in-Asp-Net-C-Sharp/ – Dot_NET Pro Jun 14 '17 at 13:21
  • @Dot_NETPro, I have already specified the source please check – Shakir Ahamed Feb 26 '20 at 08:30