You need to enable JavaScript to run this app. C# WinForm现金凭证项目点击提交报“无效对象voucher_table”错误解决方法
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForm现金凭证项目点击提交报“无效对象voucher_table”错误解决方法

现金凭证项目提交时报「无效对象 voucher_table」问题排查

问题现象

  • 开发现金凭证(cash voucher)项目过程中,每次点击提交按钮,系统都会弹出提示框,显示「Invalid Object voucher_table(无效对象 voucher_table)」,凭证数据无法正常写入数据库。
  • 提交按钮对应的点击事件实现代码如下:
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);
        }

参考截图

  • 系统报错提示截图:报错提示
  • 数据库连接配置截图:连接配置

排查结果与解决方案

  • 排查发现:app.config配置文件的数据库连接字符串未指定Initial Catalog项,程序连接SQL Server实例后,未定位到存放voucher_table表的目标业务数据库,无法识别该表对象,因此抛出「无效对象」报错。
  • 解决操作:在app.config的数据库连接字符串中补充配置项 Initial Catalog=My Cash Voucher;,明确指定程序连接后默认操作的数据库,重新运行项目后,提交功能恢复正常,报错消除。

内容的提问来源于stack exchange,提问作者AaryanKh2007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.28 05:06:06