ikapalad-8134 avatar image
0 Votes"
ikapalad-8134 asked LanHuang-MSFT commented

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);

         string command2 = "SELECT * FROM [Products] WHERE UnitPrice < 10";
         SqlCommand cmd1 = new SqlCommand(command2, con);
         adapter.SelectCommand = cmd1;
         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();
     public class ChartSqlData
         public ChartSqlData(string xvalue, double yvalue)
             this.ProductName = xvalue;
             this.UnitPrice = yvalue;
         public string ProductName
         public double UnitPrice
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @ikapalad-8134,
According to your description, you don't want to use connection strings. However, the data must to be inside the database, so I suggest you could use EF. Do you care of it?
Best regards,
Lan Huang

0 Votes 0 ·

0 Answers