Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using asp.net for which i have the database in which one of my field comments having datatype varchar(max) in SQL Server 2012 and the value can be null inserted also. In database there is no problem inserting into the database table while coming to asp.net using stored procedure i am not giving any information in the textbox that means empty string. I am getting error "Procedure or function 'procedure name' expects parameter '@Comments', which was not supplied."

What I have tried:

Database column:
[Comments] VARCHAR (MAX) NULL

.Net Code:
string commentsText = (!string.IsNullOrEmpty(txtComments.Text)) ? txtComments.Text : null

Command Addwithvalue:
cmd.Parameters.AddWithValue("@Comments", commentsText);

Getting Error this line:
cmd.ExecuteNonQuery();
Posted
Updated 22-Mar-18 21:08pm
Comments
Animesh Datta 23-Mar-18 1:56am    
Put your complete C# code
Sakina Molvi 31-Jan-24 4:40am    
hello
Member 8583441 23-Mar-18 2:06am    
string spAddOrUpdateUser = "user procedure name";
using (cmd = new SqlCommand(spAddOrUpdateUser, con))
{
cmd.CommandType = CommandType.StoredProcedure;

/* adding paramerters to SqlCommand below */
cmd.Parameters.AddWithValue("@StatementType", "Insert");
cmd.Parameters.AddWithValue("@EmpID", empID);
cmd.Parameters.AddWithValue("@NumberOfDays", txtNoOfDays.Text);
cmd.Parameters.AddWithValue("@LeaveFromDate", txtStartDate.Text);
cmd.Parameters.AddWithValue("@LeaveToDate", txtEndDate.Text);
cmd.Parameters.AddWithValue("@CreatedOn", DateTime.Now);
cmd.Parameters.AddWithValue("@TypeOfLeave", leaveTypes);
cmd.Parameters.AddWithValue("@StatusOfLeave", "Pending...");
cmd.Parameters.AddWithValue("@ReasonForLeave", txtReasonofLeave.Text);

// txtComments.Text == "" ? null : txtComments.Text;
cmd.Parameters.AddWithValue("@Comments", commentsText);

// Output Parameter
cmd.Parameters.Add("@ResponseMessage", SqlDbType.NVarChar, 250);
cmd.Parameters["@ResponseMessage"].Direction = ParameterDirection.Output;

con.Open();
cmd.ExecuteNonQuery();
con.Close();

lblUserLeavesMessage.Text = cmd.Parameters["@ResponseMessage"].Value.ToString();
}
Member 8583441 23-Mar-18 2:53am    
Problem Solved.....

What the error means is that you have a stored procedure which needs a parameter called @Comments and that the parameter cannot be a null value, which is exactly what you pass if the user doesn't enter anything:
string commentsText = (!string.IsNullOrEmpty(txtComments.Text)) ? txtComments.Text : null;
Instead of this, try always passing a string:
cmd.Parameters.AddWithValue("@Comments", txtComments.Text.Trim());
And that should work because the Text property of a TextBox can never be null.
 
Share this answer
 
The problem is at the database i included ISNULL() method for the column i required in the stored procedure this value is setting to the column.
 
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