locked
How to set SetParameter function as a Class RRS feed

  • Question

  • User1393905103 posted

    Once again, sorry for the very basic question here.

    I'm currently using this routine to set parameters in my SQLCommands and I would like this to be in a Class so that I don't have to add it to the coding for each page I might use it in.

    The only classes I've done to this point are more data components for defining Lists or variables to pass to another routine.

    private SqlParameter SetParameter(string value, string var, System.Data.SqlDbType DataType, System.Data.ParameterDirection pd)

            {      

               SqlParameter sp = new SqlParameter();

                sp.SqlDbType = DataType;

                sp.Direction = pd;

                sp.ParameterName = var;

               

    switch (DataType)

                {

                case SqlDbType.Int:

                        sp.Value = Convert.ToInt32(value);

                       break;

                   

                case SqlDbType.Text:

                        sp.Value = value;

                       break;

                   

                 case SqlDbType.NVarChar:

                        sp.Value = value;

                        break;

                   

                   case SqlDbType.DateTime:

                        sp.Value = Convert.ToDateTime(value);

                       break;

                   

                     case SqlDbType.Money:

                        sp.Value = Convert.ToDecimal(value);

                       break;

                   

                     case SqlDbType.Bit:

                        sp.Value = Convert.ToBoolean(value);

                       break;

                   

                     case SqlDbType.Decimal:

                         sp.Value = Convert.ToDecimal(value);

                       break;

                }

               

    return sp;

            }

    I appreciate any help you can provide,

    Mike

    Saturday, January 24, 2015 9:50 AM

Answers

  • User-760709272 posted

    Create a new class and give it an appropriate name, I called it ParemeterHelper.

    public class ParameterHelper
    {
        public static SqlParameter SetParameter(string value, string var, System.Data.SqlDbType DataType, System.Data.ParameterDirection pd)
        {
            SqlParameter sp = new SqlParameter();
    
            // your existing code
    
            return sp;
        }
    }

    To use the code

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlParameter p = ParameterHelper.SetParameter("a", "b", SqlDbType.VarChar, ParameterDirection.Input);
    }

    The only trick is to ensure the appropriate "using" is at the top of the page, but VS can add this for you...right-click on any instance of "ParameterHelper" with a red line under it and select "Resolve" from the right-click context menu.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2015 6:50 AM

All replies

  • User1711366110 posted

    Hi Mike225,
      As per your case ,
    1. You can keep this function in separate class.
    2.Also you can create the similar function for getting parameter value (AddWithValue).
    3. Using For Loop ,you can iterate & pass the parameter name & value with the help of ArrayList & property in multiple class. 
    For your reference ,check this link which may you give some ideas for this case.

    http://referencesource.microsoft.com/#System.Data/System/Data/SqlClient/SqlCommand.cs
    --
    with regards,
    Edwin

    Monday, January 26, 2015 6:33 AM
  • User-760709272 posted

    Create a new class and give it an appropriate name, I called it ParemeterHelper.

    public class ParameterHelper
    {
        public static SqlParameter SetParameter(string value, string var, System.Data.SqlDbType DataType, System.Data.ParameterDirection pd)
        {
            SqlParameter sp = new SqlParameter();
    
            // your existing code
    
            return sp;
        }
    }

    To use the code

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlParameter p = ParameterHelper.SetParameter("a", "b", SqlDbType.VarChar, ParameterDirection.Input);
    }

    The only trick is to ensure the appropriate "using" is at the top of the page, but VS can add this for you...right-click on any instance of "ParameterHelper" with a red line under it and select "Resolve" from the right-click context menu.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2015 6:50 AM