Avatar of N P
N P
 asked on

problems passing parameters

Hi, I have a C# web project where I am trying to pass an array from one class to another, but I keep getting errors during build saying:

) expected
; expected
invalid expression term ")"


here is my code trying to pass SqlParameter array


            if (sortExpression.Length>0)
            {
                    // get the last 3 or 4 character of expression to find sort direction
                    string direction = sortExpression.Substring(sortExpression.LastIndexOf(' ')+1, sortExpression.Length-sortExpression.LastIndexOf(' ')-1);
                    SqlParameter[] p = new SqlParameter[1];
                    p[0] = new SqlParameter("@direction", direction.ToString());
                    DataTable dt = Db.GetDataTable(sql.ToString(), SqlParameter p);     <======== it is having a problem with this statement




here is the class recieving the parameter

      public static DataTable GetDataTable(string sql, SqlParameter p)
        {
            return GetDataSet(sql).Tables[0];
        }


It got to be something simple that I am missing.

thanks
Nick
C#

Avatar of undefined
Last Comment
andrewjb

8/22/2022 - Mon
andrewjb

DataTable dt = Db.GetDataTable(sql.ToString(), SqlParameter p);

should be

DataTable dt = Db.GetDataTable(sql.ToString(), p);
ASKER CERTIFIED SOLUTION
andrewjb

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
N P

ASKER
thanks, I guess when passing parameters, you don't tell it what type.  thanks again.
N P

ASKER
Maybe I jump the gun to quick.  Whenever I pass parameters as follows:

DataTable dt = Db.GetDataTable(sql.ToString(), p);

it doesn't like it, I get the following error


Error      20      The best overloaded method match for 'WareHouse.DataLayer.DataObjects.Db.GetDataTable(string, System.Data.SqlClient.SqlParameter)' has some invalid arguments      C:\IList_WareHouse_Solution\DataObjects\SqlServer\SqlServerProductDao.cs      49      36      DataObjects

but it does like:

DataTable dt = Db.GetDataTable(sql.ToString(),  p[0]);

what if I have more items in my array such as:


  p[0] = new SqlParameter("@direction", direction.ToString());
  p[1] = new SqlParameter("@somethingelse", somethingelse.ToString());
  p[2] = new SqlParameter("@anotherthing", anotherthing.ToString());


wouldn't I try to pass it as DataTable dt = Db.GetDataTable(sql.ToString(), p); instead of DataTable dt = Db.GetDataTable(sql.ToString(),  p[0]);

thanks
Nick
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
andrewjb

Well, your GetDataTable() method doesn't actually use the parameter 'p', so it's a little difficult to tell what you're doing.... What's the plan?