[Solved] How to return a string from a method?


Your return type is void, not string. Change it to:

private string SQLSelection()

A void is a “returnless” action that is performed upon something and does not “return” back the values of the return statement. If you try to return a value, you get a compiler error as you are experiencing.

In addition, you keep redeclaring

String SQL

inside each method. Simply assign the value to SQL by removing the String declaration in each if statement.

Finally, your return statement is once again trying to redeclare the variable.

Your resulting method should look like:

private string SQLSelection()
{
    String SQL = null;

    if (comboBox1.Text == "All" & comboBox2.Text == "All")
    {
        SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status in ('New','Hold')";
    }
    else if (comboBox1.Text == "All" & comboBox2.Text == "@status")
    {
        SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status = @status";
    }
    else if (comboBox1.Text == "@username" & comboBox2.Text == "All")
    {
         SQL = "SELECT stationID, LocationName, plandate, username, status FROM dbo.joblist WHERE status = @username";
    }
    else
    {
        SQL = "SELECT stationID, LocationName, plandate, username, status  FROM dbo.joblist WHERE username = @username and status = @status";
    }

    return SQL;
}

0

solved How to return a string from a method?