0

I want to insert a value by using stored procedure in mssql. This is my stored procedure :

ALTER PROCEDURE dbo.insertNewMember 
(@name varchar(30), 
@age int )
AS
BEGIN 
    INSERT INTO member(memId, name, age) VALUES ('', @name, @age);
END 

My C# code, to use this procedure:

addStaffSql.InsertCommand = "insertNewMember";
addStaffSql.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
addStaffSql.InsertParameters.Add("@name", name);
addStaffSql.InsertParameters.Add("@age", age);
addStaffSql.Insert();

But I got the following error when I run the program:

Procedure or function 'insertNewMember' expects parameter '@name', which was not supplied.

If you have got any idea, I will appreciate.

2 Answers 2

3

Try losing the "@" symbol in the Add() methods.

addStaffSql.InsertCommand = "insertNewMember";
addStaffSql.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
addStaffSql.InsertParameters.Add("name", name);
addStaffSql.InsertParameters.Add("age", age);
addStaffSql.Insert();
Sign up to request clarification or add additional context in comments.

2 Comments

Due to this solution, this error message is given : "String or binary data would be truncated. The statement has been terminated."
@Shnkc Your SP shows that the data type for "name" is varchar(30), which could be what's causing the problem. Check the length of the string that you're passing to the procedure.
0

I guess you are passing null in the variable called name. You may want to change your code for that.

if(name!=null)
{
   addStaffSql.InsertCommand = "insertNewMember";
   addStaffSql.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
   addStaffSql.InsertParameters.Add("@name", name);
   addStaffSql.InsertParameters.Add("@age", age);
   addStaffSql.Insert();
}
else
{
  //Send message back to user that name is not filled properly !
}

1 Comment

I check all the variables. If I use this query : "addStaffSql.InsertCommand = "INSERT INTO member(name, age) VALUES('" + name + "' , '" + age + "')";", it is successfully evaluated. But I need to do it with stored procedure which I couldn't have succeeded yet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.