Click here to Skip to main content
15,915,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When i click the Register button it gives error like "Procedure or Function 'sp_InsertStudent' expects parameter '@RollNo', which was not supplied.". I m not finding how to solve it... Plz help.

C#
//code for front end that works against the clicking of Register button

private void btnRegister_Click(object sender, EventArgs e)
{
    string source = ("data source = localhost; database = student; " +
                     "Integrated Security = SSPI");
    SqlConnection conn = new SqlConnection(source);
    conn.Open();
    SqlCommand cmd = new SqlCommand("sp_InsertStudent", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    //cmd.Parameters.Add("@RollNo", SqlDbType.Int, 0, "RollNo");
    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 30, "Name");
    cmd.Parameters.Add("@CourseName", SqlDbType.VarChar, 20, 
                       "CourseName");
    cmd.Parameters.Add("@Fee", SqlDbType.VarChar, 6, 
                       "CourseName");
    cmd.ExecuteNonQuery();
    conn.Close();
}




SQL
-- My stored Procedure for inserting data in the table Student_Registration

Create Procedure sp_InsertStudent
@RollNo int,
@Name Varchar(30),
@CourseName Varchar(20),
@Fee int
As
BEGIN
 SET NOCOUNT ON
INSERT INTO dbo.Student_Registration
(
RollNo,
Name,
CourseName,
Fee
 ) 
     VALUES 
          ( 
            @RollNo,
            @Name,
            @CourseName,
            @Fee
          ) 
END 

GO


[Modified: added code formatting]
Posted
Updated 19-Mar-10 12:28pm
v5

ajitinbang wrote:
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.Add("@RollNo", SqlDbType.Int, 0, "RollNo");
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 30, "Name");


Looks to me like the line that should supply the RollNo parameter is commented out.
 
Share this answer
 
v2
I think you have a few things wrong.
1. you need values to pass to the SP
2. your type on @fee is wrong (should be int)

Try using the AddWithValue method.

C#
   private void btnRegister_Click(object sender, EventArgs e)
{  
	//Need Variables to add to SP
	int RollNumber=1;
	string Name="Some Name";
	string courseName="Course Name";
	int Fee=25;
	
	
	string source = ("data source = localhost; database = student; " +
	                 "Integrated Security = SSPI");
	SqlConnection conn = new SqlConnection(source);
	conn.Open();
	SqlCommand cmd = new SqlCommand("sp_InsertStudent", conn);
	cmd.CommandType = CommandType.StoredProcedure;
	cmd.Parameters.AddWithValue("@RollNo",RollNumber);
	cmd.Parameters.AddWithValue("@Name",Name);
	cmd.Parameters.AddWithValue("@CourseName",courseName);
	cmd.Parameters.AddWithValue("@Fee",Fee);
	cmd.ExecuteNonQuery();
	conn.Close();
} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900