ASP.NET MVC: How to create 2 pie chart in 2 different table data in sql server
Hello Community!
I'm trying to create a 2 pie chart in different table data bind in sql server.
Also I'm trying avoid this kind of connection string, I wondered how can I connect as an object and model?
public ActionResult Index()
{
List<ChartSqlData> data = new List<ChartSqlData>();
string connectionString = null;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet dataset = new DataSet();
connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\Incidents\EJ2\MVC Samples\TestSample\AspNetMvc5\App_Data\NORTHWND.MDF;Integrated Security=True;";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string command2 = "SELECT * FROM [Products] WHERE UnitPrice < 10";
SqlCommand cmd1 = new SqlCommand(command2, con);
adapter.SelectCommand = cmd1;
adapter.Fill(dataset);
for (var i = 0; i < dataset.Tables[0].Rows.Count; i++)
{
string x1 = Convert.ToString(dataset.Tables[0].Rows[i]["ProductName"]);
double y1 = Convert.ToDouble(dataset.Tables[0].Rows[i]["UnitPrice"]);
data.Add(new ChartSqlData(x1, y1));
}
ViewBag.dataSource = data;
return View();
}
[Serializable]
public class ChartSqlData
{
public ChartSqlData(string xvalue, double yvalue)
{
this.ProductName = xvalue;
this.UnitPrice = yvalue;
}
public string ProductName
{
get;
set;
}
public double UnitPrice
{
get;
set;
}
}