Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > SQLDataSource query

Reply
Thread Tools

SQLDataSource query

 
 
karlman
Guest
Posts: n/a
 
      09-25-2007
Not sure if this is a SQL question or ASP.Net control question.
Probably can do it either way I imagine.

I have a simple page that uses a SqlDataSource, DropDownList, and a
GridView. I have the DropDownList post back and is a ControlParameter
for the SqlDataSource. It works great when selecting individual items
in the DropDownList but I want to also be able to query for all in the
list.

I add an extra ListItem into the DropDownList in the Page_Load event
but I am not sure what to put for value.

Example:

SELECT * FROM [Company] WHERE Company_ID = @CompanyID

@CompanyID is a control parameter for the DropDownList

What value can I set for the 'ALL' list item so it would return all
records and basically ignore the WHERE clause.

One idea I may try is to have another SqlDataSource that does not have
the where clause and change the GridView datasource on Page_Load if it
is set to 'ALL'...

Thanks
Karl

 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 11:36 pm, karlman <(E-Mail Removed)> wrote:
> Not sure if this is a SQL question or ASP.Net control question.
> Probably can do it either way I imagine.
>
> I have a simple page that uses a SqlDataSource, DropDownList, and a
> GridView. I have the DropDownList post back and is a ControlParameter
> for the SqlDataSource. It works great when selecting individual items
> in the DropDownList but I want to also be able to query for all in the
> list.
>
> I add an extra ListItem into the DropDownList in the Page_Load event
> but I am not sure what to put for value.
>
> Example:
>
> SELECT * FROM [Company] WHERE Company_ID = @CompanyID
>
> @CompanyID is a control parameter for the DropDownList
>
> What value can I set for the 'ALL' list item so it would return all
> records and basically ignore the WHERE clause.
>
> One idea I may try is to have another SqlDataSource that does not have
> the where clause and change the GridView datasource on Page_Load if it
> is set to 'ALL'...
>
> Thanks
> Karl


Hi Karl, check the following link
http://peterkellner.net/2006/10/14/s...idviewfromddl/

 
Reply With Quote
 
 
 
 
karlman
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 2:48 pm, Alexey Smirnov <(E-Mail Removed)> wrote:
> On Sep 25, 11:36 pm, karlman <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Not sure if this is a SQL question or ASP.Net control question.
> > Probably can do it either way I imagine.

>
> > I have a simple page that uses a SqlDataSource, DropDownList, and a
> > GridView. I have the DropDownList post back and is a ControlParameter
> > for the SqlDataSource. It works great when selecting individual items
> > in the DropDownList but I want to also be able to query for all in the
> > list.

>
> > I add an extra ListItem into the DropDownList in the Page_Load event
> > but I am not sure what to put for value.

>
> > Example:

>
> > SELECT * FROM [Company] WHERE Company_ID = @CompanyID

>
> > @CompanyID is a control parameter for the DropDownList

>
> > What value can I set for the 'ALL' list item so it would return all
> > records and basically ignore the WHERE clause.

>
> > One idea I may try is to have another SqlDataSource that does not have
> > the where clause and change the GridView datasource on Page_Load if it
> > is set to 'ALL'...

>
> > Thanks
> > Karl

>
> Hi Karl, check the following linkhttp://peterkellner.net/2006/10/14/showallingridviewfromddl/- Hide quoted text -
>
> - Show quoted text -


Interesting. I actually got this to work by having two SqlDataSource
controls (one with WHERE and one without). Then I just added this code
to the page_load.

Dim li As New System.Web.UI.WebControls.ListItem("(All)",
"-1")

If Not IsPostBack Then
CompanyID.Items.Insert(0, li)
End If

If CompanyID.SelectedItem.Value = -1 Then
GridView1.DataSourceID = "SqlDataSource2"
Else
GridView1.DataSourceID = "SqlDataSource1"
End If

I will comapre and see which method I like the best.

Thank you very much!
Karl

 
Reply With Quote
 
karlman
Guest
Posts: n/a
 
      09-26-2007
On Sep 25, 2:57 pm, karlman <(E-Mail Removed)> wrote:
> On Sep 25, 2:48 pm, Alexey Smirnov <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Sep 25, 11:36 pm, karlman <(E-Mail Removed)> wrote:

>
> > > Not sure if this is a SQL question or ASP.Net control question.
> > > Probably can do it either way I imagine.

>
> > > I have a simple page that uses a SqlDataSource, DropDownList, and a
> > > GridView. I have the DropDownList post back and is a ControlParameter
> > > for the SqlDataSource. It works great when selecting individual items
> > > in the DropDownList but I want to also be able to query for all in the
> > > list.

>
> > > I add an extra ListItem into the DropDownList in the Page_Load event
> > > but I am not sure what to put for value.

>
> > > Example:

>
> > > SELECT * FROM [Company] WHERE Company_ID = @CompanyID

>
> > > @CompanyID is a control parameter for the DropDownList

>
> > > What value can I set for the 'ALL' list item so it would return all
> > > records and basically ignore the WHERE clause.

>
> > > One idea I may try is to have another SqlDataSource that does not have
> > > the where clause and change the GridView datasource on Page_Load if it
> > > is set to 'ALL'...

>
> > > Thanks
> > > Karl

>
> > Hi Karl, check the following linkhttp://peterkellner.net/2006/10/14/showallingridviewfromddl/-Hide quoted text -

>
> > - Show quoted text -

>
> Interesting. I actually got this to work by having two SqlDataSource
> controls (one with WHERE and one without). Then I just added this code
> to the page_load.
>
> Dim li As New System.Web.UI.WebControls.ListItem("(All)",
> "-1")
>
> If Not IsPostBack Then
> CompanyID.Items.Insert(0, li)
> End If
>
> If CompanyID.SelectedItem.Value = -1 Then
> GridView1.DataSourceID = "SqlDataSource2"
> Else
> GridView1.DataSourceID = "SqlDataSource1"
> End If
>
> I will comapre and see which method I like the best.
>
> Thank you very much!
> Karl- Hide quoted text -
>
> - Show quoted text -


Actually, there was an easier answer than both. I basically just
simplified the other option.

Simply add an extra listitem for 'ALL' and a value of -1. Next modify
the WHERE portion like this.

.... WHERE (Company_ID = @CompanyID Or @CompanyID = -1)

Works great and light on the code. Not sure why I didn't see it.

Karl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SqlDataSource Query Formula Problem RBeach1955 ASP .Net 0 02-11-2011 10:02 PM
SQLDataSource Query Run if Gridview is Not Visible? Paul ASP .Net 0 03-05-2009 07:08 PM
SQLDataSource and Query Speed Paul ASP .Net 1 01-21-2009 04:18 PM
run query on sqldatasource or datalist mharness ASP .Net 0 01-30-2007 07:58 PM
SqlDataSource INSERT query and TextBox text Hrvoje Vrbanc ASP .Net 0 12-08-2005 06:36 PM



Advertisments