c# - Visual Studio Windows 窗体应用程序中出现未处理的异常。会是什么呢?
问题描述
我是一名学习计算机的大学生。对于我的编程任务,我需要在 Visual Studio 中制作一个 C# Windows 窗体应用程序,以允许用户在 Car Garage 的数据库中输入和存储信息。
我已经为客户详细信息创建了一个表和存储过程,但是当我测试和运行系统时,我在代码的执行非查询部分收到一条 Exception Unhandled 错误消息。输入所有数据后,当我按下表单上的提交按钮时,就会发生这种情况。
有谁知道为什么会这样,有人能把我推向正确的方向吗?
谢谢
(客户详细信息的存储过程)
Create Procedure [dbo].[AddDetails]
@CustomerName Varchar(50),
@CustomerAddress Varchar(50),
@CustomerTelephone Varchar(50),
@CustomerEmail Varchar(50),
@CustomerType Varchar(15)
AS
Begin
Insert into [Customer]
([CustomerName]
,[CustomerAddress]
,[CustomerTelephone]
,[CustomerEmail]
,[CustomerType])
Values
(@CustomerName
,@CustomerAddress
,@CustomerTelephone
,@CustomerEmail
,@CustomerType)
End
客户详细信息表
CREATE TABLE [dbo].[Customer] (
[CustomerId] INT IDENTITY (1, 1) NOT NULL,
[CustomerName] VARCHAR (50) NOT NULL,
[CustomerAddress] VARCHAR (50) NOT NULL,
[CustomerTelephone] VARCHAR (50) NOT NULL,
[CustomerEmail] VARCHAR (50) NOT NULL,
[CustomerType] VARCHAR (15) NOT NULL,
CONSTRAINT [Pk_Name] PRIMARY KEY CLUSTERED ([CustomerId] ASC)
);
~ ~ ~
Code for the Add form
~ ~ ~
private void btnSave_Click(object sender, EventArgs e)
{
if(txtName.Text =="" || txtAddress.Text =="" || txtPhoneNo.Text =="" || txtEmail.Text =="" || cboCustomerType.Text ==""
|| txtMake.Text == "" || txtModel.Text =="" || txtRegistration.Text =="" || txtMaintenanceNo.Text =="" || cboService.Text ==""
|| txtDateofService.Text =="")
{
MessageBox.Show("Please enter all details");
}
else
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlCommand sqlcmd = new SqlCommand("AddDetails", sqlCon);
sqlcmd.CommandType = CommandType.StoredProcedure;
//add the data from the textbox into the @Name and store it in the database
sqlcmd.Parameters.AddWithValue("@CustomerName", txtName.Text.Trim());
sqlcmd.Parameters.AddWithValue("@CustomerAddress", txtAddress.Text.Trim());
sqlcmd.Parameters.AddWithValue("@CustomerTelephone", txtPhoneNo.Text.Trim());
sqlcmd.Parameters.AddWithValue("@CustomerEmail", txtEmail.Text.Trim());
sqlcmd.Parameters.AddWithValue("@CustomerType", cboCustomerType.Text.Trim());
sqlcmd.Parameters.AddWithValue("@CarMake", txtMake.Text.Trim());
sqlcmd.Parameters.AddWithValue("@CarModel", txtModel.Text.Trim());
sqlcmd.Parameters.AddWithValue("@CarReg", txtRegistration.Text.Trim());
sqlcmd.Parameters.AddWithValue("@MaintenanceNo", txtMaintenanceNo.Text.Trim());
sqlcmd.Parameters.AddWithValue("@Service", cboService.Text.Trim());
sqlcmd.Parameters.AddWithValue("@Problems", txtProblems.Text.Trim());
sqlcmd.Parameters.AddWithValue("@ServiceHistory", txtServiceHistory.Text.Trim());
sqlcmd.Parameters.AddWithValue("@DateofService", txtDateofService.Text.Trim());
//execute query
sqlcmd.ExecuteNonQuery();
//message box to show details have been entered
MessageBox.Show("Details Saved");
解决方案
您正在尝试将 13 个参数添加到存储过程调用。存储过程只定义了 5 个参数。
这就是为什么你会得到一个异常,告诉你你添加了太多参数。
您应该只传递AddDetail
需要的参数,它们是:
@CustomerName
@CustomerAdress
@CustomerTelephone
@CustomerEmail
@CustomerType
推荐阅读
- networking - SIP 标头理解
- katalon-studio - 如何将步骤过滤器[用于调试]添加到katalon studio
- java - Spring WebFlux 无法在结果中将阻塞操作切换到其他调度程序
- javascript - 从值 [- 1, 1] 规范化到 [0, 1]
- regex - 当正则表达式中包含感叹号时,Sed 命令行为异常
- c - 读取带有空行的文本文件
- android - 布局表达式 - 可绘制值的三元运算
- jquery - 在每个父 div 中,获取位于子 div 中的元素并将其附加到不同的子 div
- java - 使用TestNG从硒测试用例中的页面对象调用方法时如何修复NullPointerException错误?
- javascript - Angular 7中静态内容的通配符路由