2013-12-23 44 views
0

我試圖將結果返回給圖表並繪製圖表。我返回的數據庫數據是日期時間和浮點數,因爲它們都是對象,所以應該可以工作。我得到的錯誤是存儲過程中的points.addxy

不能從string轉換爲int

AddXY方法兩個參數。

string dateSelected = monthCalendarAdv1.Value.ToShortDateString(); 
dataConnection.Open(); 

SqlCommand dataCommand4 = new SqlCommand("SpTimeSeriesTotalParity", dataConnection); 
dataCommand4.Connection = dataConnection; 
dataCommand4.CommandType = CommandType.StoredProcedure; 
dataCommand4.Parameters.Add(new SqlParameter("@ValDate", dateSelected)); 
dataCommand4.Parameters.Add(new SqlParameter("@Acct", cmbSelectAccno.Text)); 

下面的代碼是相似的,併爲第一個參數工作沒有任何問題。

this.chart1.Series["$Parity"].Points.AddXY("Item1",1); 
this.chart1.Series["$Parity"].Points.AddXY("Item2", 2); 
this.chart1.Series["$Parity"].Points.AddXY("Item3", 3); 
this.chart1.Series["$Parity"].Points.AddXY("Item4", 4); 
this.chart1.Series["$Parity"].Points.AddXY("Item5", 5); 

try 
{ 
    dataConnection.Open(); 
    using (SqlDataReader myReader = dataCommand4.ExecuteReader()) 
        // myReader = dataConnection.ExecuteReader(); 
     while (myReader.Read()) 
     { 
      this.chart1.Series["$Parity"].Points.AddXY 
       (myReader.GetDateTime("ValuationDate"), myReader.GetDouble("SumParity")); 
     } 
} 
+1

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

0

這裏的問題不是你的addxy方法,而是Command讀取器的getter方法。

兩者都期望列的基於0的索引,而不是名稱。

更改您的代碼是這樣的:

this.chart1.Series ["$Parity"].Points.AddXY (myReader.GetDateTime(2), myReader.GetDouble(3)); 
+0

以下代碼類似,並且正在爲第一個參數工作,沒有任何問題。 this.chart1.Series [「$ Parity」] .Points.AddXY(「Item1」,1); this.chart1.Series [「$ Parity」] .Points.AddXY(「Item2」,2); this.chart1.Series [「$ Parity」] .Points.AddXY(「Item3」,3);這個圖表顯示了這個圖表。 this.chart1.Series [「$ Parity」] .Points.AddXY(「Item5」,5); – user2321650

+0

另外: 語法 C# C++ F# VB 公衆詮釋AddXY( \t對象x值, \t params對象[] y值 ) 參數 x值 類型:System.Object 的x值數據點。 yValue 類型:System.Object [] One o – user2321650