2017-10-09 160 views
0

我試圖從下拉列表中選擇並使用它的ItemName來獲取數據在sql列「價格」下,然後輸入一個數量(然後將其作爲一個sql插入在ItemName列中選中selecteditemfromdropdown +在Qty列上的數量+我試圖從另一個表中獲取的價格)。錯誤說輸入字符串的格式不正確

我使用了一個數據集表適配器「datPrice」並將其存儲在數據表「tblPrice」中。上線

int price = Convert.ToInt32(tblPrice.Rows[0]["Price"].ToString()); 

發生錯誤,原因是其說,

輸入字符串的不正確的格式

我怎樣才能解決這個錯誤嗎?

protected void btnSave_Click(object sender, EventArgs e) 
{ 
    DataSetTableAdapters.RawMaterialMFTableAdapter datItemName = new DataSetTableAdapters.RawMaterialMFTableAdapter(); 
    DataTable tblItemID = new DataTable(); 
    tblItemID = datItemName.GetDataByItemName(drpItemName.Text); 
    int itemID = Convert.ToInt32(tblItemID.Rows[0]["ItemID"].ToString()); 

    DataSetTableAdapters.RawMaterialMFTableAdapter datPrice = new DataSetTableAdapters.RawMaterialMFTableAdapter(); 
    DataTable tblPrice = new DataTable(); 
    tblPrice = datPrice.GetDataByPrice(drpItemName.Text); 

    int price = Convert.ToInt32(tblPrice.Rows[0]["Price"].ToString()); // Error on this line 
    int total = price * Convert.ToInt32(txtQuantity.Text.ToString()); 

    DataSetTableAdapters.QRawMaterialTableAdapter datData = new DataSetTableAdapters.QRawMaterialTableAdapter(); 
    datData.InsertQueryQRM(itemID, Convert.ToInt32(txtQuantity.Text.ToString()), price, total); 
} 
+0

您是否調試過代碼並檢查您在'tblPrice.Rows [0] [「Price」]中獲得的值?ToString()'? –

+1

美元/價格通常以小數存儲,您試過了嗎? 'var price = Convert.ToDecimal(tblPrice.Rows [1] [「Price」] .ToString()); **',同時檢查第一行包含Price的數值。 –

+0

你的意思是我得到的數據類型或我使用的參數?對不起,我不是很擅長這 –

回答

0

int price = int.Parse(tblPrice.Rows[0]["Price"].ToString());

嘗試。

+0

我做了,但是我做了什麼它似乎沒有從它的表中得到的價格。謝謝無論如何,我會嘗試做更多的試驗,然後 –

+0

你試圖刪除.ToString?順便說一句,你的tblPrice是十進制? –

+0

我DID和IT致力於感謝您的幫助和耐心等待我! –