简体   繁体   English

System.Data.SqlClient.SqlException:“将 nvarchar 值转换为数据类型 int 时转换失败。”

[英]System.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value to data type int.'

I have creates a database and I am trying to INSERT INTO TABLE with the following code:我已经创建了一个数据库,我正在尝试使用以下代码INSERT INTO TABLE

string sqlquery = "INSERT INTO Runner VALUES (@firstName, @lastName, @amount, @category, @city, @charity, @route)";
cmd = new SqlCommand(sqlquery, conn);

conn.Open();

cmd.Parameters.AddWithValue("@firstName", txtFirstName.Text.ToString());
cmd.Parameters.AddWithValue("@lastName", txtLastName.Text.ToString());
cmd.Parameters.AddWithValue("@amount", Convert.ToInt32(txtMoney.Text));
cmd.Parameters.AddWithValue("@category", cbBoxCategory.ValueMember);
cmd.Parameters.AddWithValue("@city", cbBoxCity.ValueMember);
cmd.Parameters.AddWithValue("@charity", cbBoxCharity.ValueMember);
cmd.Parameters.AddWithValue("@route", cbBoxRoute.ValueMember);

cmd.ExecuteNonQuery();
conn.Close();

When I run it, I get this error:当我运行它时,我得到这个错误:

System.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value 'id' to data type int.' System.Data.SqlClient.SqlException:“将 nvarchar 值 'id' 转换为数据类型 int 时转换失败。”

All my cbBox (comboBox) need to leave a INT , I understand that is need to convert to NVARCHAR to INT , I try to use Convert.Into32(cbBox.Category.ValueMember) but without success.我所有的cbBox (comboBox) 都需要留下一个INT ,我知道需要将NVARCHAR转换为INT ,我尝试使用Convert.Into32(cbBox.Category.ValueMember)但没有成功。

I have tried everything with no success.我已经尝试了一切都没有成功。

After looking for other people answer I find this solution.在寻找其他人的答案后,我找到了这个解决方案。

string sqlquery = "INSERT INTO Runner (firstName, lastName, amount, categoryID, cityID, charityID, routeID) VALUES (@firstName, @lastName, @amount, @category, @city, @charity, @route)";
cmd = new SqlCommand(sqlquery, conn);
conn.Open();
cmd.Parameters.Add("@firstName", SqlDbType.VarChar, 50).Value =  txtFirstName.Text;
cmd.Parameters.Add("@lastName", SqlDbType.VarChar, 50).Value = txtLastName.Text;
cmd.Parameters.Add("@amount", SqlDbType.Int).Value = txtMoney.Text;
cmd.Parameters.Add("@category", SqlDbType.Int).Value = cbBoxCategory.SelectedValue;
cmd.Parameters.Add("@city", SqlDbType.Int).Value = cbBoxCity.SelectedValue;
cmd.Parameters.Add("@charity", SqlDbType.Int).Value = cbBoxCharity.SelectedValue;
cmd.Parameters.Add("@route", SqlDbType.Int).Value = cbBoxRoute.SelectedValue;
cmd.ExecuteNonQuery();
MessageBox.Show("New Runner has been registered!");
conn.Close();

By using Convert.ToInt32(cbBoxRoute.SelectedValue)通过使用Convert.ToInt32(cbBoxRoute.SelectedValue)

Here is the link where I find my solution:这是我找到解决方案的链接:

Insert ID into SQL from C# Combo box 将 ID 从 C# 组合框插入 SQL

暂无
暂无

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

相关问题 System.Data.SqlClient.SqlException:“将 nvarchar 值 'STORES' 转换为数据类型 int 时转换失败。” - System.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value 'STORES' to data type int.' System.Data.SqlClient.SqlException:'将 nvarchar 数据类型转换为 datetime 数据类型导致值超出范围。语句 - System.Data.SqlClient.SqlException: 'The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.The statement System.Data.SqlClient.SqlException:“从字符串转换日期和/或时间时转换失败。” - System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.' 错误:System.Data.SqlClient.SqlException(0x80131904):将数据类型nvarchar转换为数值时出错 - Error:System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to numeric 错误System.Data.SqlClient.SqlException:'将数据类型nvarchar转换为数字时出错。 在c#中 - Error System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to numeric.' in c# System.Data.SqlClient.SqlException nvarchar浮动 - System.Data.SqlClient.SqlException nvarchar to float System.Data.SqlClient.SqlException: '将数据类型 nvarchar 转换为日期时出错。' - System.Data.SqlClient.SqlException: 'Error converting data type nvarchar to date.' System.Data.SqlClient.SQLException:将 varchar 数据类型转换为 datatime - System.Data.SqlClient.SQLException: The conversion of a varchar data type to datatime 将nvarchar值“第1部分”转换为数据类型int时转换失败 - Conversion failed when converting the nvarchar value 'Part 1' to data type int 将nvarchar值'OrgID'转换为数据类型int时转换失败 - Conversion failed when converting the nvarchar value 'OrgID' to data type int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM