0

I'm having trouble with this method. It returns empty string, what is wrong with this ?

I have this method:

public static string GetData(string Table1, string Column1, string WhereColumn, string WhereValue)
{
    Table1 = Methods.cleaninjection(Table1); // Some injection method that cleans the string

    SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["connection"].ConnectionString);

    SqlCommand command = new SqlCommand("SELECT " + "@Column1" + " FROM " + Table1 + " WHERE " + "@WhereColumn" + " = " + "@WhereValue", connection);
    command.Parameters.AddWithValue("Column1", Column1);
    command.Parameters.AddWithValue("WhereColumn", WhereColumn);
    command.Parameters.AddWithValue("WhereValue", WhereValue);

    try
    {
        if ((connection.State == ConnectionState.Closed) || (connection.State == ConnectionState.Broken))
        {
            connection.Open();
        }
        string veri = Convert.ToString(command.ExecuteScalar());
        return veri;
    }
    finally
    {
        connection.Close();
    }
}

When I run this, the command string looks like this:

SELECT @Column1 FROM Table1 WHERE @WhereColumn = @WhereValue

It looks like correct but I couldn't find what is wrong.
Any ideas?

4
  • Try: "SELECT " + Column1 + " FROM "... Commented Aug 6, 2015 at 0:50
  • Tried but not worked, thanks. Commented Aug 6, 2015 at 0:59
  • 1
    Also, the @WhereColumn. You can't use parameters as column or object names. Commented Aug 6, 2015 at 1:02
  • Now worked, thank you so much @Dan Guzman Commented Aug 6, 2015 at 1:19

1 Answer 1

2

As commented, you cannot parameterize your column names and table names. Instead, do string concatenation:

"SELECT " + Column1 + " FROM " + Table1 + " WHERE " + WhereColumn + " =  @WhereValue";

Here is how your code should be:

public static string GetData(string Table1, string Column1, string WhereColumn, string WhereValue)
{
    Table1 = Methods.cleaninjection(Table1); // My injection method that cleans the string

    string sql = "SELECT " + Column1 + " FROM " + Table1 + " WHERE " + @WhereColumn + " =  @WhereValue";

    using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["connection"].ConnectionString))
    {
        using (SqlCommand command = new SqlCommand(sql, connection))
        {
            command.Parameters.Add("@WhereValue", SqlDbType.VarChar, 50).Value = WhereValue;

            connection.Open();

            string veri = Convert.ToString(command.ExecuteScalar());
            return veri;
        }
    }
}

Notes:

  1. Please do not use AddWithValue. Use Parameters.Add() instead. According to this article:

There is a problem with the AddWithValue() function: it has to infer the database type for your query parameter. Here’s the thing: sometimes it gets it wrong.

  1. Wrap your object in Using to ensure proper cleanup of resources.
  2. For additional security purposes, you can wrap your column name and table name in square brackets [].
Sign up to request clarification or add additional context in comments.

9 Comments

Okey thank you, but is this secure? And do you mean in your code like this? : WHERE + WhereColumn + " = @WhereValue";
Also make sure to cleanInjection pretty much everything except @WhereValue which is the only actual parameter. And you want to be really sure that cleanInjection is up to the task. All in all I think the approach by @mrbengi is inherently flawed.
For additional security purposes, wrap your table names and columns name with [].
I think in terms of security, both methods are the same. The difference is that AddWithValue() has to infer the DB type, which sometimes is wrong and cause performance issues.
The good thing with Add is you can specify the SQL datatype, unlike with AddWithValue. Say, you have a VARCHAR column, and AddWithValue sends your parameter as NVARCHAR. The DB will not convert the param into VARCHAR as it may lead to data loss. Instead, the column will be converted into NVARCHAR, which makes your WHERE clause non-SARGABLE.
|

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.