Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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_Sql Insert - Fatal编程技术网

C# 缺少参数?即使我已经诱导了它

C# 缺少参数?即使我已经诱导了它,c#,asp.net,sql,sql-insert,C#,Asp.net,Sql,Sql Insert,我试图使用INSERT命令将新用户添加到SQL数据库中,但运行时抛出一个错误,表示它需要@Username参数。我给它这个参数:/ 我试了大约两个小时来修复,但不知道出了什么问题 我正在使用ASP.NETWeb表单和C#代码隐藏 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.Web

我试图使用INSERT命令将新用户添加到SQL数据库中,但运行时抛出一个错误,表示它需要@Username参数。我给它这个参数:/

我试了大约两个小时来修复,但不知道出了什么问题

我正在使用ASP.NETWeb表单和C#代码隐藏

代码如下:

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

using System.Web.Security;
using System.Data.SqlClient;
using HashLibrary;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Data;

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

        }

        protected void LoginBtn_Click(object sender, EventArgs e)
        {
            ResultLbl.Text = Hasher.HashString(PasswordTxt.Text.Trim());
            if (UsernameTxt.Text.Length <= 50 && PasswordTxt.Text.Length <= 25)
            {
                SqlConnection conn = null;

                try
                {
                    conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LetsRent"].ConnectionString);
                    SqlCommand cmd = new SqlCommand("AgentRegisterNewUser", conn);

                    //SqlParameter user = new SqlParameter();
                    //user.ParameterName = "@username";
                    //user.Value = UsernameTxt.Text.Trim();
                    //cmd.Parameters.Add(user);
                    cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = UsernameTxt.Text.Trim();


                    SqlParameter pass = new SqlParameter();
                    pass.ParameterName = "@password";
                    pass.Value = Hasher.HashString(PasswordTxt.Text.Trim());
                    cmd.Parameters.Add(pass);

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        ResultLbl.Text = "User added!";
                    }
                    catch (Exception ex)
                    {
                        ResultLbl.Text = "error";
                        throw new Exception("Execption adding account. " + ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    ResultLbl.Text = "error";
                    throw new Exception("Execption adding account. " + ex.Message);
                }
            }
          }
    }
}

我猜
AgentRegisterNewUser
是一个存储过程,因此应该是
StoredProcedure

SqlCommand cmd = new SqlCommand("AgentRegisterNewUser", conn);
cmd.CommandType = CommandType.StoredProcedure;

在没有此更改的情况下,您正在执行由文本“AgentRegisterNewWuser”组成的T-SQL批处理。这将编译以调用该过程,即使没有exec,但不会传递任何参数,因为您的参数已传递给批处理。

请发布Hasher类的代码。我想这可能是你的问题。具体地说,我认为问题出在这个类的HashString方法中。您的
SqlCommand
是做什么的?它是一个存储过程吗?它只包含
AgentRegisterNewUser
?@christopaisios哈希器类显然是用来哈希密码的。这与他的问题有什么关系?是的,Hasher类可以工作。我还发布了SQL内部命令,您需要将命令类型指定为存储过程。请检查下面Remus的答案。我不会添加一个答案,因为你已经有了一个很好的答案,除非你还有另一个问题。顺便说一句(所以应该有第三个参数的过程…),但这是一个不同的讨论…我不知道如何盐使用asp.net。。。可能是我要调查的事情
SqlCommand cmd = new SqlCommand("AgentRegisterNewUser", conn);
cmd.CommandType = CommandType.StoredProcedure;