Binding a datatable to a chart at runtime

Below is a stored procedure that I created that will result a set of records,  This stored procedure is called by a fshared unction I created that is listed below the stored procedure. This function returns a datatable to the calling procedure.  I want to use this data table to a chart.   I've tried the code below but I'm getting a cast error.

        For Each g As GraphReport In graphs
            Dim dt As New DataTable
            dt = ReportDB.GetGraphData(g.DataVersionId, g.MetricName)

            strmessage = strmessage & g.DataVersionId.ToString & vbTab & g.MetricName.ToString & vbTab & g.GraphType.ToString & vbTab & g.GraphTitle.ToString & vbCrLf
alter PROCEDURE [dbo].[up_GetGraphDataSet] 
@dvid AS nvarchar(255),
@metricname as NVARCHAR(255)


SELECT   dv.DataVersionID,DV.BudgetYear, DV.ForecastYear , M.MetricName ,
SUBSTRING(DV.DataVersionID,CHARINDEX('-',DV.DataVersionID) + 1, CHARINDEX('-',DV.DataVersionID,CHARINDEX('-',DV.DataVersionID) + 1) - CHARINDEX('-',DV.DataVersionID) - 1)   as Technology 
FROM DataVersion DV
inner join Metrics M
ON DV.DataVersionRowID = M.DataVersionRowID
INNER JOIN LtbTechnologySet  TS
WHERE DV.DataVersionID = @dvid
M.MetricName = @metricname;

Public Shared Function GetGraphData(ByVal strDataVersionId As String, ByVal strmetricname As String) As DataTable
        Dim CnSql As SqlConnection = BadsDB.GetConnection
        Dim cmdA As SqlCommand
        Dim strmessage As String = ""

        cmdA = New SqlClient.SqlCommand("up_GetGraphDataSet", CnSql)
        cmdA.CommandType = CommandType.StoredProcedure
        'add some parameters 
        cmdA.Parameters.AddWithValue("@dvid", strDataVersionId)
        cmdA.Parameters.AddWithValue("@metricname", strmetricname)

            Dim dAdp As New SqlDataAdapter(cmdA)
            Dim dTable As New DataTable
            Return dTable

        Catch excA As SqlClient.SqlException
            Return Nothing

        End Try
    End Function

Open in new window

Juan VelasquezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan VelasquezAuthor Commented:

It looks like I have it working via the attached code.  I'll doublecheck on Monday
For Each g As GraphReport In graphs
            Dim dt As New DataTable
            dt = ReportDB.GetGraphData(g.DataVersionId, g.MetricName)
            Chart1.DataSource = dt
            Chart1.Series("Series1").XValueMember = "ForecastYear"
            Chart1.Series("Series1").YValueMembers = "MetricValue"

            strmessage = strmessage & g.DataVersionId.ToString & vbTab & g.MetricName.ToString & vbTab & g.GraphType.ToString & vbTab & g.GraphTitle.ToString & vbCrLf

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.