0

I have a grid and a dropdownlist from where the user select the query to fill in the grid. As it is now it works fine:

protected void Button1_Click(object sender, EventArgs e)
    {
        string selected = DropDownList1.SelectedItem.Value;
        if (selected == "0")
        {
            Label1.Text = "You shall select a query!";
        }
        else
        {
            int x = Int32.Parse(selected);
            string query = "";
            switch (x)
            {
                case 1:     //Top 100 TB 321
                    query = @"...something...";
                    SqlDataSource1.SelectCommand = query;
                    break;

                case 2:
                    query = @"....something else....";
                    SqlDataSource1.SelectCommand = query;
                    break;

                case 3:     
               .......
            }
        }

And in the markup I simply have the sqldatsource with the connection string:

 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
         ConnectionString="<%$ ConnectionStrings:BOMConnectionString %>" 
         ></asp:SqlDataSource>

With the above code I am unable to export the content of the grid because it generate an error "Object reference not set to an instance of an object". To avoid this issue, instead of defining the select command from code behind, I should pass the query as variable to the sqldatasource in the markup. How can I pass the variable "query" to the sqldatasource SelectCommand in the markup?

4
  • Do you know exactly where the Null reference exception ("... not set to an instance..." error) is coming from? I can see a few places that could generate one. For example, you're assuming that DropDownList1 always has a SelectedItem. In any case, there's no client-side SqlDataSource. It's a purely server-side thing, and with good reason; if client code could access your database, anybody loading your web app would have access to your database credentials and could log in and mess with stuff. Commented Aug 9, 2014 at 7:03
  • The method I am using to export is taking the data from the sqldatasource, so at time I click to export there is no select command there. This is why I would ike to pass the variable to the SelectCommand of Sqldatsource in the markup. Sorry for the mistake in my question, I will update it now. Commented Aug 9, 2014 at 7:04
  • Is the "Export" button clicked by the web app user? If so, why not have the handler for it check the DropDownList selected value and set the SelectCommand, just like you're doing in the code above? Commented Aug 9, 2014 at 7:12
  • Thats sound a good idea. Thank you. Commented Aug 9, 2014 at 7:17

1 Answer 1

0

If the "export" feature is also a callback from the web application, you should be able to see the state of DropDownList1 in the callback. Use that to set the SelectCommand as in your code above. (I recommend moving the duplicated code into its own helper function so that if you call it from multiple places you don't have to change them all if you edit the functionality later).

Alternatively, but the SqlDataSource in session storage on the server, rather than pretending it's part of the web page. Then when you set its SelectCommand, that value will be remembered across callbacks.

Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to use a session variable "query" instead of setting the SelectCommand from code behind and then use such variable in the sqlparameters and set the SelectCommand="@query". But now a new error is rised: "The name 'myquerytext' is not a valid identifier!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.