2013-08-16 114 views
5

我遇到了一個問題,我只能在單個文本框中從數據庫檢索數據。我想是的,我想從數據庫中檢索每一個文本框的數據,但是當我試圖,得到錯誤:無法從字符串轉換爲int32

下面是代碼:

string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;"); 

private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>(); 
private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>(); 
private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>(); 
private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>(); 
private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>(); 

private void Form1_Load(object sender, EventArgs e) 
{ 
    UpdateTextPosition(); 

    OleDbDataReader dReader; 
    OleDbConnection conn = new OleDbConnection(connectionString); 
    conn.Open(); 
    OleDbCommand cmd = new OleDbCommand("SELECT [Code] FROM [Data]", conn); 

    dReader = cmd.ExecuteReader(); 

    AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection(); 

    while (dReader.Read()) 
    { 
     string numString = dReader[0].ToString().PadLeft(4, '0'); 
     codesCollection.Add(numString); 
    } 

    dReader.Close(); 
    conn.Close(); 
} 

private void UpdateDatas() 
{  
    OleDbDataReader dReader; 
    OleDbConnection conn = new OleDbConnection(connectionString); 
    conn.Open(); 
    OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT [Description], [Price] FROM [Data] WHERE [Code][email protected]", conn); 

    cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer); 
    cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text; 

    dReader = cmd.ExecuteReader(); 

    while (dReader.Read()) 
    { 
     this.textBoxDescContainer[0][0].Text = dReader["Description"].ToString(); 
     this.textBoxSubTotalContainer[0][0].Text = dReader["Price"].ToString(); 
    } 

    dReader.Close(); 
    conn.Close(); 
} 

我已經嘗試過加入:

cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer); 
cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][1].Text; 

while (dReader.Read()) 
{ 
    this.textBoxDescContainer[0][1].Text = dReader["Description"].ToString(); 
    this.textBoxSubTotalContainer[0][1].Text = dReader["Price"].ToString(); 
} 

,但我得到它說的錯誤:「無法參數值從〜應變轉換克至一個INT32"

+0

錯誤的代碼格式。 –

+0

你在哪裏得到錯誤? – Default

+0

根據失敗的建議,看起來您有數據問題。向我們展示樣本數據?它可以是代碼或價格包含非整數。 – cjb110

回答

2

試試這個

你必須解析的Int32數據類型

cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer); 
cmd.Parameters["Code"].Value =int.Parse(this.textBoxCodeContainer[0][1].Text); 

,如果用戶輸入一個有效的整數

對於例如這應該工作

int Val=int.Parse("45");//Works for me 

注:這並不適用於十進制值

工作,或者如果你的Price值包含小數點你可以做的是

cmd.Parameters["Code"].Value = Convert.ToInt32(Convert.ToDouble(this.textBoxCodeContainer[0][1].Text)); 

這應該工作

+0

我已經嘗試過了,但錯誤顯示:輸入字符串格式不正確。發生什麼事? - 「 – Reinhardt

+0

@Fuhans看看我的更新後的答案,你的輸入是否包含十進制值,如45.21或79.45 ??類似的?? – Rohit

+0

爲代碼,數字不是十進制值,但對於價格,它包含在數據庫中的十進制值,這是「10.000,00」 – Reinhardt

1

更改line

cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text; 

cmd.Parameters["Code"].Value = Convert.ToInt32(this.textBoxCodeContainer[0][0].Text); 
+0

我已經嘗試過,但錯誤說:輸入字符串不是以正確的格式。會發生什麼? - 「 – Reinhardt

+0

我會建議http://stackoverflow.com/a/18267369/503110答案。可能'textBoxCodeContainer'文本框中的所有文本都不會轉換爲int,在這種情況下,您的代碼將受到打擊。改用「int.TryParse」。 – PawanS

0
this.textBoxSubTotalContainer[0][0].Text=Convert.ToInt32(dReader["Price"].ToString()); 

上面的語句應該可以幫助您...

問候

阿努拉格

+0

我已經嘗試過了,但是錯誤提示:不能將int類型轉換爲字符串 – Reinhardt

+0

哪一行會得到錯誤? – Anurag

1

此行

cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][1].Text; 

需要爲你想傳遞一個字符串類型爲ineteger的參數改爲

int codeValue; 
if(int.TryParse(this.textBoxCodeContainer[0][1].Text,out codeValue)) 
    cmd.Parameters["Code"].Value = codeValue; 
else 
    { 
    //do some error handling of invalid input 
    } 

0

根據你的代碼,你沒有真正提到錯誤發生在哪裏,這似乎是你的問題的根源。

  cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer); 
      cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text; 

您已經聲明「Code」作爲Integer,但你分配一個Text值。將其轉換爲Int32

Int32 codeValue = 0; 
Int32.TryParse(this.textBoxCodeContainer[0][0].Text, out codeValue); 
cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer); 
cmd.Parameters["Code"].Value = codeValue; 
+0

我已經試過這種方式,但錯誤說:輸入字符串格式不正確。發生什麼事? - 「 – Reinhardt

+0

我已編輯代碼 – SollyM

+0

謝謝,現在可以使用 – Reinhardt