@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.