1

I am trying to display the list of cars that are provided by the dealer Toyota in a Gridview

I get the information from a database called Dealership.accdb The name of the table is Table1 The table has two columns dealer and car

But I keep getting this error "Additional information: No value given for one or more required parameters."

The code works fine when I use it to display the list of "dealers" But when I try to display the list of cars provided by the dealer it shows the error I mentioned above

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Web;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Data;
          using System.Data.OleDb;

          public partial class toyota : System.Web.UI.Page
          {
             protected void Page_Load(object sender, EventArgs e)
             {
                 OleDbConnection connect = new OleDbConnection();

                 connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
                 Data Source=C:\Users\Student\Documents\Visual Studio 2013\WebSites\WebSite1\Dealership.accdb";
                 connect.Open();

                 OleDbCommand cmd = new OleDbCommand("SELECT Car FROM Table1 WHERE dealer = Toyota", connect);

                 OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

                 DataTable dt = new DataTable();

                 adapter.Fill(dt);

                 GridView1.DataSource = dt;
                 GridView1.DataBind();
             }
         }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Destiny
  • 63
  • 6
  • 1
    it thinks Toyota is a field, not a string. You need to put quotes round it – BugFinder Apr 26 '16 at 08:37
  • 1
    Note: Quoting might fix your immediate problem, but should you ever replace the hard-coded "Toyota" value with a user-supplied value, [you need to use parameterized SQL](http://stackoverflow.com/q/35163361/87698). – Heinzi Apr 26 '16 at 08:39

1 Answers1

2

You should properly quote your variables:

SELECT Car FROM Table1 WHERE dealer = 'Toyota'

Instead of:

SELECT Car FROM Table1 WHERE dealer = Toyota

Now it is trying to match the field dealer with the field named Toyota, where I think you meant to check for the value Toyota.

You could parameterize your query too, so you can easily get BMW next time:

SELECT Car FROM Table1 WHERE dealer = ?

And in your C#:

OleDbCommand cmd = new OleDbCommand("SELECT Car FROM Table1 WHERE dealer = ?", connect);
cmd.Parameters.AddWithValue("?", "Toyota");
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • @Destiny - Consider marking the answer as expected and/or upvoting it, if it worked for you. – Yogi Apr 26 '16 at 08:51