2014-12-05 31 views
0

我在這裏有一些錯誤,我沒有問題如何解決它。 它無法調用表eRROR With:輸入字符串格式不正確

我嘗試調用調試的名單,當它達到低於

List<Int32> ListintNo = DT_ExcludeNo.AsEnumerable().Select(x 
    =>Convert.ToInt32(x[0].ToString())).ToList(); 

聲明敬請協助。

DataTable DT_ExcludeNo = new DataTable(); 
     dataAdapter.Fill(DT_ExcludeNo); 

     InsertLogFile("AMB SP15: Validate Insert Data"); 

     //InsertLogFile("AMB SP15: Execute Query: " + sSqlCommandRetrieveExcludeNO); 

     List<Int32> ListintNo = DT_ExcludeNo.AsEnumerable().Select(x => Convert.ToInt32(x[0].ToString())).ToList(); 
     for (Int32 i = 1; i <= TotalCount; i++) 
     { 
      foreach (Int32 intNo in ListintNo) 
      { 
       if (i == intNo) 
       { 
       string sSqlCommandRetrieveData = "SELECT Name, [I.C], [D.O.B], NO, [EFF DATE], Sum_Insured from AMB_Temp WHERE NO = " + i; 

       SqlCommand obj_SQLCommand = new SqlCommand(sSqlCommandRetrieveData, myconn); 
       dataAdapter = new SqlDataAdapter(obj_SQLCommand); 

       DataSet DS_RetrieveData = new DataSet(); 
       dataAdapter.Fill(DS_RetrieveData); 

       //InsertLogFile("AMB SP15: Execute Query: " + sSqlCommandRetrieveData); 

       DataValidation(DS_RetrieveData); 
       } 
+1

向我們顯示發生異常的堆棧跟蹤或行以及參數的值(如果有)。你也不會向我們展示x [0]的實際代碼片段。什麼是「x」?它有什麼價值?它是什麼類型的對象?那個指數的數據是什麼? – 2014-12-05 02:49:30

+0

例外的您好THA線發生在: 列表ListintNo = DT_ExcludeNo.AsEnumerable()選擇(X => Convert.ToInt32(X [0]的ToString()))ToList(); – user1210826 2014-12-05 03:17:55

+0

DataTable中沒有的值數/ nvarchar的 – user1210826 2014-12-05 03:18:41

回答

0

下面是更新後的代碼:

//start new code 
string fieldString = x[0].ToString(); 
if (string.IsNullOrEmpty(fieldString)) 
{ 
    fieldString = "0"; 
} 
//InsertLogFile("AMB SP15: Execute Query: " + sSqlCommandRetrieveExcludeNO); 

List<Int32> ListintNo = DT_ExcludeNo.AsEnumerable().Select(x => Convert.ToInt32(fieldString)).ToList(); 
//end new code 
    DataTable DT_ExcludeNo = new DataTable(); 
    dataAdapter.Fill(DT_ExcludeNo); 

    InsertLogFile("AMB SP15: Validate Insert Data"); 

    //InsertLogFile("AMB SP15: Execute Query: " + sSqlCommandRetrieveExcludeNO); 
     string fieldString = x[0].ToString(); 
     if (string.IsNullOrEmpty(fieldString)) 
     { fieldString = "0"; } 

    List<Int32> ListintNo = DT_ExcludeNo.AsEnumerable().Select(x => Convert.ToInt32(fieldString)).ToList(); 



     for (Int32 i = 1; i <= TotalCount; i++) 
    { 
     foreach (Int32 intNo in ListintNo) 
     { 
      if (i == intNo) 
      { 
      string sSqlCommandRetrieveData = "SELECT Name, [I.C], [D.O.B], NO, [EFF DATE], Sum_Insured from AMB_Temp WHERE NO = " + i; 

      SqlCommand obj_SQLCommand = new SqlCommand(sSqlCommandRetrieveData, myconn); 
      dataAdapter = new SqlDataAdapter(obj_SQLCommand); 

      DataSet DS_RetrieveData = new DataSet(); 
      dataAdapter.Fill(DS_RetrieveData); 

      //InsertLogFile("AMB SP15: Execute Query: " + sSqlCommandRetrieveData); 

      DataValidation(DS_RetrieveData); 
      } 
+0

都能跟得上轉換。你不需要那樣做。它仍然會編譯好。原始錯誤也是一個RUNTIME錯誤。 – 2014-12-05 02:48:55

+0

我認爲它發生在使用動態sql調用dataAdapter.Fill方法時。 sqlcommand接受一個字符串作爲參數,但在運行時它被分配一個字符串加一個int。 – 2014-12-05 02:53:42

+0

錯誤發生在線: List ListintNo = DT_ExcludeNo.AsEnumerable()。Select(x => Convert.ToInt32(x [0] .ToString()))。ToList(); – user1210826 2014-12-05 03:19:44

0

請檢查您的NO數據庫列字段的數據類型,如果它是VarChar or Char的,你必須改變你的where語句。

string sSqlCommandRetrieveData = "SELECT Name, [I.C], [D.O.B], NO, [EFF DATE], Sum_Insured from AMB_Temp WHERE NO = " + Convert. ToString(i); 
+0

嗨,是NVARCHAR是可以把這個聲明? – user1210826 2014-12-05 03:12:21

+0

根據我對你的問題的理解,'NvarChar'在該陳述中是可能的。 – 2014-12-05 03:22:07

+0

是的,你是對的 – user1210826 2014-12-05 03:23:25