Asp.Net Windows窗体运行时错误 - "过程或函数Insert指定的参数太多。"

时间:2015-03-24 04:34:30

标签: c# asp.net sql-server-2008

Windows窗体:Click Here

错误:Click here

我创建了一个Windows窗体应用程序,但每当我提交表单时,都会收到错误。

代码:

using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication12
{
   public partial class Add : Form
   {
        string connectionString = @"Data Source=AMAR;Initial Catalog=Test;Integrated Security=True";

        public Add()
        {
            InitializeComponent();
        }

        private void PhoneNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;

            if (!Char.IsDigit(ch) && ch != 8)
            {
                 e.Handled = true;
            }
        }

        private void Pin_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;

            if (!Char.IsDigit(ch) && ch != 8)
            {
                 e.Handled = true;
            }
        }

    private void cancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void Submit_Click(object sender, EventArgs e)
    {
        string Stat = Status.Text;
        string FirstName = FName.Text;
        string LastName = LName.Text;
        string Address = Addr.Text;
        string Cities = City.Text;
        string States = State.Text;
        string Countries = Country.Text;
        string PhoneNos = PhoneNo.Text;;
        string EmailId = Email.Text;
        string PinCode = Pin.Text;
        string CIn = CheckIn.Text;
        string COut = CheckOut.Text;
        string AdultNo = Adult.Text;
        string ChildNo = Child.Text;
        string InfantNo=Infant.Text;
        string RoomNos=RoomNo.Text;

        TestObject obj = new TestObject();

        obj.Stat=Status.Text;
        obj.FirstName = FName.Text;
        obj.LastName = LName.Text;
        obj.Address=Addr.Text;
        obj.Cities=City.Text;
        obj.States= State.Text;
        obj.Countries =Country.Text;
        obj.PhoneNos= PhoneNo.Text;;
        obj.EmailId= Email.Text;
        obj.PinCode=Pin.Text;
        obj.CIn=CheckIn.Text;
        obj.COut=CheckOut.Text;
        obj.AdultNo=Adult.Text;
        obj.ChildNo=Child.Text;
        obj.InfantNo=Infant.Text;
        obj.RoomNos=RoomNo.Text;

        string spName = "Insert";

        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand com = new SqlCommand(spName, conn);

        conn.Open();

        com.Parameters.AddWithValue("@Stat", obj.Stat);
        com.Parameters.AddWithValue("@FirstName", obj.FirstName);
        com.Parameters.AddWithValue("@LastName", obj.LastName);
        com.Parameters.AddWithValue("@Address", obj.Address);
        com.Parameters.AddWithValue("@Cities", obj.Cities);
        com.Parameters.AddWithValue("@States", obj.States);
        com.Parameters.AddWithValue("@Countries", obj.Countries);
        com.Parameters.AddWithValue("@PhoneNos", obj.PhoneNos);
        com.Parameters.AddWithValue("@EmailId", obj.EmailId);
        com.Parameters.AddWithValue("@PinCode", obj.PinCode);
        com.Parameters.AddWithValue("@CIn", obj.CIn);
        com.Parameters.AddWithValue("@COut", obj.COut);
        com.Parameters.AddWithValue("@AdultNo", obj.AdultNo);
        com.Parameters.AddWithValue("@ChildNo", obj.ChildNo);
        com.Parameters.AddWithValue("@InfantNo", obj.InfantNo);
        com.Parameters.AddWithValue("@RoomNos", obj.RoomNos);

        com.CommandType = CommandType.StoredProcedure;

        com.ExecuteNonQuery();

        conn.Close();
    }
}

public class TestObject
{
    public string Id;
    public string Status;
    public string FName;
    public string LName;
    public string Addr;
    public string City;
    public string State;
    public string  Country;
    public string  PhoneNo;
    public string  Email;
    public string Pin;
    public string CheckIn;
    public string  CheckOut;
    public string  AdultNo;
    public string  ChildNo;
    public string InfantNo;
    public string RoomNo;
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string States { get; set; }
    public string Countries { get; set; }
    public string Cities { get; set; }
    public string PhoneNos { get; set; }
    public string EmailId { get; set; }
    public string PinCode { get; set; }
    public string CIn { get; set; }
    public string COut { get; set; }
    public string RoomNos { get; set; }
    public string Stat { get; set; }}
}

SQL Server存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Insert] 
    @Stat nvarchar(100),
    @FirstName nvarchar(100),
    @LastName nvarchar(100),
    @Address nvarchar(100),
    @Cities nvarchar(100),
    @States nvarchar(100),
    @Countries nvarchar(100),
    @PhoneNos int,
    @EmailId nvarchar(100),
    @PinCode int,
    @CIn nvarchar(100),
    @COut nvarchar(100),
    @AdultNo int,
    @ChildNo int,
    @InfantNo int, 
    @RoomNos int
AS
BEGIN
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO [Hotel].[dbo].[Details](Status, FirstName, LastName, Address, City, State, Country, PhoneNo, CheckIn, CheckOut, EmailId, Pincode, AdultNo, ChildNo, InfantNo, RoomNo)
    VALUES(@Stat, @FirstName, @LastName, @Address, @Cities, @States, @Countries, @PhoneNos, @EmailId, @PinCode, @CIn, @COut, @AdultNo, @ChildNo, @InfantNo, @RoomNos)
END

请尽快帮助我......非常感谢

1 个答案:

答案 0 :(得分:0)

异常消息是不言自明的,您传递的参数数量与存储过程中定义的参数数量不匹配。但是,保留已发布的SP并且代码都具有相同数量的参数,但您的SP定义位于Hotel数据库上: -

USE [Hotel]

当您从表单中点击Test数据库时: -

string connectionString = @"Data Source=AMAR;Initial Catalog=Test;
                              Integrated Security=True";

因此,您需要指向部署正确SP的同一个DB: -

Initial Catalog=Hotel;

除此之外,请注意使用using语句自动处理昂贵的数据库资源并使用Add代替AddWithValue,请阅读此Article