2016-01-21 70 views
0

我試圖將InnerHtml值轉換爲十進制值。用戶將輸入的XPath到TextBox2中將InnerHtml值轉換爲十進制

List<Decimal> productPriceList = new List<Decimal>(); 
var priceTags = document.DocumentNode.SelectNodes(textBox2.Text);  

然後我做一個foreach循環遍歷雖然發現價格,並將它們添加到一個名爲productPriceList

foreach (var prices in priceTags) 
{ 
    label9.Visible = true; 
    label9.Text += prices.InnerHtml + "\n"; 
    productPriceList.Add(prices.InnerHtml); 
    label2.Visible = false; 
    label3.Visible = false; 
} 

然後,添加列表值轉換成數據庫

using (var con = new SqlConnection(connectionString)) 
{ 
    con.Open(); 
    using (var cmd = new SqlCommand(@"INSERT INTO OnlineProductsTemp$(CompetitorID, ProductCode, ProductName, DateCaptured) 
     VALUES(@CompetitorID, @ProductCode, @ProductName, @DateCaptured)", con)) 
    { 
     cmd.Parameters.Add("@CompetitorID", SqlDbType.Int); 
     cmd.Parameters.Add("@ProductCode", SqlDbType.VarChar); 
     cmd.Parameters.Add("@ProductName", SqlDbType.VarChar); 
     cmd.Parameters.Add("@Price", SqlDbType.Decimal); 
     cmd.Parameters.Add("@DateCaptured", SqlDbType.DateTime); 
     for (int i = 0; i < competitorList.Count; i++) 
     { 
      cmd.Parameters["@CompetitorID"].Value = competitorList[i]; 
      cmd.Parameters["@ProductCode"].Value = productCodeList[i]; 
      cmd.Parameters["@ProductName"].Value = productNameList[i]; 
      cmd.Parameters["@Price"].Value = productPriceList[i]; 
      cmd.Parameters["@DateCaptured"].Value = dateCapturedList[i]; 
      int rowsAffected = cmd.ExecuteNonQuery(); 
     } 

一切工作正常,但我似乎無法找到一種方法正確地將InnerHtml值轉換爲十進制類型。有沒有辦法實現這一點?

+0

您是否試過[Decimal.Parse](https://msdn.microsoft.com/en-us/library/system.decimal.parse(v = vs.110).aspx)? –

+0

我收到錯誤**索引超出範圍。當我添加一個'productPriceList.Add(Decimal.Parse(prices.InnerHtml));'到foreach循環時,它必須是非負的並且小於集合**的大小。將數據添加到數據庫時發生此錯誤,並且列表中有0個元素 – TeaAnyOne

+0

然後很明顯,您應該檢查在列表中是否有某些內容,特別是當您遍歷其他數組/列表時。 –

回答

1

根據你的價值觀是來自內部HTML,你可以做這樣的事情

using (var con = new SqlConnection(connectionString)) 
{ 
    con.Open(); 
    using (var cmd = new SqlCommand(@"INSERT INTO  OnlineProductsTemp$(CompetitorID, ProductCode, ProductName, DateCaptured) 
    VALUES(@CompetitorID, @ProductCode, @ProductName, @DateCaptured)", con)) 
    { 
     cmd.Parameters.Add("@CompetitorID", SqlDbType.Int); 
     cmd.Parameters.Add("@ProductCode", SqlDbType.VarChar); 
     cmd.Parameters.Add("@ProductName", SqlDbType.VarChar); 
     cmd.Parameters.Add("@Price", SqlDbType.Decimal); 
     cmd.Parameters.Add("@DateCaptured", SqlDbType.DateTime); 
     for (int i = 0; i < competitorList.Count; i++) 
     { 
      cmd.Parameters["@CompetitorID"].Value = competitorList[i]; 
      cmd.Parameters["@ProductCode"].Value = productCodeList[i]; 
      cmd.Parameters["@ProductName"].Value = productNameList[i]; 
      cmd.Parameters["@Price"].Value = productPriceList[i]; 
      cmd.Parameters["@DateCaptured"].Value = dateCapturedList[i]; 
      int rowsAffected = cmd.ExecuteNonQuery(); // also the variable rowsAffected is not visible outside the scope of the for loop 
     } 
    } 
} 

更新: 你應該,那麼,轉換值,將其添加在列表中,像這樣之前:

foreach (var prices in priceTags) 
{ 
    label9.Visible = true; 
    label9.Text += prices.InnerHtml + "\n"; 
    productPriceList.Add(Decimal.Parse(prices.InnerHtml, <cultureInfoHere>)); 
    label2.Visible = false; 
    label3.Visible = false; 
} 

通過這樣做,您不必更改列表類型。