Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this code in my DataAccessLayer, I want to call this method to store data into my database. How do I call this method with defined parameters?

I didn't write this, but I need to use it.


C#
public int InsertData(string StoredProcedureName, List<DataParameters> lParameters)
         {
             int iResult = 0;
             try
             {
                 DataConnector oDataConnector = new DataConnector();
                 oSqlConnection = oDataConnector.GetConnection();

                 oSqlCommand = new SqlCommand(StoredProcedureName, oSqlConnection);
                 oSqlCommand.CommandType = CommandType.StoredProcedure;

                 foreach (DataParameters item in lParameters)
                 {
                     SqlParameter oParameter = new SqlParameter();
                     oParameter.ParameterName = item.ParameterName;
                     oParameter.DbType = item.ParameterDataType;
                     oParameter.Value = item.ParameterValue;
                     oSqlCommand.Parameters.Add(oParameter);

                 }
                 iResult = oSqlCommand.ExecuteNonQuery();

             }
             catch (Exception ex)
             {
                 Log.CreateLog(ex.ToString());

             }
             finally
             {
                 if (oSqlConnection.State != ConnectionState.Closed)
                 {
                     oSqlConnection.Close();
                 }

                 if (oSqlConnection != null)
                     oSqlConnection.Dispose();

                 if (oSqlCommand != null)
                     oSqlCommand.Dispose();

                 if (oSqlAdapter != null)
                     oSqlAdapter.Dispose();

             }
             return iResult;
         }


class DataParameters
{

    private string iParameterName;
    private System.Data.DbType iParameterDataType;
    private string iParameterValue;

    /// <summary>
    /// This method is to add parameters by creating a list object
    /// </summary>
    /// <param name="ParameterName"></param>
    /// <param name="ParameterDataType"></param>
    /// <param name="ParameterValue"></param>
    public DataParameters(string ParameterName, System.Data.DbType ParameterDataType, string ParameterValue)
    {
        this.iParameterName = ParameterName;
        this.iParameterDataType = ParameterDataType;
        this.iParameterValue = ParameterValue;
    }

    /// <summary>
    /// Property for setting ParameterName
    /// </summary>
    public string ParameterName
    {
        get { return iParameterName; }
        set { iParameterName = value; }
    }

    /// <summary>
    /// Property for setting ParameterDataType
    /// </summary>
    public System.Data.DbType ParameterDataType
    {
        get { return iParameterDataType; }
        set { iParameterDataType = value; }
    }


    /// <summary>
    /// Property for setting the ParameterValue
    /// </summary>
    public string ParameterValue
    {
        get { return iParameterValue; }
        set { iParameterValue = value; }
    }
}
Posted

Instead of asking it here, try to learn basics of programming, nowadays, there are a lot of resources available

XML
//Create list

List<DataParameters> lParameters=new List<DataParameters>();

//Create paramters, you should change @NameOfParamter, System.Data.DbType.String, Value of paramter as per your requirment

DataParameters param1=New DataParameters("@NameOfParamter",System.Data.DbType.String,"Value of paramter");

//Add parametrs to the list

lParameters.Add(param1);



//call the method

int result=InsertData("Your sp name",lParameters);
 
Share this answer
 
Comments
King Fisher 10-Sep-15 8:55am    
if i want to pass another parameter ,Is this i have to do?
DataParameters param2=New DataParameters("@NameOfParamter",System.Data.DbType.String,"Value of paramter");
lParameters.Add(param2);

but if i wanna pass 10 parameters to list, then What i have to do?
should i have to repeat it.
Abdul Samad KP 11-Sep-15 17:03pm    
If you want to pass 10 parameters , you have to add all of them to the list
Enemali William 10-Sep-15 9:05am    
You iterate through the data and call the function at every iteration to save the record
King Fisher 10-Sep-15 9:08am    
I have to store all the data in list which i want to store, then i have to call the method with the collection list.
//Create list

List<dataparameters> lParameters=new List<dataparameters>();


for (int i=0; i < number_of_parameters_to_insert; i++)
{

DataParameters param+i=New DataParameters("@NameOfParamter",System.Data.DbType.String,"Value of paramter");
lParameters.Add(param+i);


}
//call the method

int result=InsertData("Your sp name",lParameters);


You iterate through the item you want to add as a parameter and build up your list then call the insertData method after adding all items. You dont need to create multiple dataparameters to insert into a list. just reuse the existing one
 
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