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
<!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>