none
How to populate a win forms chart datasource with a Entity Framework stored procedure? RRS feed

  • Question

  • I am using VB.NET/ Visual Studio 2015. Entity Framework 6. Windows Forms Application. SQL Server 2014

    How do I populate a win forms chart datasource with a Entity Framework stored procedure?

    Thursday, June 8, 2017 1:57 AM

All replies

  • Hi kingelk,

    Based on your description, I create a simple demo for your reference.

    #Table.

    CREATE TABLE [dbo].[Account] (
        [account_id] INT          NOT NULL,
        [Name]       VARCHAR (50) NULL,
        [Count]      INT          NULL
    );

    #Stored Procedure

    CREATE PROCEDURE [dbo].[GetAccountInfo]
    	
    AS
    	SELECT * from [dbo].[Account]
    RETURN 0
    

    #Usage(Ef code first)

    using System;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                using (var db = new AdoNetContext())
                {
                    var query = db.Accounts.SqlQuery("GetAccountInfo").ToList();
    
                    string[] N = new string[query.Count()];
                    int[] M = new int[query.Count()];
    
                    int i = 0;
    
                    foreach (var item in query)
                    {
                        N[i] = item.Name;
                        M[i] = (int)item.Count;
                        i++;
                    }
                    chart1.Series[0].Points.DataBindXY(N, M);
                }
            }
        }
    }
    

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 8, 2017 7:05 AM
    Moderator