0

我正在使用我的gridview上的asp.net开发一个Web应用程序,我使用一个命令字段编辑按钮调用一个存储过程来更新数据但同时更新

我收到以下错误:

过程或函数 updateprofile 指定的参数过多。

说明:
执行当前 Web 请求期间发生未处理的异常。
请查看堆栈跟踪以获取有关错误及其
源自代码的位置的更多信息。

异常详细信息:
System.Data.SqlClient.SqlException:过程或函数 updateprofile 指定了太多
参数。

这是我的存储过程:

ALTER procedure [dbo].[updateprofile] 
   @pid int, 
   @pfirstname varchar(50), 
   @plastname varchar(50), 
   @padress varchar(50),
   @pemail varchar(50),
   @ptelephone varchar(50), 
   @pbirthday date 
as begin 
   update profile 
   set firstname = @pfirstname, @plastname = lastname,
       @padress = adress, @pemail = email, @ptelephone = telephone,
       birthday = @pbirthday 
   where id = @pid end

这是我的asp代码:

SelectCommand="selectAllProfile" SelectCommandType="StoredProcedure"     
UpdateCommand="updateprofile" UpdateCommandType="StoredProcedure"
DeleteCommand="deleteprofile" DeleteCommandType="StoredProcedure"

<UpdateParameters>
  <asp:Parameter Name="pid" Type="Int32" />
  <asp:Parameter Name="pfirstname" Type="String" /> 
  <asp:Parameter Name="plastname" Type="String" />
  <asp:Parameter Name="padress" Type="String" /> 
  <asp:Parameter Name="pemail" Type="String" /> 
  <asp:Parameter Name="ptelephone" Type="String" /> 
  <asp:Parameter DbType="Date" Name="pbirthday" /> 
</UpdateParameters>

顺便说一句,我面临删除命令的相同错误

这是我的VB代码:

Imports System.Data.SqlClient
Imports System.Data
Imports Class1e

Public Class Profile_test
Inherits System.Web.UI.Page
Dim rc As Integer
Dim inc As Integer
Dim dt As New DataTable
Dim x As New Class1e
Protected Sub Btn_save_Click(sender As Object, e As System.EventArgs) Handles  
Btn_save.Click

    If id_txt.Text.Length = 0 Then
        x.insertProfile(fname_txt.Text, lname_txt.Text, adresss_txt.Text,  
email_txt.Text, birthday_txt.Text, phone_txt.Text)
        id_txt.Text = "0"
    Else
        x.updateProfile(id_txt.Text, fname_txt.Text, lname_txt.Text,  
  adresss_txt.Text,  
 email_txt.Text, birthday_txt.Text, phone_txt.Text)
    End If
    GridView1.DataBind()

End Sub

Protected Sub GridView1_Load(sender As Object, e As System.EventArgs) Handles  
GridView1.Load
    GridView1.Columns(1).Visible = False

End Sub

Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs)  
Handles GridView1.SelectedIndexChanged
    Dim row As GridViewRow = GridView1.SelectedRow
    id_txt.Text = row.Cells(1).Text
    fname_txt.Text = row.Cells(2).Text
    lname_txt.Text = row.Cells(3).Text
    adresss_txt.Text = row.Cells(4).Text
    email_txt.Text = row.Cells(5).Text
    phone_txt.Text = row.Cells(6).Text
    birthday_txt.Text = row.Cells(7).Text
End Sub

Protected Sub btn_new_Click(sender As Object, e As System.EventArgs) Handles  
btn_new.Click
    fname_txt.Text = ""
    lname_txt.Text = ""
    adresss_txt.Text = ""
    email_txt.Text = ""
    phone_txt.Text = ""
    birthday_txt.Text = ""
    id_txt.Text = ""
End Sub

这是我的VB类代码:

Public Class Class1e
Dim x As String
Public Sub New()

End Sub

Public connectionString As String = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString
Public cn As SqlConnection = New SqlConnection(connectionString)

Public Sub updateProfile(ByVal id As Integer, ByVal fname As String, ByVal lname As String, ByVal adress As String, ByVal email As String, ByVal birthday As String, ByVal phone As String)

    Try
        Dim command As SqlCommand = New SqlCommand("updateprofile", cn)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.AddWithValue("@pid", id)
        If fname.Length = 0 Then
            MsgBox("First name is a required file")
        Else
            command.Parameters.AddWithValue("@pfirstname", fname)

        End If

        If lname.Length = 0 Then
            MsgBox("Last Name is a required file")
        Else
            command.Parameters.AddWithValue("@plastname", lname)
        End If

        command.Parameters.AddWithValue("@padress", adress)

        If email.IndexOf(".") = -1 Or email.IndexOf("@") = -1 Then
            MsgBox("The email format should be name@domain.com")
        Else
            command.Parameters.AddWithValue("@pemail", email)
        End If

        If phone.Length = 0 Then
            MsgBox("Tel is a required file")
        Else
            If phone.Length <> 8 Then
                MsgBox("The tel should be 8 digits")
            Else
                command.Parameters.AddWithValue("@ptelephone", phone)
            End If
        End If

        command.Parameters.AddWithValue("@pbirthday", birthday)

        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If

        command.ExecuteNonQuery()
        MsgBox("1 Record updated ")
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        If cn.State = ConnectionState.Open Then
            cn.Close()
        End If
    End Try
End Sub

Public Sub deleteProfile(ByVal id As Integer)

    Try
        Dim command As SqlCommand = New SqlCommand("deleteprofile", cn)
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.AddWithValue("@pid", id)

        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If
        command.ExecuteNonQuery()
        MsgBox("1 Record deleted ")
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        If cn.State = ConnectionState.Open Then
            cn.Close()
        End If
    End Try
End Sub

Public Sub insertProfile(ByVal fname As String, ByVal lname As String, ByVal adress As String, ByVal email As String, ByVal birthday As String, ByVal phone As String)


    Try
        Dim command As SqlCommand = New SqlCommand("insertprofile", cn)
        command.CommandType = CommandType.StoredProcedure
        If fname.Length = 0 Then
            MsgBox("First name is a required file")
        Else
            command.Parameters.AddWithValue("@pfirstname", fname)

        End If

        If lname.Length = 0 Then
            MsgBox("Last Name is a required file")
        Else
            command.Parameters.AddWithValue("@plastname", lname)
        End If

        command.Parameters.AddWithValue("@padress", adress)

        If email.IndexOf(".") = -1 Or email.IndexOf("@") = -1 Then
            MsgBox("The email format should be name@domain.com")
        Else
            command.Parameters.AddWithValue("@pemail", email)
        End If

        If phone.Length = 0 Then
            MsgBox("Tel is a required file")
        Else
            If phone.Length <> 8 Then
                MsgBox("The tel should be 8 digits")
            Else
                command.Parameters.AddWithValue("@ptelephone", phone)
            End If
        End If

        command.Parameters.AddWithValue("@pbirthday", birthday)
        command.Parameters.AddWithValue("@pprocessed", 0)
        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If

        command.ExecuteNonQuery()
        MsgBox("1 Record inserted ")
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        If cn.State = ConnectionState.Open Then
            cn.Close()
        End If
    End Try
  End Sub
End Class

等待您的回复

非常感谢您的帮助 :)

4

1 回答 1

0

而不是给予

set firstname = @pfirstname, lastname=@plastname,adress = @padress, 
                email = @pemail,telephone = @ptelephone,birthday = @pbirthday 
                where id = @pid

你给了

set firstname = @pfirstname, @plastname = lastname,@padress = adress, 
                @pemail = email, @ptelephone = telephone,birthday = @pbirthday 
                where id = @pid
于 2013-08-19T08:53:38.350 回答