title: Visual Studio Query Database from Variable DataGridView
body: <p>I have been trying to query a access database and displaying the results in a DataDrigView. The grid displays all the data when I use the "SELECT * FROM CustomerPayments". However once I try adding a Parameter to the query I get an error message by </p>
  
  <pre><code>adapter.Fill(ds); //No value given for one more required parameters
  </code></pre>
  
  <p>However I have already successfully use a query using a parameter in another part of my program, it only seems to happen when I want to display in a data grid </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  using System.Data.OleDb;
  using System.Data.SqlClient; //Import database class
  
  
  namespace WindowsFormsApplication1
  {
      public partial class frmCustomerDetails : Form
      {
          //Database
          //Get connection from database
          String connectionString = staticConnectionString.connectionString;
  
          //Declare a connection object
          OleDbConnection con;
  
          //Declare a command object for the SQL
          OleDbCommand cmd;
  
          //Declare an accedd type reader to read SQL commands
          OleDbDataReader dr;
  
          String[] CustomerID = new String[500]; 
          String[] CustomerName = new String[500];
          String[] Street = new String[500];
          String[] Area = new String[500];
          String[] PostCode = new String[500];
          String[] TelNo = new String[500];
          String[] Email = new String[500];
  
          int RecordNo = 0;
          int LastRecord = 0;
  
          public frmCustomerDetails()
          {
              InitializeComponent();
          }
  
          private void frmCustomerDetails_Load(object sender, EventArgs e)
          {
              try
              {
                  //create a connection object to the database via string location
                  con = new OleDbConnection(connectionString);
                  //Open the connection to the database
                  con.Open();
                  //Creat a new command object
                  cmd = new OleDbCommand();
                  //Set the SQL command text
                  cmd.CommandText = "SELECT * FROM Customer WHERE CustomerID = @ID;";
  
                  cmd.Parameters.AddWithValue("@ID", GlobalVar.SelectedCustomer);
  
                  //Link the command to the connection so the correct DB is used
                  cmd.Connection = con;
                  //Run the command and store resulting table in the datareader
                  dr = cmd.ExecuteReader();
              }
              catch (Exception err)
              {
                  //Any database errors jump here and output error message
                  MessageBox.Show("A database error has occurred: " + Environment.NewLine + err.Message);
              }
              finally
              {
                  //Do this whatever happens
                  //This is so the first record will be displayed
                  //Connection and dr are left open until from closes so more records can be accessed
              }
  
              //dr.Read() gets the next in the results if possible
              while (dr.Read())
              {
                  //Fill the text boxes with data
                  CustomerID[RecordNo] = dr["CustomerID"].ToString();
                  CustomerName[RecordNo] = dr["Names"].ToString();
                  Street[RecordNo] = dr["Street"].ToString();
                  Area[RecordNo] = dr["Area"].ToString();
                  PostCode[RecordNo] = dr["PostCode"].ToString();
                  TelNo[RecordNo] = dr["TelNo"].ToString();
                  Email[RecordNo] = dr["Email"].ToString();
  
                  RecordNo = RecordNo + 1;
  
              }
              LastRecord = RecordNo;
              RecordNo = 0;
              ReadDisplay();
          }
  
  
          private void btnMenu_Click(object sender, EventArgs e)
          {
              var SelectCustomer = new frmSelectCustomer();
              SelectCustomer.Show();
  
              this.Close();
          }
  
  
  
          private void btnWeeklyOrders_Click(object sender, EventArgs e)
          {
  
              String connectionString = staticConnectionString.connectionString;
              OleDbCommand command;
              OleDbDataAdapter adapter;
  
              con = new OleDbConnection(connectionString);
              command = con.CreateCommand();
  
              //create data set
              DataSet ds = new DataSet();
  
              //Clear the gride of data
              CustomerInfo.DataSource = null;
  
              //create a new dataset
              ds = new DataSet();
  
              //Open connection
              con.Open();
  
              //Run the query
              command.CommandText = "SELECT * FROM CustomerPayments WHERE CustomerID = @ID;";
              cmd.Parameters.AddWithValue("@ID", GlobalVar.SelectedCustomer);
  
  
  
              adapter = new OleDbDataAdapter(command);
              adapter.Fill(ds);
  
              //close connection
              con.Close();
  
              //Populate grid with data
              CustomerInfo.DataSource = ds.Tables[0];
  
          }
  
  
          private void ReadDisplay()
          {
  
              lblCustomerID.Text = CustomerID[RecordNo];
              lblNames.Text = CustomerName[RecordNo];
              lblStreet.Text = Street[RecordNo];
              lblArea.Text = Area[RecordNo];
              lblPostCode.Text = PostCode[RecordNo];
              lblTelNo.Text = TelNo[RecordNo];
              lblEmail.Text = Email[RecordNo];
  
          }
  
      }
  }
  </code></pre>