Re: Newbie question about dataadapter parameter by William
William
Thu Jul 28 11:38:10 CDT 2005
There are a number of ways to handle sparse parameter lists (where one or
more parameters are left off). However, these (generally) assume you're
calling a stored procedure that sets default values (used when the parameter
value is not supplied).
When you setup any query you must supply values for any parameter
placeholders you define in the query either by setting the corresponding
Parameter.Value.
One approach that I'm working on now is to use the Command.Clone function
that permits me to define a separate Command object for each variation I
want to use. In this case, I set the Value property to either a hard-coded
default value or to Nothing (in VB.NET) or Null (in C#). Of course this
assumes that you're calling a SP -- and using ADO.NET 2.0 (which adds the
"Clone" function). Until then, you'll have to build separate Command objects
by hand...
If you're using ad hoc queries (most production apps don't), you'll probably
have to create a clone (or multiple instances) of the Command and change the
SELECT's WHERE clause to reflect the changes in the desired parameter
configuration.
The bottom line: This is not that hard to do if you're using SPs.
hth
--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
"Alphonse" <amphysvena@gmail.com> wrote in message
news:1122542258.743087.317810@g47g2000cwa.googlegroups.com...
> Hi,
> I have a dataadapter that has 2 parameters. But sometimes I would like
> to use only one of the parameters.
>
> For example:
>
> The dataadapter selectcommand would be like this:
> SELECT CategoryID, CategoryName, Description, Picture
> FROM Categories
> WHERE (CategoryID = @CID) AND (CategoryName = @CName)
>
>
> On My code, before filling the dataset, I would add the parameters
>
> Mydataadapter.SelectCommand.Parameters("@CID").Value = 1
> Mydataadapter.SelectCommand.Parameters("@CName").Value = "Beverages"
> Mydataadapter.Fill(Mydataset)
>
> This Code would run nicely. But how if I want to ignore one of the
> parameter? For example I just want to use The @CID parameter. How to
> ignore the @CName parameter?
>
>
> Hope, my question is clear cause my English is not so good. All helps
> will be greatly appreciated. Thank you
>