2017-02-26 9 views
0

リストに値を追加しようとしましたが、引き続きエラー "ArgumentOutOfRangeException"が発生しました。私はlivecharts(チャートライブラリ)を使用しています。C#のリストに値を追加する方法

ここに私のコードです:

string constring = "server=localhost;port=3306;username=root;password=root"; 
     string Query = "SELECT Therapist, COUNT(*) AS magnitude FROM dbinfo.tblorder GROUP BY Therapist ORDER BY magnitude DESC"; 
     MySqlConnection con = new MySqlConnection(constring); 
     MySqlCommand cmdDB = new MySqlCommand(Query, con); 
     MySqlDataReader myReader; 


     try 
     { 
      con.Open(); 
      myReader = cmdDB.ExecuteReader(); 

      while (myReader.Read()) 
      { 
       int valuez = myReader.GetInt16("magnitude"); 
       cartesianChart1.Series[1].Values.Add(valuez); 
      } 
     } 
     catch (Exception ex) 
     { 

      MessageBox.Show(ex.ToString()); 
     } 
     Data.con.Close(); 
     cartesianChart1.Series = new SeriesCollection 
     { 
      new ColumnSeries 
      { 

       Title = "Therapist", 
       Values = new ChartValues<int> { 10, 50, 39, 50 } 
      } 
     }; 
     cartesianChart1.AxisX.Add(new Axis 
     { 
      Title = "Therapists", 
      Labels = new[] { "Maria", "Susan", "Charles", "Frida" } 
     }); 
+0

[Dapper](https://github.com/StackExchange/Dapper)のような軽量のORMの使用をお勧めします。 –

+1

例外をスローする行は何ですか?どのような価値が「範囲外」ですか? –

+0

申し訳ありませんが、この "cartesianChart1.Series [1] .Values.Add(valuez);" – sakusa1

答えて

0

cartesianChart1.Seriesここで定義されている唯一の1つの項目が含まれています

cartesianChart1.Series = new SeriesCollection 
    { 
     new ColumnSeries 
     { 

      Title = "Therapist", 
      Values = new ChartValues<int> { 10, 50, 39, 50 } 
     } 
    }; 

あなたはこの部分で第2項目にアクセスしよう:

 while (myReader.Read()) 
     { 
      int valuez = myReader.GetInt16("magnitude"); 
      cartesianChart1.Series[1].Values.Add(valuez); 
     } 

最初の項目にアクセスしてください:

あなたはデータリーダー

からデータを読み込む前に、あなたはcertesianChart.Seriesを初期化する必要があります

 while (myReader.Read()) 
     { 
      int valuez = myReader.GetInt16("magnitude"); 
      cartesianChart1.Series[0].Values.Add(valuez); 
     } 
+0

動作しません。 :( – sakusa1

2

もゼロ

cartesianChart1.Series[0].Values.Add(valuez); 

への変更インデックスは、ここで作業コードです。

cartesianChart1.Series = new SeriesCollection 
{ 
    new ColumnSeries 
    { 
     Title = "Therapist", 
     Values = new ChartValues<int> { 10, 50, 39, 50 } 
    } 
}; 

try 
{ 
    con.Open(); 
    myReader = cmdDB.ExecuteReader(); 

    while (myReader.Read()) 
    { 
     int valuez = myReader.GetInt16("magnitude"); 
     cartesianChart1.Series[0].Values.Add(valuez); 
    } 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.ToString()); 
} 
+0

それは働いた!ありがとう! – sakusa1

関連する問題