Link to home
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
Avatar of andrewjb
andrewjb
Flag of United Kingdom of Great Britain and Northern Ireland image

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

should be

DataTable dt = Db.GetDataTable(sql.ToString(), p);
ASKER CERTIFIED SOLUTION
Avatar of andrewjb
andrewjb
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of N P
N P

ASKER

thanks, I guess when passing parameters, you don't tell it what type.  thanks again.
Avatar of 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
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?