C# Visual Studio windows窗体应用程序中发生未处理的异常。可能是什么?
我是一名学习计算机的大学生。对于我的编程任务,我需要在Visual Studio中制作一个C#Windows窗体应用程序,以允许用户在车库数据库中输入和存储信息 我已经为客户详细信息创建了一个表和存储过程,但是当我测试和运行系统时,在代码的执行非查询部分会收到一条未处理的异常错误消息。当我输入所有数据后按表单上的submit按钮时,就会发生这种情况 有人知道为什么会这样吗?有人能把我推向正确的方向吗 谢谢 (客户详细信息的存储过程) 客户详细信息表C# Visual Studio windows窗体应用程序中发生未处理的异常。可能是什么?,c#,sql,C#,Sql,我是一名学习计算机的大学生。对于我的编程任务,我需要在Visual Studio中制作一个C#Windows窗体应用程序,以允许用户在车库数据库中输入和存储信息 我已经为客户详细信息创建了一个表和存储过程,但是当我测试和运行系统时,在代码的执行非查询部分会收到一条未处理的异常错误消息。当我输入所有数据后按表单上的submit按钮时,就会发生这种情况 有人知道为什么会这样吗?有人能把我推向正确的方向吗 谢谢 (客户详细信息的存储过程) 客户详细信息表 Create Procedure [dbo].
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
嗨,谢谢你的回复。我是新手,所以我不太清楚“传递AddDetails需要的参数”是什么意思,我该怎么做?由于我已经在AddDetails过程中声明了这些参数,所以我不能确定您是否真的声明了这些参数,除此之外,您还传递了8个未在
AddDetail
中定义的参数,因此您需要删除这8行,因此:删除或注释掉从sqlcmd.parameters.AddWithValue(@CarMake)开始的所有行,cboCustomerType.Text.Trim())代码>并以sqlcmd.Parameters.AddWithValue(“@DateofService”,txtDateofService.Text.Trim())结尾代码>这些行尝试将数据推入存储过程AddDetail
,该存储过程不需要该数据。它只需要上述5个参数。或者,扩展存储过程AddDetail
,以便它接受更多的数据,但出现以下错误:“过程或函数'AddDetails'需要参数'@CustomerType',但未提供参数”。“是的,您删除了太多的一行,需要保留该行。(是我的错,编辑了原始评论)异常中的错误消息是什么?