Binding a datatable to a chart at runtime

Hello,
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)
            Chart1.DataBindTable(dt)

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

AS 

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
ON DV.TSID = TS.TSID 
WHERE DV.DataVersionID = @dvid
AND 
M.MetricName = @metricname;
GO

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)

        Try
            Dim dAdp As New SqlDataAdapter(cmdA)
            Dim dTable As New DataTable
            CnSql.Open()
            dAdp.Fill(dTable)
            dAdp.Dispose()
            Return dTable

        Catch excA As SqlClient.SqlException
            MsgBox(excA.Message)
            Return Nothing
        Finally

            CnSql.Close()
        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:
Hello,

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.DataBindTable(dt)
            Chart1.DataSource = dt
            Chart1.Series("Series1").XValueMember = "ForecastYear"
            Chart1.Series("Series1").YValueMembers = "MetricValue"
            Chart1.DataBind()


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

Open in new window

0

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.