Destiny
Destiny

Reputation: 63

Database access troubles

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();
             }
         }

Upvotes: 1

Views: 63

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156918

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");

Upvotes: 2

Related Questions