2013-04-29 74 views
1

所以问题是:过程或函数期望未提供的参数。的ExecuteNonQuery

,当我点击我的注册网页表单提交按钮有就行了“oCom.ExecuteNonQuery();”错误(在文本框中添加相应的值之后)。它说

“过程或函数'ADDUSR'期望参数'@useremail',其中 未提供。”

而且我一直在试图找出为什么它说,因为我仔细检查了与oUsr.Email = "[email protected]",该信息来自调试器,这是有道理的,因为我只是增加它的文本框点击提交按钮之前。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data; 

namespace Carnisoftix 
{ 
    public partial class Register : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
     } 

     protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      carniuser oUsr = new carniuser(); 

      oUsr.Email = txtEmail.Text; 
      oUsr.Name = txtName.Text; 
      oUsr.Pass = txtPassword.Text; 
      oUsr.Phone = txtPhone.Text; 
      oUsr.Usrname = txtUsername.Text; 
      oUsr.Address = txtAddress.Text; 
      oUsr.SpecialK = Convert.ToInt16(txtSpecial.Text); 
      oUsr.Auth = 1; 
      regUsr(oUsr); 

     } 
     public static void regUsr(carniuser oUsr) 
     { 
      //int oNum; 
      SqlConnection oConnection = new SqlConnection("Server=.\\SQLExpress;AttachDbFilename=L:\\Apps\\VS Projects\\Carnisoftix\\CarniDb.mdf;Database=CarniDb;Trusted_Connection=Yes;"); 
      string oSql = "ADDUSR"; 
      SqlCommand oCom = new SqlCommand(oSql, oConnection); 
      oCom.CommandType = CommandType.StoredProcedure; 
      oCom.Parameters.AddWithValue("@useremail ,", oUsr.Email); 
      oCom.Parameters.AddWithValue("@userpass, ", oUsr.Pass); 
      oCom.Parameters.AddWithValue("@name, ", oUsr.Name); 
      oCom.Parameters.AddWithValue("@phone, ", oUsr.Phone); 
      oCom.Parameters.AddWithValue("@address, ", oUsr.Address); 
      oCom.Parameters.AddWithValue("@username, ", oUsr.Usrname); 
      oCom.Parameters.AddWithValue("@authority, ", oUsr.Auth); 
      oCom.Parameters.AddWithValue("@special, ", oUsr.SpecialK); 
      //SqlParameter oReturn = new SqlParameter("@out", SqlDbType.Int); 
      //oReturn.Direction = ParameterDirection.ReturnValue; 
      //oCom.Parameters.Add(oReturn); 
      oConnection.Open(); 
      oCom.ExecuteNonQuery(); 
      //oNum = (int)oCom.Parameters["@out"].Value; 
      oConnection.Close(); 
      //return oNum; 
     } 
    } 
    /* my database's stored procedure requires this parameters: 
    @useremail, 
    @username, 
    @userpass, 
    @name, 
    @phone, 
    @address, 
    @authority, 
    @special*/ 
    public class carniuser 
    { 
     private string email, usrname, pass, name, phone, address; 
     private int authority, specialK; 
     public carniuser() 
     { 
      email = ""; 
      usrname = ""; 
      pass = ""; 
      name = ""; 
      phone = ""; 
      address = ""; 
      authority = 1; 
      specialK = 1; 
     } 
     public string Email 
     { 
      get { return email; } 
      set { email = value; } 
     } 
     public string Pass 
     { 
      get { return pass; } 
      set { pass = value; } 
     } 
     public string Name 
     { 
      get { return name; } 
      set { name = value; } 
     } 
     public string Phone 
     { 
      get { return phone; } 
      set { phone = value; } 
     } 
     public string Address 
     { 
      get { return address; } 
      set { address = value; } 
     } 
     public string Usrname 
     { 
      get { return usrname; } 
      set { usrname = value; } 
     } 
     public int Auth 
     { 
      get { return authority; } 
      set { authority = value; } 
     } 
     public int SpecialK 
     { 
      get { return specialK; } 
      set { specialK = value; } 
     } 

     public carniuser(string email, string usrname, string pass, string name, string phone, string address, int authority, int specialK) 
     { 
      Email = email; 
      Usrname = usrname; 
      Pass = pass; 
      Name = name; 
      Phone = phone; 
      Address = address; 
      Auth = authority; 
      SpecialK = specialK; 
     } 
    } 
} 

在这里,你有我尝试使用数据库的存储过程:

CREATE PROCEDURE ADDUSR 

@useremail varchar (35), 
@username varchar (30), 
@userpass varchar (30), 
@name varchar (100), 
@phone varchar (20), 
@address varchar (150), 
@authority int, 
@special bit 

AS 
BEGIN 
INSERT INTO USERS 
(
_UserEmail, 
_UserName, 
_UserPass , 
_Name , 
_Phone, 
_Address, 
_Authority, 
_Special 
) 
VALUES 
(
@useremail, 
@username, 
@userpass, 
@name, 
@phone, 
@address, 
@authority, 
@special 
) 
END 

回答

2

你只犯了一个错误,当你写usermail参数。

变化

oCom.Parameters.AddWithValue("@useremail ,", oUsr.Email); 

oCom.Parameters.AddWithValue("@useremail", oUsr.Email); 

,并删除所有逗号你AddWithValue方法。喜欢;

oCom.CommandType = CommandType.StoredProcedure; 
oCom.Parameters.AddWithValue("@useremail", oUsr.Email); 
oCom.Parameters.AddWithValue("@userpass", oUsr.Pass); 
oCom.Parameters.AddWithValue("@name", oUsr.Name); 
oCom.Parameters.AddWithValue("@phone", oUsr.Phone); 
oCom.Parameters.AddWithValue("@address", oUsr.Address); 
oCom.Parameters.AddWithValue("@username", oUsr.Usrname); 
oCom.Parameters.AddWithValue("@authority", oUsr.Auth); 
oCom.Parameters.AddWithValue("@special", oUsr.SpecialK); 
3
oCom.Parameters.AddWithValue("@useremail ,", oUsr.Email); 
             ^^^^ 
+0

要添加到这个答案,看看你所有的'AddWithValue'电话,他们都与他们有逗号,这是不对的。 – 2013-04-29 10:35:16

+1

感谢大家的快速解答! – octohedron 2013-04-29 10:41:29

0

尝试这一个 -

static string connectionString = "Server=.\\SQLExpress;AttachDbFilename=L:\\Apps\\VS Projects\\Carnisoftix\\CarniDb.mdf;Database=CarniDb;Trusted_Connection=Yes;"; 

public static void regUsr(carniuser oUsr) 
{ 
    using(SqlConnection oConnection = new SqlConnection(connectionString)) 
    { 
     oConnection.Open(); 

     using (SqlCommand oCom = new SqlCommand("ADDUSR", oConnection)) 
     { 
      oCom.CommandType = CommandType.StoredProcedure; 
      oCom.Parameters.AddWithValue("@useremail", oUsr.Email); 
      oCom.Parameters.AddWithValue("@userpass", oUsr.Pass); 
      oCom.Parameters.AddWithValue("@name", oUsr.Name); 
      oCom.Parameters.AddWithValue("@phone", oUsr.Phone); 
      oCom.Parameters.AddWithValue("@address", oUsr.Address); 
      oCom.Parameters.AddWithValue("@username", oUsr.Usrname); 
      oCom.Parameters.AddWithValue("@authority", oUsr.Auth); 
      oCom.Parameters.AddWithValue("@special", oUsr.SpecialK); 
      oCom.ExecuteNonQuery(); 
     } 

     oConnection.Close(); 
    } 
} 
相关问题