Click here to Skip to main content
15,903,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

How do i add a new parameter

new SqlParameter("@LastName", app.LastName??"")

to @params in the If condition.



SqlParameter[] @params = {
SqlParameter("@AppId", app.AppId),
									new SqlParameter("@FirstName", app.FirstName??""),
										new SqlParameter("@MiddleName", app.MiddleName ?? ""),
										new SqlParameter("@Gender", app.Gender??""),
};
@params[0].Direction = ParameterDirection.Output;
if (app.AppId == 1)
				{
					SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, "Create_Kiosk", @params);
				}
				else
				{
					SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, "Create", @params);
				}


What I have tried:

@params[0].Direction = ParameterDirection.Output;
if (app.AppId == 1)
				{
@params.Concat(new SqlParameter("@LastName", app.LastName??""));
					SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, "Create_Kiosk", @params);
				}
				else
				{
					SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, "Create", @params);
				}
Posted
Updated 19-Jul-18 6:06am
v3

An array is a fixed-size data structure. You can't add items to it without creating a new array. The remarks on the Array.Resize method[^] explain:
Quote:
This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.

A better option would be to use List<T> to store your parameters. If the method you're calling only accepts an array, then you can call the lists's ToArray method[^] to get one.
C#
string procedureName;

var @params = new List<SqlParameter>
{
    new SqlParameter("@AppId", app.AppId) { Direction = ParameterDirection.Output },
    new SqlParameter("@FirstName", app.FirstName ?? ""),
    new SqlParameter("@MiddleName", app.MiddleName ?? ""),
    new SqlParameter("@Gender", app.Gender ?? ""),
};

if (app.AppId == 1)
{
    procedureName = "Create_Kiosk";
    @params.Add(new SqlParameter("@LastName", app.LastName ?? ""));
}
else
{
    procedureName = "Create";
}

SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, procedureName, @params.ToArray());
 
Share this answer
 
SqlParameter[] @params;


if (app.AppId == 1)
				{
@params = {
SqlParameter("@AppId", app.AppId),									new SqlParameter("@FirstName", app.FirstName??""),										new SqlParameter("@MiddleName", app.MiddleName ?? ""),										new SqlParameter("@Gender", app.Gender??"")}; //add other params

@params[0].Direction = ParameterDirection.Output;

SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, "Create_Kiosk", @params);
				}
				else
				{
@params = {
SqlParameter("@AppId", app.AppId),									new SqlParameter("@FirstName", app.FirstName??""),										new SqlParameter("@MiddleName", app.MiddleName ?? ""),										new SqlParameter("@Gender", app.Gender??"")}; //add other params

@params[0].Direction = ParameterDirection.Output;

					SqlHelper.ExecuteNonQuery(cn, CommandType.StoredProcedure, "Create", @params);
				}
 
Share this answer
 
Comments
wizklaus 18-Jul-18 7:20am    
Thanks @Ziee-M. But its not a solution i can accept as i have more than 50 parameters in the source code. I have read other instance where List was used. but cant it be archive with an array? adding a new parameter to sqlparameter[] collection.
Eric Lynch 18-Jul-18 14:58pm    
You may want to look at the SqlParameterCollection.AddRange(SqlParameter[]) method, described here: https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlparametercollection
Ziee-M 18-Jul-18 9:11am    
Making multiple SqlParameters does not make sense to me, because, a table has always the same number of parameters.
Basically, you should have a unique parameter parser for all of your CRUD method, and reuse it.
In case a paramter is not applicable, you should set a default value for that specific param or just ignore it(you should set that field nullable in the database level)

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