i'd like to create google chrat visualisatin in my website using c#.
For that i need to :
- connect to SQL server and get SQL data table --> DONE
- transform SQL data table to c# DataTable --> DONE
- parse DataTable to google visualisation array --> PROBLEM
I would have to do soemthing in this way, but it doesn't work.
@using System.Data;
@using System.Collections.Generic;
@using System.Data.SqlClient;
@{
Layout = null;
string connectionString="Server=192.168.1.125,1433\\sqlexpress; Database=HEK; Uid=sa; Pwd=xxxxxxx";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT Datum, Sava, RPWS1 FROM vws", conn))
{
adapter.Fill(dt);
Object o = dt.Rows[0]["RPWS1"];
List<string> rowsList = new List<string>();
rowsList.Add("['Sava', 'RPWS1', 'RPWS2', 'RPWS3']");
};
};
//PROBLEMATIC PART
foreach (DataRow row in dt.rowsList)
{
rowsList.Add("[" + row.item["Datum"] + ", " + row.item["Sava"] + ", " + row.item["RPWS1"] + "]");
}
String rows = String.Join(", ", rowsList);
}
I get error saying :
Error 3 'System.Data.DataTable' does not contain a definition for 'rowsList' and no extension method 'rowsList' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?) c:\inetpub\wwwroot\Gecko_Dev\Members\testni.cshtml
@using System.Data;
@using System.Collections.Generic;
@using System.Data.SqlClient;
@{
Layout = null;
string connectionString="Server=192.168.1.125,1433\\sqlexpress; Database=HEK; Uid=sa; Pwd=lubelincek12121";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT Datum, Sava, RPWS1 FROM vws", conn))
{
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
rowsList.Add("[" + row.item["Datum"] + ", " + row.item["Sava"] + ", " + row.item["RPWS1"] + "]");
}
//Object o = dt.Rows[0]["RPWS1"];
//Response.Write(o);
};
};
}
I get the two errors :
1. 'System.Data.DataRow' does not contain a definition for 'item' and no extension method 'item' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?) c:\inetpub\wwwroot\Gecko_Dev\Members\testni.cshtml
2. The name 'rowsList' does not exist in the current context c:\inetpub\wwwroot\Gecko_Dev\Members\testni.cshtml
<div></div> <div>i figured the Item error out. Seem like in c# you can write it directly like :</div> <div>
@using System.Data;
@using System.Collections.Generic;
@using System.Data.SqlClient;
@{
Layout = null;
string connectionString="Server=xxx.xxx.xxx.xxx\\sqlexpress; Database=HEK; Uid=xx; Pwd=xx";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT Datum, Sava, RPWS1 FROM vws", conn))
{
adapter.Fill(dt);
List<string> rowsList = new List<string>();
rowsList.Add("['Datum', 'Sava', 'RPWS1']");
foreach(DataRow row in dt.rowsList)
{
rowsList.Add("[" + row["Datum"] + ", " + row["Sava"] + ", " + row["RPWS1"] + "]");
}
String rows = String.Join(", ", rowsList);
};
};
}
</div> <div> <div>But the error about rowsList remains. I can't figure out what's wrong....</div> <div></div> <div>Html and jscript part is like this :</div> <div>
<script type='text/javascript' src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
function drawChart () {
var data = google.visualization.arrayToDataTable(@rowsList);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {
height: 350,
width: 900,
title: 'Absolutna kota vode v tesnilni zavesi',
vAxis: {
maxValue: 165.5,
minValue: 159,
title: 'absolutna kota'
}
});
}
google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawChart);
</script>
<div id="chart_div"></div>
Matevz
Member
2 Points
42 Posts
Passig DataTable to google.Visualisation.Array
Feb 21, 2013 09:09 AM|LINK
Hi,
i'd like to create google chrat visualisatin in my website using c#.
For that i need to :
- connect to SQL server and get SQL data table --> DONE
- transform SQL data table to c# DataTable --> DONE
- parse DataTable to google visualisation array --> PROBLEM
I would have to do soemthing in this way, but it doesn't work.
@using System.Data; @using System.Collections.Generic; @using System.Data.SqlClient; @{ Layout = null; string connectionString="Server=192.168.1.125,1433\\sqlexpress; Database=HEK; Uid=sa; Pwd=xxxxxxx"; DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT Datum, Sava, RPWS1 FROM vws", conn)) { adapter.Fill(dt); Object o = dt.Rows[0]["RPWS1"]; List<string> rowsList = new List<string>(); rowsList.Add("['Sava', 'RPWS1', 'RPWS2', 'RPWS3']"); }; }; //PROBLEMATIC PART foreach (DataRow row in dt.rowsList) { rowsList.Add("[" + row.item["Datum"] + ", " + row.item["Sava"] + ", " + row.item["RPWS1"] + "]"); } String rows = String.Join(", ", rowsList); }Error 3 'System.Data.DataTable' does not contain a definition for 'rowsList' and no extension method 'rowsList' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?) c:\inetpub\wwwroot\Gecko_Dev\Members\testni.cshtml
Any ideas?
Nasser Malik
Star
11260 Points
1731 Posts
Re: Passig DataTable to google.Visualisation.Array
Feb 21, 2013 09:28 AM|LINK
DataTable contains collection of Rows and "Rows" property will get you all the rows
foreach (DataRow row in dt.Rows) { rowsList.Add("[" + row.item["Datum"] + ", " + row.item["Sava"] + ", " + row.item["RPWS1"] + "]"); }Skype: maleknasser1
Matevz
Member
2 Points
42 Posts
Re: Passig DataTable to google.Visualisation.Array
Feb 21, 2013 09:49 AM|LINK
Hi,
tnx for reply.
I tried that, bu tit doesn't work ;
@using System.Data; @using System.Collections.Generic; @using System.Data.SqlClient; @{ Layout = null; string connectionString="Server=192.168.1.125,1433\\sqlexpress; Database=HEK; Uid=sa; Pwd=lubelincek12121"; DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT Datum, Sava, RPWS1 FROM vws", conn)) { adapter.Fill(dt); foreach (DataRow row in dt.Rows) { rowsList.Add("[" + row.item["Datum"] + ", " + row.item["Sava"] + ", " + row.item["RPWS1"] + "]"); } //Object o = dt.Rows[0]["RPWS1"]; //Response.Write(o); }; }; }I get the two errors :
1. 'System.Data.DataRow' does not contain a definition for 'item' and no extension method 'item' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?) c:\inetpub\wwwroot\Gecko_Dev\Members\testni.cshtml
2. The name 'rowsList' does not exist in the current context c:\inetpub\wwwroot\Gecko_Dev\Members\testni.cshtml
Ideas?
Matevz
Member
2 Points
42 Posts
Re: Passig DataTable to google.Visualisation.Array
Feb 21, 2013 09:35 PM|LINK
Hi,
<div></div> <div>i figured the Item error out. Seem like in c# you can write it directly like :</div> <div>
@using System.Data; @using System.Collections.Generic; @using System.Data.SqlClient; @{ Layout = null; string connectionString="Server=xxx.xxx.xxx.xxx\\sqlexpress; Database=HEK; Uid=xx; Pwd=xx"; DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT Datum, Sava, RPWS1 FROM vws", conn)) { adapter.Fill(dt); List<string> rowsList = new List<string>(); rowsList.Add("['Datum', 'Sava', 'RPWS1']"); foreach(DataRow row in dt.rowsList) { rowsList.Add("[" + row["Datum"] + ", " + row["Sava"] + ", " + row["RPWS1"] + "]"); } String rows = String.Join(", ", rowsList); }; }; }</div> <div> <div>But the error about rowsList remains. I can't figure out what's wrong....</div> <div></div> <div>Html and jscript part is like this :</div> <div>
<script type='text/javascript' src="http://www.google.com/jsapi"></script> <script type="text/javascript"> function drawChart () { var data = google.visualization.arrayToDataTable(@rowsList); var chart = new google.visualization.LineChart(document.getElementById('chart_div')); chart.draw(data, { height: 350, width: 900, title: 'Absolutna kota vode v tesnilni zavesi', vAxis: { maxValue: 165.5, minValue: 159, title: 'absolutna kota' } }); } google.load('visualization', '1', {packages: ['corechart']}); google.setOnLoadCallback(drawChart); </script> <div id="chart_div"></div>Any ideas, cause i'm lost!
Thanks, m
Paul Linton
Star
13407 Points
2533 Posts
Re: Passig DataTable to google.Visualisation.Array
Feb 22, 2013 04:21 AM|LINK
it should be
foreach(DataRow row in dt.Rows)
as mentioned earlier.
Change nothing else. Just this one line.