Tom McNish
Tom McNish

Reputation: 95

Display data in datagridview table with select statement

I want to know how to populate a DataGridView with information found from a Select statement. Here's my event for the select button:

private void SelectCust(object sender, EventArgs e)
{
    OleDbConnection conn = null;
    conn = new OleDbConnection(
    "Provider=Microsoft.ACE.OLEDB.12.0; " + "Data Source=" + "c:/inetpub/ftproot/ora103/App_Data/Fashion.accdb");
    conn.Open();
    OleDbCommand cmd = new OleDbCommand("Select * from Customer where CustNum = @MyCust ", conn);
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    adapter.SelectCommand = cmd;
    DataTable dt = new DataTable();
    adapter.Fill(dt);
    dataGridView1.DataSource = dt;
    dataGridView1.DataBind();

    if (dt.Rows.Count > 0)
    {

    }

    conn.Close();
}

I want to use the "if" statement to insert information I've gathered from my Customers table, and populate a DataGridView. However, I'm having trouble connecting the DataGridView to my Microsoft Access Database (called Fashion.accdb). Here's what my DataGridView looks like as of now (I know it's not much):

<asp:GridView ID="dataGridView1" runat="server" EnableModelValidation="True">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>

EDIT

Here's the error :

Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.

The idea is that I'll be typing in a customer number into a textbox. Then I'll hit the submit button. Then the event will retrieve that number from the textbox that was typed in, grab the information in the database pertaining to that customer, and display it in a GridView

Upvotes: 0

Views: 1883

Answers (1)

wazz
wazz

Reputation: 5078

oledb parameters are different than sql server's.

...where CustNum = @MyCust

needs to be

...where CustNum = ? (or: where CustNum = [?])

after setting the adapter's command, set the parameter(s):

cmd.Parameters.Add("@CustNum", OleDbType.Integer, 15).Value = yourValue;

hope that helps.

Upvotes: 1

Related Questions