c# - 这个错误每次都会发生。我该怎么办?
问题描述
我已经开始从事现金券项目。当我单击提交按钮时,会出现一个弹出框并显示“无效对象凭证表”。我该怎么办??
private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
string con = ConfigurationManager.ConnectionStrings["mydb"].ConnectionString;
SqlConnection sqlcn = new SqlConnection(con);
sqlcn.Open();
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO voucher_table(customerID, planName, days, planAmount, validFrom, validTo, amountInWords, date, rupees) values(@customerID, @planName, @days, @planAmount, @validFrom, @validTo, @amountInWords, @date, @rupees)", sqlcn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@customerID", customerID.Text);
cmd.Parameters.AddWithValue("@planName", planName.Text);
cmd.Parameters.AddWithValue("@days", days.Text);
cmd.Parameters.AddWithValue("@planAmount", planAmount.Text);
cmd.Parameters.AddWithValue("@validFrom", validFrom.Text);
cmd.Parameters.AddWithValue("@validTo", validTo.Text);
cmd.Parameters.AddWithValue("@amountInWords", amountInWordsTextBox1.Text + amountInWordsTextBox2.Text);
cmd.Parameters.AddWithValue("@date", date.Text);
cmd.Parameters.AddWithValue("@rupees", rupees.Text);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Voucher Created Successfully");
SqlCommand cmd1 = new SqlCommand("select max(primaryNo) from voucher_table", sqlcn);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
MessageBox.Show("Your Voucher No is '" + dr1.GetInt32(0) + "'Your Voucher is Created Successfully!");
Voucher_Successful success = new Voucher_Successful();
success.ShowDialog();
}
this.Close();
}
else
{
MessageBox.Show("An Error Occured... Voucher Not Created");
}
sqlcn.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
编辑:图片 01
编辑:找到的答案:我添加了初始目录:我的现金券;在 app.config 中,问题已解决。
解决方案
我认为您不在正确的数据库中(即 SQL 命令找不到凭证表)。检查您连接的数据库中是否确实存在凭证表。
我发现了一个与你类似的问题,你也可以在那里找到帮助:SQL Server: invalid object name in query execution
推荐阅读
- xml - 在 Nest.js 中获取 Post 请求正文中的 XML
- javascript - jQuery 表单提交按钮根据选中的复选框转到 URL
- javascript - react-spring:使用 useChain 进行缩放和翻译
- vue.js - Vue2 - 路由级别的延迟加载不起作用?
- r - 如何仅针对每个文件中的某些值组合多个 .RDS 文件?
- c++ - 在 C++ 中运行项目时出现未定义的引用错误
- visual-studio - 您好,我有一个使用 specflow 用 c# 编写的自动化框架
- python - 如何并行计算天体运动?
- c++ - 如何转发声明在单独的头文件中相互引用的结构?
- spring - 如何使用 Scala 在可执行 Jar 中自动装配 Spring 配置?