0

For the code:

sqlParameter = parameterSettings.GetParameters<T>(table, config);

I'm receiving the error:

Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Data.SqlClient.SqlParameter' C:\Users\jonathan.cameron\Documents\Visual Studio 2013\Projects\DataGenerator\DataGenerator\Methods\Query.cs 99 28 DataGenerator

The method signature for GetParameters:

public List<SqlParameter> GetParameters<T>(T table, Config config)

The rest of the code

public int RunInsertQuery<T>(bool returnPrimary, SqlCommand cmd, Config config, 
             ParameterSettings parameterSettings, SqlParameter sqlParameter, 
                T table)
{
        // Used to create an INSERT Query
    Query query = new Query();

        // Pass item that needs to be inserted into SQL
    cmd.CommandText = query.InsertQuery<T>(table, config);
        // If the primary key needs to be returned...
    if(returnPrimary == true)
    {
        cmd.CommandText += "SELECT CAST(scope_identity() AS int)";
    }            
        // Obtain column names/data types/values into the SqlParameter object
    sqlParameter = parameterSettings.GetParameters<T>(table, config);

        // Check for null in Sql Parameter values, apply DBNull.Value, return parameters to
        // SqlCommand instance
    cmd.Parameters.AddRange(parameterSettings.NullValueCheck(sqlParameter).ToArray());

        // if returnPrimary is truen, return the primary key while executing 
        // the sql parameter
    if (returnPrimary)
    {
        return (int)cmd.ExecuteScalar();
    }
    else
        cmd.ExecuteNonQuery();
    return 1;            
}

1 Answer 1

1

The GetParameters method returns a List and thus cannot be assigned to an SqlParameter field. This field needs to be of type List<SqlParameter> or you need to select one value from the return value of the GetParameters method.

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

4 Comments

Ah, blind on that. SqlParameter was a List<>, whoops! Thank you! I would +1, but I do not have +15 rep. Sorry. Answer: public int RunInsertQuery(bool returnPrimary, SqlCommand cmd, Config config, ParameterSettings parameterSettings, List sqlParameter, T table)
@jcameron47 No problem. I guess you'll change the 'List' to the generic version 'List<SqlParameter>' aswell, right? :)
Yup, keeping em all in Generic city :) Much appreciated.
@jcameron47 if the answer is OK. You can accept it as answer.

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.