1

I have the following two controls in my code

gridview (for data display) sqldatasource (source of my gridview)

I want to change the gridview, but when I change the sqlcommandstring in my sqldatasource, in new view of my datagrid table sorting doesn't fire for the first click. Please see below for sqlcommandstring change process.

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack)
            {   // Restore saved sqlcommand in page refresh, please see below
                SqlDataSource1.SelectCommand = ViewState["MySQL"].ToString();
            }
        }
        catch (Exception ex){}            
    }

    protected void btn_SearchLibrary_Click(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "SELECT * FROM Books WHERE id=1 OR id=2";
        ViewState["MySQL"] = "SELECT * FROM Books WHERE id=1 OR id=2";
        //Saves sqlcommand in viewstate, to restore it in page refresh.
    }
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
HOY
  • 1,067
  • 10
  • 42
  • 85

1 Answers1

2

I believe the DataBinding of your GridView is happening before the Click event handler for your button. Therefore, your grid has already been updated from the data source before the data source itself is changed. After updating the SelectCommand in your Click event handler, you would have to explicitly call DataBind for the grid.
Also, as an aside, it looks like your try-catch in your Page_Load is just to suppress the NullReferenceException, since Page_Load also fires before the Click event handler, so ViewState["MySQL"] is always going to be null at first.

Mike Guthrie
  • 4,029
  • 2
  • 25
  • 48
  • I tried to understand but couldn't get you clearly, I added databind in click event handler, but it didn't work, – HOY Mar 05 '12 at 18:19
  • My answer may be off-base, so I think I need to ask for some clarification: Could you go through the typical usage for me? Is it as following: Page is loaded first time with grid empty; user clicks SearchLibrary button and the grid is populated; user clicks column header to sort the grid, but this first click does not process; all subsequent clicks do process and sort correctly. Is that accurate? – Mike Guthrie Mar 05 '12 at 20:52
  • This is how the process: Page is loaded for first time with grid has 3 elements inside, (because my SqlDataSource1 -> Sqlcommandstring = SELECT * from books), also sorting is working okey, after search clicked above btn_SearchLibrary_Click fires and grid is now with 2 elements, now the sorting doesn't work quite well. (PS: I used View state to keep the command string same all the time, because if I don't use it, gridview turns back to initial state with 3 elements each time something is clicked) – HOY Mar 05 '12 at 21:06
  • Creating a small sample project, I wasn't able to reproduce this issue. In the same case as you describe (adding filter to results for smaller set), the sorting is still working. Not sure where to go from here unless you want to post whole aspx and code-behind. (Remove any connection strings or other sensitive info.) – Mike Guthrie Mar 05 '12 at 21:16
  • 1
    My remark about the ViewState is that you should not use such Try-Catch blocks to suppress exceptions - rather you should find the cause of the exception and fix it. In your case, you have an exception because your first postback calls `ViewState["MySQL"]` before it is written to, thus the NullReferenceException. That is a simple fix - remove the try-catch and instead use: `SqlDataSource1.SelectCommand = ViewState["MySQL"] as string;` Hope that makes more sense. – Mike Guthrie Mar 05 '12 at 21:19
  • You may download my source code from the below address, the project is very simple one, not much code written in it , please let me know if you have problems about download https://www.wetransfer.com/dl/d166gXCd/19dc2456a6f6a52da5daa74e71d0fc4fefd4d12b18650c22cda76957e59ac5af3a0ad8c1298aa5b – HOY Mar 05 '12 at 21:47
  • Not sure if it's good news or bad, but your code worked fine for me. The only change I made was to point the SqlDataSource to a database and table accessible to me. Only thing I can do now is to promote your question, and hopefully someone else will have an idea. I recommend you edit the question to add the steps from your comment above, showing when sorting fails. – Mike Guthrie Mar 06 '12 at 13:21
  • Well I didn't tried to check it anymore, since a friend told me that filtering the gridview will be a better choice,so I solved the issue by filtering. But I am appreciated for your help. – HOY Mar 11 '12 at 20:39