Sqldatareader Incompatible Databindtable does not implement Ienumerable

Asked

Viewed 33 times

2

I have a problem showing the data of a column on , to insert as x and y in my Chart in .
The problem is this, it seems the method (DataBindTable) is waiting for IEnumerable but my SqlDataReader does not implement IEnumerable.
If you could give me examples of how to overcome this difficulty I would appreciate.

Here follows the insertion code:

protected void Chart1_Load13(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand("SELECT [Consumo_Medio_Real], [Tipo_de_Fatura]  FROM [dbo].[t_faturas]", con);
        con.Open();

        SqlDataReader rdr = cmd.ExecuteReader();
        Chart1.DataBindTable(rdr,"Consumo_Medio_Real");
    }
}

This is the connection code:

<connectionStrings>
    <add name="CS" connectionString="Data Source=ASUS;Initial Catalog=DB_SACC;Persist Security Info=True;User ID=sa;Password=1234" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

Here are the usings:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Windows.Forms; 
using System.IO;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Data;
using System.ComponentModel;
using System.Threading;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Configuration;
using System.Web.UI.DataVisualization.Charting;
using System.Data.DataTable;

1 answer

1

This solution can help you:

var dt = new System.Data.DataTable();
dt.Load(rdr);

var enumerableTable = (dt as System.ComponentModel.IListSource).GetList();
Chart1.DataBindTable(enumerableTable, "Consumo_Medio_Real");
  • thanks this to give this error any idea how to fix ? Error 2 'Datatable' is an ambiguous Reference between 'Microsoft.Office.Interop.Excel.Datatable' and 'System.Data.Datatable"

  • @Diogogomes updated response. Caused ambiguity pq in their using is already using a reference that also implements a class DataTable.

  • I just put the usings to take a look thanks has been relentless @Smael

  • What you yourself quoted: using Microsoft.Office.Interop.Excel. Look at this question for other information.

  • or remove Microsoft.Office.Interop.Excel? @Ismael

  • Run the procedures of the question I sent you and this will clean up all unused references. If you remove manually, unknowingly, you can cause errors in your code. You can use it the way I put it in the answer, no problem: var dt = new System.Data.DataTable();

  • I thank you for your cooperation

  • thanks for your help this solved, was me quite user@ismael

  • @Diogogomes Mark this answer as accepted if you really solved your problem. At tour this is well explained.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.