2013-08-16 200 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