943,989 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 1104
  • ASP.NET RSS
Oct 30th, 2009
0

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

Expand Post »
I have a simple sqlDataSource that populates a GridView control
Nothing out of the ordinary here.
ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 9
Solved Threads: 0
Light Poster
ricksvoid is offline Offline
39 posts
since May 2008
Nov 3rd, 2009
0
Re: programmaticly alter the command text of a sqldatasource???anyone?
Try like this.

ASP.NET Syntax (Toggle Plain Text)
  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. }
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Nov 9th, 2009
0
Re: programmaticly alter the command text of a sqldatasource???anyone?
Yes, thanks Ramesh. This worked great.
Reputation Points: 9
Solved Threads: 0
Light Poster
ricksvoid is offline Offline
39 posts
since May 2008
Nov 10th, 2009
0
Re: programmaticly alter the command text of a sqldatasource???anyone?
Hi Rick Pace,

Please mark this thread solved if your question is answered.

Regards
Ramesh S
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Editing Registry Keys
Next Thread in ASP.NET Forum Timeline: SqlDateTime Overflow Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC