繁体   English   中英

无法从字符串转换为int32

[英]Failed to convert from a string to a 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]=@Code", 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”

试试这个

您必须将字符串解析为Int32 DataType

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));

这应该工作

改变线

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

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

这条线

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

需要改为

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
  }

因为您正在尝试将字符串传递给类型为ineteger的参数。

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

以上陈述应该对你有所帮助......

问候

阿努拉格

根据您的代码,您没有真正提到错误引发的位置,这似乎是您的问题的根源。

            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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM