0
public int set(string procName , object[] param)
{
      SqlConnection conn = new SqlConnection(constr);
      conn.Open();

      SqlCommand cmd = new SqlCommand(procName,conn);
      cmd.CommandType = CommandType.StoredProcedure;

      foreach(object o in param)
      {
         cmd.Parameters.Add(o);      // Error
      }
      int res = cmd.ExecuteNonQuery();
      conn.Close();
      return res;
}

The values are correctly passed via the calling function... The 2nd parameter of the function 'set' contains 2 string values.

Even at the ERROR statment the 'o' contains the first value "Computer" but represent ERROR as stated above.

1
  • could you please explain what are you trying to achieve it looks like a "generic" db access is this right? Commented Apr 24, 2013 at 13:09

2 Answers 2

2

You need to add new SqlParameter in your collection like:

cmd.Parameters.Add(new SqlParameter(o.ToString(), o));

The above statement is just to show how to add parameters to the command parameters. I am not really sure why you want to send a parameter with just a single value to the stored procedure, Your method is accepting and object type array, if the array holds SqlParameter then you can do:

cmd.Parameters.Add((SqlParameter) o);

Otherwise you need to specify the parameter name and its value.

You can modify your method to accept an array of SqlParameter type objects and then add those to the command parameters.

Sign up to request clarification or add additional context in comments.

3 Comments

I tired using it, but unfortunately no constructor of SqlParameter takes 1 argument.
@AkanshaBansal, yes, actually your object should have atleast two values, once is the parameter name and the other is its value atleast. Why are you sending object[] as parameter, and does your object contains SqlParameter ?
Yes! I am trying to make a generic function to set values via Stored Procedure into the respective tables with the required parameters.
1

try change the param type.

public int set(string procName , SqlParameter[] param)
        {
              SqlConnection conn = new SqlConnection(constr);
              conn.Open();

          SqlCommand cmd = new SqlCommand(procName,conn);
          cmd.CommandType = CommandType.StoredProcedure;

          foreach(SqlParameter o in param)
          {
             cmd.Parameters.Add(o);      // Error
          }
          int res = cmd.ExecuteNonQuery();
          conn.Close();
          return res;
    })

You can get how to create SqlParameter SqlParameter Constructor

If u want create array: SqlParameter[] parameter = { new SqlParameter(...), new SqlParameter(...), new SqlParameter(...) };

1 Comment

If i does so then i am unable to create a SqlParameter[] to pass to the function 'set'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.