0

I want to fill a GridView with data from a database and let the user choose what part of the "Cursussen" Table is shown. He can do this by changing the selection of the parameter in a dropdownbox.

Error : No overload for method "Add" takes 2 arguments.

protected void Page_Load(object sender, EventArgs e)
{
    ConnectionStringSettings planner =
        ConfigurationManager.ConnectionStrings["CursusDatabase"];
    DbConnection connection = new SqlConnection(planner.ConnectionString);
    DbCommand cmd = connection.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText =
        "SELECT * FROM Cursussen " +
        "WHERE CursusId = @CursusId";
    cmd.Parameters.Add("CursusId", DropDownList1.SelectedValue); // <-- here

    connection.Open();
    DbDataReader rdr = cmd.ExecuteReader();
    DataTable planning = new DataTable();
    planning.Load(rdr);
    connection.Close();

    GridView1.DataSource = planning;
    GridView1.DataBind();
    }
}

3 Answers 3

5

EDIT:

As specified by Oded in the comments, I missed that you are using DbCommand

var parameter = cmd.CreateParameter();
parameter.ParameterName = "CursusId";
parameter.Value = DropDownList1.SelectedValue;
cmd.Parameters.Add(parameter);
Sign up to request clarification or add additional context in comments.

4 Comments

Which will not work, as OP is using DbConnection and DbCommand and not SqlConnection and SqlCommand.
Should i use Sqlconnection and command instead of dbconnection?
Okey will try get back to you in a sec
Got it working with the SqlConnection and the DbConnection. The reason why it took so long was because i had ORDER BY before WHERE :p
0

You are using DbCommand - if you change this to be SqlCommand, you can do the following:

cmd.Parameters.AddWithValue("CursusId", DropDownList1.SelectedValue);

Otherwise, you need to use the Add method that takes a DbParameter:

cmd.Parameters.Add(new DbParameter { ParameterName = "CursusId", 
                                     Value = DropDownList1.SelectedValue});

Comments

0
Dim myParamList As New List(Of SqlParameter)()
Dim myParam As SqlParameter = Nothing

myParam = New SqlParameter("@X", SqlDbType.Int)
myParam.Value = Y
myParamList.Add(myParam)

Then add the parameter list to the command.

Comments

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.