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

chtullu135Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
chtullu135Connect With a Mentor Author 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
All Courses

From novice to tech pro — start learning today.