Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, using the C# code below with the HTML (also below), when i click the button i will have the following string jSonString
I cant read the string in the javascript of the html page, i want to display it using angular JS on the same page
any help ?

[{"QuestionID":1,"Answer":"E","AnswerID":null,"QuestionNumber":1,"QuestionText":"Which language does Microsoft use to create apps","test_id":"1","theID":1},{"QuestionID":2,"Answer":"D","AnswerID":null,"QuestionNumber":2,"QuestionText":"Which of the following is not included in the Cs Language","test_id":"1","theID":2},{"QuestionID":3,"Answer":"A","AnswerID":null,"QuestionNumber":3,"QuestionText":"Which of the statements below is a proper method call for c","test_id":"1","theID":3},{"QuestionID":4,"Answer":"B","AnswerID":null,"QuestionNumber":4,"QuestionText":"In the WriteLine Method of the ","test_id":"1","theID":4}] 




using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;



public partial class _Default : System.Web.UI.Page
{
    public DataTable GetDataTable()
    {
        DataTable dataTable = new DataTable();
        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|Quiz.mdb"))

        {
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from Questions";
            cmd.CommandType = CommandType.Text;
            
            if (conn.State != ConnectionState.Open)
                conn.Open();

            OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            dataTable.Load(dr);
        }
        return dataTable;
    }

    public String ConvertDataTableTojSonString(DataTable dataTable)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer =
               new System.Web.Script.Serialization.JavaScriptSerializer();

        List<Dictionary<String, Object>> tableRows = new List<Dictionary<String, Object>>();

        Dictionary<String, Object> row;

        foreach (DataRow dr in dataTable.Rows)
        {
            row = new Dictionary<String, Object>();
            foreach (DataColumn col in dataTable.Columns)
            {
                row.Add(col.ColumnName, dr[col]);
            }
            tableRows.Add(row);
        }
        return serializer.Serialize(tableRows);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        
        DataTable dataTable = GetDataTable();
        String jSonString = ConvertDataTableTojSonString(dataTable);
        gv.DataSource = dataTable;
       
    }
}



I use this html

XML
<!DOCTYPE html>
<html ng-app="questionsApp">
<head>
    <title></title>
    <script src="js/angular.js"></script>
</head>
<body>

    <div ng-controller="questionsController">
      search:<input type="text" ng-model="search" />




    <table>
        <tr ng-repeat="i in questions | filter:search">

             <td>
            {{i.QuestionID}}</td>

             <td>
            {{i.QuestionText }}</td>
        </tr>
        </table>
    <script>
        var app = angular.module('questionsApp', []);
        app.controller('questionsController', function ($scope) {
            $scope.questions = [{ "QuestionID": 1, "Answer": "koko", "AnswerID": null, "QuestionNumber": 1, "QuestionText": "koko Which language does Microsoft use to create apps", "test_id": "1", "theID": 1 }, { "QuestionID": 2, "Answer": "D", "AnswerID": null, "QuestionNumber": 2, "QuestionText": "Which of the following is not included in the Cs Language", "test_id": "1", "theID": 2 }, { "QuestionID": 3, "Answer": "A", "AnswerID": null, "QuestionNumber": 3, "QuestionText": "Which of the statements below is a proper method call for c", "test_id": "1", "theID": 3 }, { "QuestionID": 4, "Answer": "B", "AnswerID": null, "QuestionNumber": 4, "QuestionText": "In the WriteLine Method of the ", "test_id": "1", "theID": 4 }];

        });
    </script>
        </div>
</body>
</html>
Posted
Updated 7-Aug-18 4:19am
v4
Comments
ZurdoDev 14-Mar-14 17:45pm    
Where are you stuck?
Mohamed Kamal 14-Mar-14 17:52pm    
i am really new to angularjs, all samples i checked is dealing with a .json file that already exists in the application using the url parameter. i could not find a source to know how to display the JSON just generated by the C#
Mohamed Kamal 14-Mar-14 17:57pm    
should i use $scope.save, is this right? then what?
ZurdoDev 14-Mar-14 18:42pm    
I haven't used angular either. But at least you have added detail now so that someone who has may be able to help.

1 solution

Please check the fiddle: http://jsfiddle.net/mjaric/pJ5BR/[^] which shows how to render Json using Angular.js
 
Share this answer
 
Comments
Mohamed Kamal 18-Mar-14 4:30am    
Thanks a lot, i would also appreciate it if you can guide me to a good source for routing and CRUD operation with a SQL server backend

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900