programmaticly alter the command text of a sqldatasource???anyone?

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 34
Reputation: ricksvoid is an unknown quantity at this point 
Solved Threads: 0
ricksvoid ricksvoid is offline Offline
Light Poster

programmaticly alter the command text of a sqldatasource???anyone?

 
0
  #1
Oct 30th, 2009
I have a simple sqlDataSource that populates a GridView control
Nothing out of the ordinary here.
  1. <asp:SqlDataSource ID="personalInfoSource" runat="server" ConnectionString="<%$ appSettings:Maya%>"
  2. SelectCommand="SELECT customerID, lname, fname, dateNow FROM dbo.customers ORDER BY lname ASC">
  3. </asp:SqlDataSource>
  4.  
  5. <asp:GridView ID="names" runat="server" Width="280px" DataSourceID="personalInfoSource"
  6. AutoGenerateColumns="False" DataKeyNames="customerID" AllowSorting="True">
  7. <RowStyle BackColor="#FFBA00" Font-Names="arial" ForeColor="#26354a" />
  8. <AlternatingRowStyle BackColor="#D3DCE6" Font-Names="arial" ForeColor="#26354a"/>
  9. <HeaderStyle BackColor="#D3DCE6" ForeColor="#26354a" Font-Names="arial" />
  10. <Columns>
  11. <asp:BoundField DataField="customerID" HeaderText="Id" SortExpression="customerID"/>
  12. <asp:BoundField DataField="lname" HeaderText="Last Name" />
  13. <asp:BoundField DataField="fname" HeaderText="First Name" />
  14. <asp:BoundField DataField="dateNow" HeaderText="Id" Visible="false"/>
  15.  
  16. <asp:CommandField ShowSelectButton="true" ButtonType="Button"
  17. SelectText="Select" ItemStyle-HorizontalAlign="left"
  18. HeaderText="Details" >
  19. <ItemStyle HorizontalAlign="Left" />
  20. </asp:CommandField>
  21. </Columns>
  22. </asp:GridView>

The problem is that my client wants to be able to click a button and refine the querry by only showing certain records instead of all of them. Can i do this programmaticaly? Can I programaidically change the commandtext so that it says :
SELECT customerID, lname, fname, dateNow FROM dbo.customers WHERE rep is null> ORDER BY lname ASC

I beleive that i can but im unsure of what the proper syntax is. Remember, I want the gridview to display all records by defalut and only filter them as the result of some event such as a button click.

Thanks in advance.
Rick Pace
Last edited by Ezzaral; Oct 30th, 2009 at 4:15 pm. Reason: Snipped url. Please restrict such links to your site-wide user signature, which can be edited from the user control panel.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 442
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training
 
0
  #2
34 Days Ago
Try like this.

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. SqlDataSource1.SelectCommand = "SELECT customerID, lname, fname, dateNow FROM dbo.customers ORDER BY lname ASC";
  6. }
  7. }
  8. protected void SqlDataSource1_Init(object sender, EventArgs e)
  9. {
  10.  
  11. SqlDataSource1.SelectCommand = String.Empty;
  12. }
  13. protected void Button1_Click(object sender, EventArgs e)
  14. {
  15.  
  16. SqlDataSource1.SelectCommand = "SELECT customerID, lname, fname, dateNow FROM dbo.customers WHERE rep is null ORDER BY lname ASC";
  17.  
  18. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 34
Reputation: ricksvoid is an unknown quantity at this point 
Solved Threads: 0
ricksvoid ricksvoid is offline Offline
Light Poster
 
0
  #3
27 Days Ago
Yes, thanks Ramesh. This worked great.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 442
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training
 
0
  #4
27 Days Ago
Hi Rick Pace,

Please mark this thread solved if your question is answered.

Regards
Ramesh S
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC