0

I'm trying to fetch a query from the database and I've done the following:

public DataTable FetchTasks(string questName, string categoryName)
        {
            const string queryString = @"   SELECT  *
                                            FROM    dbo.Task as t
                                                    INNER JOIN dbo.[Quest] as q
                                                        on q.QuestID = t.QuestID
                                                    INNER JOIN dbo.[Category] as c
                                                        on c.CategoryID = t.CategoryID
                                            WHERE   q.[Description] = @questName
                                                    AND c.CategoryTitle = @categoryName";

            using (var dataAdapter = new SqlDataAdapter(queryString, ConnectionString))
            {
                using (var cmd = new SqlCommandBuilder(dataAdapter))
                {
                    cmd.Parameters.AddWithValue("@questName", questName);
                    cmd.Parameters.AddWithValue("@categoryName", categoryName);

                    var table = new DataTable { Locale = System.Globalization.CultureInfo.InvariantCulture };

                    dataAdapter.Fill(table);

                    return table;
                }
            }

However I'm receiving an error on the following lines:

         cmd.Parameters.AddWithValue("@questName", questName);
         cmd.Parameters.AddWithValue("@categoryName", categoryName);

That reads:

Cannot resolve symbol 'Parameters'

Where have I gone wrong here? I know the mistake is in where I've added the parameters - but I don't understand enough to know how to correct it.

2
  • Its a guess can you add adapter.SelectCommand = command; after using (var dataAdapter = new SqlDataAdapter(queryString, ConnectionString))? Commented Jul 2, 2014 at 1:47
  • cmd is an instance of a SqlCommandBuilder not a SqlCommand. Commented Jul 2, 2014 at 1:52

1 Answer 1

1

The Parameters collection isn't accessible directly from SqlCommandBuilder.

Try replacing the two lines in question with these:

cmd.DataAdapter.SelectCommand.Parameters.AddWithValue("@questName", questName);
cmd.DataAdapter.SelectCommand.Parameters.AddWithValue("@categoryName", categoryName);
Sign up to request clarification or add additional context in comments.

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.