C# 无法从字符串转换为int32
我遇到了一个问题,我只能在一个文本框中从数据库检索数据。我想要的是,我想从数据库中检索每个文本框的数据,但当我尝试时,得到错误: 代码如下:C# 无法从字符串转换为int32,c#,database,winforms,C#,Database,Winforms,我遇到了一个问题,我只能在一个文本框中从数据库检索数据。我想要的是,我想从数据库中检索每个文本框的数据,但当我尝试时,得到错误: 代码如下: 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;"); p
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();
}
及
但我得到了一个错误,上面写着:“无法将参数值从字符串转换为int32”试试这个
您必须将字符串解析为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
注意:这不适用于十进制值
或者,如果您的价格
值包含小数,则您可以执行以下操作:
cmd.Parameters["Code"].Value = Convert.ToInt32(Convert.ToDouble(this.textBoxCodeContainer[0][1].Text));
这应该有效更改行
cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text;
到
上述声明应该有助于你
问候
Anurag这一行
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类型的参数时。根据您的代码,您并没有真正提到错误产生的位置,这似乎是问题的根源
cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text;
您已将“code
”声明为整数
,但您正在分配一个文本
值。将其转换为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;
我已经试过了,但错误是:输入字符串的格式不正确。发生了什么事--“@Fuhans看看我的更新答案您的输入是否包含十进制值??如45.21或79.45??类似的内容??对于代码,数字不是十进制值,但是对于价格,它在数据库中包含十进制值,即“10.000,00”“我已经试过了,但错误是:输入字符串的格式不正确。发生了什么事--“我建议您回答。可能
textBoxCodeContainer
文本框中的所有文本都不会转换为int,在这种情况下,您的代码将被破坏。请改用int.TryParse
。您从何处得到错误?从失败的建议中,看起来您有数据问题。向我们展示示例数据?可能是代码或价格非整数。在从字符串转换为数字时始终使用cultreinfo我已尝试过,但错误为:无法隐式将类型int转换为字符串我已尝试过这种方法,但错误为:输入字符串的格式不正确。会发生什么情况?--”
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
}
cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["Code"].Value = this.textBoxCodeContainer[0][0].Text;
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;