Solved

Passing parameter of unknown type

Posted on 2009-04-10
11
559 Views
Last Modified: 2013-12-17
Greetings all

I have a page from which the user selects a procedure to execute. Each procedure may be linked to one or more parameters (stored in a db table as a string value and a single character, indicating the type).

Once the user has selected the procedure, he or she is then able to populate the parameters.

When the user hits the "Execute" button I want to pass the procedure name and parameters to the datalayer which will do the retrieval.

My problem(s):

1.  Number of paraneters per procedure varies (some have no parameters)
2.  The parameter types vary

How can I do this?


In advance, thanks!!!


    allanmark

0
Comment
Question by:allanmark
  • 4
  • 4
  • 3
11 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 24117811
how about an object array?
0
 
LVL 22

Accepted Solution

by:
p_davis earned 300 total points
ID: 24117843
void DynamicMethod(Object[] objectArray)
{

   for(int i = 0; i<objectArray.Length;i++)
  {
          if(objectArray[i].GetType() == typeof(String))
                ProcessStringType(objectArray[i] as String);

//something like this maybe?  if not can you give a little more on what you are wanting to do.  


  }
}
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24117977
Would you be open to creating a snippet of XML

<procedures>
  <proc>proc_1
    <param>param_val_a</param>
    <param>param_val_b</param>
 </proc>
 <proc>proc_6
  <param>sometext</param>
 </proc>
<procedures>


Then at the other end you can unravel the XML and do what logic you have to do

Let me know if you want anymore help with this solution


   
0
 

Author Comment

by:allanmark
ID: 24118080
At this stage, XML isn't an option (as determined by the powers that be!)!

There are parameter records (param name [as will be in the stored proc],  param description, param type [single char], default value [varchar]). These are read in and  the appropriate controls are dynamically created. When the user has entered the necessary parm details and clicks the execute button, I want to pass the params to a "generic" method in the DataLayer that will build the parametrs for the stored proc and then call the procedure.

My first idea was to create a class for the params (see snippet) and then create a <List> of param objects, populated with the param name [from the db record], the type [also from the db record] and the value [from the control - ie textbox, checkbox, etc].
using System;

using System.Collections.Generic;

using System.Text;
 

namespace DataTransferObjects

{

    public class DbParam

    {

        private string parmName;

        private char parmType;

        private object parmVal;
 

        public DbParam(string pParmName, char pParmType, object parmVal)

        {

            ParmName = pParmName;

            ParmType = pParmType;

            ParmVal = parmVal;

        }
 

        public string ParmName

        {

            get { return parmName;}

            set { parmName = value;}

        }
 

        public object ParmVal

        {

            get { return parmVal; }

            set { parmVal = value; }

        }
 

        public char ParmType

        {

            get { return parmType; }

            set { parmType = value; }

        }
 

    }

}

Open in new window

0
 
LVL 22

Expert Comment

by:p_davis
ID: 24118108
so what's wrong with that approach? seems like if it fits all your needs it would be a good idea. and with creating your own object you won't be taking a performance hit of any kind.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:williamcampbell
ID: 24118220
The powers that be are narrow minded :)

 With LINQ and XML this would be a snip using XElement

But as p_davis says your build a list of Params and pass those is probably the easiest at this stage.

(You could also dynamically build the Stored Proc on the client and pass that as a string, save server time)
0
 

Author Comment

by:allanmark
ID: 24118441
Thanks, guys!

Going to take a coffee break and then get onto it. Will post back, later.
0
 

Author Comment

by:allanmark
ID: 24118809
Ok. So far so good. I now have a list of parameter objects.

Next step is to set up some sort of structure for the dat taht comes back from the stored procedure (which ever one that might be).

Since I have no idea how many fields are being returned and also, at this stage, am not interested in the field types, would the following (see snippet) suffice as the basis for receiving the data.
List<object[]> myRecords = new List<object[]> ;
 

object[] returnedFields;
 

sqlConn.Open();

SqlDataReader drResults = sqlGetResults.ExecuteReader();

while (drResults.Read())

{

  returnFields = null;

  for (int i = 0; i < drResults.FieldCount; i++)

  {

     returnedFields[i] = drResults.getValue(i);

  }

  myRecords.Add(returnedFields);

}

Open in new window

0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24119044
Would the below work...
while (drResults.Read())

{

     myRecords.Add(drResults.Clone());

}

Open in new window

0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 300 total points
ID: 24119048
i believe you would have to new up the  object array item

returnedFields[i] = new Object();
returnedFiedls[i] = drREsults.GetValue(i);
0
 

Author Closing Comment

by:allanmark
ID: 31568998
100%!!  Many thanks!!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now