You can create a c# function and called it from client side. the same code you will have to write in GetData function and that will return string as you have serialize using JavaScriptSerializer
.
var Json = <%= GetData(); %>
That will render page like this,
var Json = [["1","John","xyz","12000"],["2","Mike","pqr","15000"],["3","Nick","fjdu","18000"],["4","Brad","wee","22000"]]
And you can access values like Json[0][0]
would return 1.
Code behind
public partial class Somepage : System.Web.UI.Page
{
public string GetData()
{
SqlConnection con = new SqlConnection("Data Source=COMP7;Initial Catalog=GK_Practice;User ID=sa;Password=SQLEXPRESS");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from employee";
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
int rowcount=0, columncount;
while(rd.Read())
{
rowcount++;
}
columncount = rd.FieldCount;
rd.Close();
rd = cmd.ExecuteReader();
string[][] str=new string[rowcount][];
int i = 0;
while(rd.Read())
{
str[i] = new string[columncount];
for (int j = 0; j < columncount; j++)
{
str[i][j] = rd.GetValue(j).ToString();
}
i++;
}
Label2.Text = str[1][1].ToString();
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize((object)str);
rd.Close();
con.Close();
return json;
}
protected void Page_Load(object sender, EventArgs e)
{
// Just to feel you this is code behind
}
}
Clint side (Javascript)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var Json = <%= GetData(); %>
</script>
</head>