Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 过程或函数需要未提供的参数。执行查询_C#_Asp.net_Sql Server_Visual Studio 2010_Tsql - Fatal编程技术网

C# 过程或函数需要未提供的参数。执行查询

C# 过程或函数需要未提供的参数。执行查询,c#,asp.net,sql-server,visual-studio-2010,tsql,C#,Asp.net,Sql Server,Visual Studio 2010,Tsql,因此,问题如下: 当我在注册web表单上单击“提交”按钮时(在文本框中添加相应的值后),行“oCom.ExecuteNonQuery();”上出现错误。上面说 “过程或函数'ADDUSR'需要参数'@useremail',该参数 没有提供。” 我一直在试图找出它为什么会这样说,因为我反复检查并oUsr.Email=”hello@hello.com“,该信息来自调试器,这很有意义,因为我只是在单击提交按钮之前将其添加到文本框中 using System; using System.Collecti

因此,问题如下:

当我在注册web表单上单击“提交”按钮时(在文本框中添加相应的值后),行“
oCom.ExecuteNonQuery();
”上出现错误。上面说

“过程或函数'ADDUSR'需要参数'@useremail',该参数 没有提供。”

我一直在试图找出它为什么会这样说,因为我反复检查并
oUsr.Email=”hello@hello.com“
,该信息来自调试器,这很有意义,因为我只是在单击提交按钮之前将其添加到文本框中

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

您在编写
usermail
参数时犯了一个错误

改变

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);

您在编写
usermail
参数时犯了一个错误

改变

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);
试试这个-

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();
    }
}
试试这个-

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();
    }
}

要添加到这个答案中,请查看您所有的
AddWithValue
呼叫,它们都带有逗号,这是不对的。感谢大家的快速回答!要添加到这个答案中,请查看您所有的
AddWithValue
呼叫,它们都带有逗号,这是不对的。感谢大家的快速回答!