Question

I am trying to add a new user to a SQL database using INSERT command but when i run it throws an error saying that it expects @Username parameter. I have given it this parameter :/

Any idea were am going wrong, iv tried for about two hours to fix but have no idea whats going wrong.

I am using ASP.Net web forms and C# code behind

CODE below:

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

SQL command

USE [LetsRent]
GO
/****** Object:  StoredProcedure [dbo].[AgentRegisterNewUser]    Script Date: 08/04/2014 14:03:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Chris
-- Create date: 08/04/2014
-- Description: Adds a new user to AgentUser Database
-- =============================================
ALTER PROCEDURE [dbo].[AgentRegisterNewUser] 
    -- Add the parameters for the stored procedure here
    @Username VarChar(50), 
    @Password VarChar(25) 
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT @Username, @Password

    INSERT INTO AgentUsers(Username, Password)
    VALUES ( @Username, @Password);
END
Was it helpful?

Solution

I guess AgentRegisterNewUser is a stored procedure, therefore the cmd.CommandType should be StoredProcedure:

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

w/o this change you are executing the T-SQL batch consisting of the text "AgentRegisterNewUser". This will compile to call the procedure, even w/o an exec, but will not pass any parameter since your params are passed to the batch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top