c# - 将 varchar 值“cat_id”转换为数据类型 int 时转换失败
问题描述
如何将cateogry_id从另一个表加载并保存到组合框中,但在组合框中显示Cateogry_Name c#?我的保存代码:
con.Open();
comboBox1.Text.ToString();
String Query = "INSERT INTO tbl_Item (Cat_id, Barcode, ItemName, Unit, Quantity, SalePrice, PPrice) VALUES ('"+ comboBox1.ValueMember + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "')";
SqlDataAdapter SDA = new SqlDataAdapter(Query, con);
SDA.SelectCommand.ExecuteNonQuery();
float mg = float.Parse(textBox6.Text) - float.Parse(textBox7.Text);
label10.Text = mg.ToString();
con.Close();
MessageBox.Show("Data Saved!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
con.Close();
也是一种方法
protected void FillCombo()
{
DataSet ds = new DataSet();
try
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from tbl_cateogry", con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
comboBox1.DisplayMember = "Cat_Name";
comboBox1.ValueMember ="Cat_id";
comboBox1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
con.Close();
}
当我运行它时,它告诉我“将 varchar 值“cat_id”转换为数据类型 int 时转换失败”请发布写入代码
解决方案
在我看来,您正在尝试将空字符串“”传递给它应该是 int 的地方。
您的 Cat_id 是否设置为自动增量?如果是,只需跳过 Cat_id 并仅传递其他值。
推荐阅读
- json - 有没有更好的方法来格式化 docker 输出?
- pandas - 当另一个数据框列中的列表项匹配时更新数据框中的 NaN 值
- java - ServiceLoader.load 如何与 JPMS 中的类加载器一起工作?
- python - 使用 shift() 比较 Pandas Dataframe 中的行
- sql-server - 如何在数据集中查找度量
- python - 无法使用 beautifulsoup 和 requests 进行网页抓取
- python - 无法使用 mysql.connector 从 SQL 获取结果到 Python
- mysql - 如何通过拆分table1中teacher_id的值来编写查询以获取table2中的teacher_name
- r - 创建多个变量的多图分布到一个
- shell - 使用 docker -c 选项在 shell 脚本中将 if 语句作为单行运行