2015-11-02 38 views
-3

我是一名使用c#的初學者。我試圖在c#中使用if else語句進行編碼。當我運行該程序時,出現此錯誤消息。它沿着線double x = Convert.ToDouble("XValue");出現。我試圖將字符串X值轉換爲double來匹配我的變量x。將字符串轉換爲雙精度時,輸入字符串的格式不正確

static void Main(String[] args) 
    { 
     connectDB(); 
     OpenConnection(); 


     MySqlCommand command = c.CreateCommand(); 
     command.CommandText = "SELECT * FROM gazecoords INNER JOIN gazeperiod ON gazecoords.gazeID = gazeperiod.gazeID INNER JOIN trialImage on gazeperiod.imageID = trialImage.imageID INNER JOIN areacoords on trialImage.imageID = areacoords.trialImageID;"; 
     try 
     { 
      c.Open(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     MySqlDataReader reader = command.ExecuteReader(); 
     while (reader.Read()) 
     { 
      Console.WriteLine(reader["imageID"].ToString()); 
      Console.WriteLine(reader["topLeftX"].ToString()); 
      Console.WriteLine(reader["topRightX"].ToString()); 
      Console.WriteLine(reader["topLeftY"].ToString()); 
      Console.WriteLine(reader["bottomLeftY"].ToString()); 
      Console.WriteLine(reader["XValue"].ToString()); 
      Console.WriteLine(reader["YValue"].ToString()); 
      Console.WriteLine(reader["in"].ToString()); 

      while (reader.Read()) 
      { 

       double x = Convert.ToDouble("XValue"); 
       double y = Convert.ToDouble("YValue"); 
       List <int> image = new List<int>(Convert.ToInt32("imageID")); 
       // int coordsID = Convert.ToInt32("coordsID"); 
       double topLeftX = Convert.ToDouble("topLeftX"); 
       double topRightX = Convert.ToDouble("topRightX"); 
       double topLeftY = Convert.ToDouble("topLeftY"); 
       double bottomLeftY = Convert.ToDouble("bottomLeftY"); 
       int inside = Convert.ToInt32("in"); 
       inside = 0; 

       foreach (int coordsID in image) 
       { 
        if (x > topLeftX && x < topRightX && y > topLeftY && y < bottomLeftY) 
        { 
         Console.WriteLine("The value inside are the area is {0}", inside + 1); 
        } 
        else 
         Console.WriteLine("0"); 
        Console.ReadLine(); 
       } 
      } 
     } 
    } 

​​

+0

唉呦。你沒有意識到你試圖解析'「XValue」'字符串,而不是'reader [「XValue」]'值? –

+1

那麼'String''「XValue」'不代表任何'Double',對吧?然而,'reader [「XValue」]'可能包含一個'Double'值 –

+0

XValue是如何存儲的? MySQL不具有「真正的」double類型,所以它必須存儲爲'string'?如果它必須是一個「字符串」,它將存儲什麼數字格式,以及哪種數字格式是您的默認值(CurrentCulture/CurrentUICulture)? – Corak

回答

0

應該

double x = Convert.ToDouble(reader["XValue"]); 

有人試圖轉換字符串「x值」爲Double這是不可能的,但現在它會轉換的reader["XValue"]值。

0

您還沒有傳遞到reader["XValue"]double x = Convert.ToDouble("XValue");
XValuestring這是造成錯誤,而轉換爲double

將其更改爲

double x = Convert.ToDouble(reader["XValue"]); 
0

也許,x值

double x = Convert.ToDouble(reader["XValue"] ?? 0); 
+0

XValue不爲null。我是使用C#的初學者,所以我很迷茫。我的Xvalue類似於363.0809等。 – SPP

+0

@SPP如果您正在使用visual studio,請選擇'reader [「XValue」]'並單擊鼠標右鍵 - >單擊快速監視並在調試模式下查看此處的值。 –

+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 –