Active Query Builder support area

How to get and set SQL text in ASP.NET Edition using ASPX View engine

Last modified:


This article provides samples of saving and loading the query between work sessions and describes how to handle SQL query text updates programmatically using Active Query Builder ASP.NET Edition with ASPX view engine.

Saving query

  • To retrieve the query text from the query builder control, use the following code:
    protected void btnGetQuery_Click(object sender, System.EventArgs e)
    {
        // First variant:
    
        // 1a) if your code is at the page that contains the QueryBuilderUC User Control:
        QueryBuilderControl control = (QueryBuilderControl) QueryBuilderUC1.FindControl("QueryBuilderControl1");
        QueryBuilder queryBuilder = control.QueryBuilder;
        PlainTextSQLBuilder sqlBuilder = control.PlainTextSQLBuilder;
    
        // 1b) if your code is inside the QueryBuilderUC User Control:
        QueryBuilder queryBuilder = QueryBuilderControl1.QueryBuilder;
        PlainTextSQLBuilder sqlBuilder = QueryBuilderControl1.PlainTextSQLBuilder;
    
        // Second variant: globally available static helpers
        QueryBuilder queryBuilder = SessionStore.Current.QueryBuilder;
        PlainTextSQLBuilder sqlBuilder = SessionStore.Current.PlainTextSQLBuilder;
    
        // Common part:
        string plainSQL = queryBuilder.SQL; // unformatted text
        string formattedSQL = sqlBuilder.SQL; // formatted text
    }
  • To retrieve the query text from the text editor, use the following code:
    protected void btnGetQuery_Click(object sender, System.EventArgs e)
    {
        // if your code is at the page that contains the QueryBuilderUC User Control:
        SQLEditor editor = (SQLEditor) QueryBuilderUC1.FindControl("SQLEditor1");
        string textSQL1 = editor.SQL;
    
        // if your code is inside the QueryBuilderUC User Control:
        string textSQL2 = SQLEditor1.SQL;
    }

    If you want to parse the final modifications made to SQL text by the end-user before the query execution, additionally use the following code:

        try
        {
            queryBuilder.SQL = editor.SQL;
    
            // execute the query here
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Parsing error");
        }

Loading query

  • To assign SQL query text to the control use the following code:
    protected void btnSetQuery_Click(object sender, System.EventArgs e)
    {
        // if your code is at the page that contains the QueryBuilderUC User Control:
        SQLEditor editor = (SQLEditor) QueryBuilderUC1.FindControl("SQLEditor1");
        editor.SQL = "SELECT * FROM ...";
    
        // if your code is inside the QueryBuilderUC User Control:
        SQLEditor1.SQL = "SELECT * FROM ...";
    }
    If you want to do this without reloading a web page, additionally use the following JavaScript code:
    QB.Web.Application.fullUpdate();
  • To update the SQL text on the client use the following JavaScript code:
    QB.Web.Application.importSQL('Select * From ...');
    QB.Web.Application.refreshSql();

To save and load the query along with the layout of objects on Design Pane, use the QueryBuilderControl.Layout or SessionStore.Current.Layout property.


Is this article helpful for you?