0

I have a problem with displaying data from a column that is in SQL Server, to insert as x and y in my chart in asp.net the problem is this, so 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, thank you.

Here is 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>
  • Use a datatable Load method to fill it with the datareader then bind the datatable – Steve Apr 12 '17 at 12:40
  • Use a sqldataadapter which will automatically fill a datatable that you can bind to : https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter(v=vs.110).aspx – jdweng Apr 12 '17 at 12:42
  • how can i do that@Steve? – gomes diogo Apr 12 '17 at 12:43
  • You can also call `AsEnumerable()` on any `DataTable` to convert it to an `IEnumerable`. Beyond that, this is actually a possible duplicate of (or at least answered by) this:http://stackoverflow.com/questions/6939054/best-method-to-use-idatareader-as-ienumerablet – CDove Apr 12 '17 at 12:43
  • can you give me practicle exemples? – gomes diogo Apr 12 '17 at 12:44

2 Answers2

0

You could load your datareader into a data table:

DataTable dt = new DataTable();
SqlDataReader rdr = cmd.ExecuteReader();
dt.Load(rdr);
dataReader.Close();
Chart1.DataBindTable(dt,"Consumo_Medio_Real");
BeanFrog
  • 2,297
  • 12
  • 26
  • im having problems in use datatable it gives this error :Error 4 'DataTable' is an ambiguous reference between 'Microsoft.Office.Interop.Excel.DataTable' and 'System.Data.DataTable' – gomes diogo Apr 12 '17 at 12:47
  • when use type "DataTable" use "System.Data.DataTable" – M84 Apr 12 '17 at 12:49
  • Have you used the full reference in both places? ie `System.Data.DataTable dt = new System.Data.DataTable();`? – BeanFrog Apr 12 '17 at 13:17
0

Try with this:

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();
        //Add datatable...
        System.Data.DataTable dt  = new DataTable();
        dt.Load(rdr);
        var enumerableTable = (dt as System.ComponentModel.IListSource).GetList();
        chart1.DataBindTable(enumerableTable , "X");

here you have an answer...

Hope this help.

Community
  • 1
  • 1
M84
  • 727
  • 6
  • 14
  • im having problems in use datatable it gives this error :Error 4 'DataTable' is an ambiguous reference between 'Microsoft.Office.Interop.Excel.DataTable' and 'System.Data.DataTable' – gomes diogo Apr 12 '17 at 12:47
  • when use type "DataTable" use "System.Data.DataTable" – M84 Apr 12 '17 at 12:48