An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll. Additional information: Procedure or function 'UserAdd' expects parameter '@UserID', which was not supplied.

Apex6554 60 Reputation points
2023-04-05T01:43:36.26+00:00
Fixed the one problem I was having then I get a new error Message... 




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ProjectProtocol_MW3_RTM_Tool
{
    public partial class Registration : Form
    {
        string connectionString = @"Data Source=DESKTOP-S0G9CIH\APEX6654;Initial Catalog=tbl_Register;Integrated Security=True";

        public Registration()
        {
            InitializeComponent();
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            this.Close();
            Form1 back = new Form1();
            back.Show();
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlCon = new SqlConnection(connectionString))
            {
                if (txtUsername.Text == "" || txtPassword.Text == "")
                    MessageBox.Show("Please fill mandatory fields");
                sqlCon.Open();
                SqlCommand sqlCmd = new SqlCommand("UserAdd", sqlCon);
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
                sqlCmd.ExecuteNonQuery();
                MessageBox.Show("Registration Succesfully !");
                Clear();
            }
        }
        void Clear()
        {
            txtEmail.Text = txtUsername.Text = txtPassword.Text = "";
        }
    }
}

SQL Server Other
Developer technologies C#
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2023-04-05T06:11:43.9366667+00:00

    @Apex6554, Welcome to Microsoft Q&A, based on my test, I reproduced your problem. The error means that your stored producedure has 4 parameters, but you only input 3 parameters, which missed @UserID column. There are two methods you could try to solve the problem.

    First, you could remove @UserID int, in your stored procedure, like the following(without changing c# code): Stored procedure:

    CREATE PROC UserAdd @Email varchar(50), @Username varchar(50), @Password varchar(50) AS INSERT INTO UserReg(Email,Username,Password) VALUES(@Email,@Username,@Password)
    

    Table:

    CREATE TABLE [dbo].[UserReg] (
    [UserId]       INT           IDENTITY (1, 1) NOT NULL,
    [Email]    NVARCHAR (50) NULL,
    [Username] NVARCHAR (50) NULL,
    [Password] NVARCHAR (50) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
    );
    

    Note: we need to set the id to be self-growing. Second, you could try to add a parameter in c# code if you want to insert userid:

    using (SqlConnection sqlCon = new SqlConnection(connectionString))
            {
                if (textBox1.Text == "" || textBox2.Text == "")
                    MessageBox.Show("Please fill mandatory fields");
                sqlCon.Open();
                SqlCommand sqlCmd = new SqlCommand("UserAdd", sqlCon);
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@UserID",1001)
                sqlCmd.Parameters.AddWithValue("@Email", textBox1.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Username", textBox2.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@Password", textBox3.Text.Trim());
                sqlCmd.ExecuteNonQuery();
                MessageBox.Show("Registration Succesfully !");
                
            }
    

    Stored procedure:

    CREATE PROC UserAdd
    @UserID int,
    @Email varchar(50),
    @Username varchar(50),
    @Password varchar(50)
    AS
    INSERT INTO UserReg( UserID   ,Email,Username,Password)
    VALUES( @UserID,  @Email,@Username,@Password)
    

    Table:

    CREATE TABLE [dbo].[UserReg] (
    [UserID]   INT           NOT NULL,
    [Email]    NVARCHAR (50) NULL,
    [Username] NVARCHAR (50) NULL,
    [Password] NVARCHAR (50) NULL,
    PRIMARY KEY CLUSTERED ([UserId] ASC)
    );
    

    Hope my advice could help you. Best Regards, Jack If the answer is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.