Search code examples
asp.netsqlparameterized

ASP.NET C# Parameterized Query Returning Nothing


Okay I am a noob to parameterized queries. I understand why you should use them and all but I cannot find any resource that shows the correct way or at least one that shows the correct way that actually works.

So my question is about whether or not my code is right. It compiles and runs just fine but it returns absolutely nothing in the gridview.

 protected void SearchButton_Click(object sender, EventArgs e)
{
    string searchBoxValue = SearchBox.Text;
    string columnNameValue = ColumnName.SelectedValue;
    columnNameValue.ToLower();

    SqlCommand searchCommand = new SqlCommand();
    searchCommand.Connection = connection;
    searchCommand.CommandText = "select firstname AS FirstName,lastname AS LastName, zipcode as ZipCode, phone AS Phone, email AS Email, cancersurvivor AS CancerSurvivor, ethnicity AS Ethnicity from registrants where @columnname = @searchterm";

    SqlParameter columnParam = new SqlParameter();
    columnParam.ParameterName = "@columnname";
    columnParam.Value = columnNameValue;

    SqlParameter searchBoxParam = new SqlParameter();
    searchBoxParam.ParameterName = "@searchterm";
    searchBoxParam.Value = searchBoxValue;

    searchCommand.Parameters.Add(columnParam);
    searchCommand.Parameters.Add(searchBoxParam);

    UpdateTable(searchCommand);

}

The UpdateTable function takes in the an SqlCommand object and then uses a DataAdapter object to execute the command and fills a DataTable object then sets the gridview datasource to the datatable object and binds it.

Like I said before I am really looking for the proper way to do this? do I need a stored procedure in order to do this? I am confused by all this and why it is not working.


Solution

  • You cannot parameterise @columnname. This needs to be a literal in your query.

    Your statement

    select 
     /* .... */
    from registrants where @columnname = @searchterm
    

    will return all rows from registrants if the value of the parameters happens to be the same or no rows otherwise.

    It will not look and see if you have a column of that name and see if @searchterm exists in it.

    To do this in a safe way you would need to check that columnNameValue matches one of a whitelist of valid column names (as you must know the possible column names in that table) and concatenate it into your query. Do not concatenate unvalidated user input. as then you open yourself up to SQL injection.

    So you might implement it something like

    using System.Linq;
    
    protected void SearchButton_Click(object sender, EventArgs e)
    {
        string columnNameValue = ColumnName.SelectedValue.ToLower();
    
        var validColumnNames = new string[] { "firstname", "lastname", "zipcode" };
    
        if (!validColumnNames.Contains(columnNameValue))
        {
            throw new Exception("Unexpected column name " + columnNameValue);
        }
    
        /* ... code omitted */
    
        searchCommand.CommandText = "select firstname AS FirstName,lastname AS LastName, zipcode as ZipCode, phone AS Phone, email AS Email, cancersurvivor AS CancerSurvivor, ethnicity AS Ethnicity from registrants where " + columnNameValue + " = @searchterm";
    
        /* ... code omitted */
    }