DatagridView problems

P

paulquinlan100

Hi

I'm pretty new to C# and have been using a guide from the internet to
populate a DataGridView with data from Access. I've been using the
following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string connString = @"Provider=Microsoft.JET.OLEDB.4.0;"
+ @"data source=C:\Programme DB\Investment
Programme_be.mdb";

conn = new OleDbConnection(connString);
command = connString.CreateCommand();

DataSet ds = new DataSet();

}

private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;

ds = new DataSet();

conn.Open;

command.CommandText = "Select * From NewSites";
adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);

conn.Close();

dataGridView1.DataSource = ds.Tables[0];
}
}
}


However, i get a number of error messages when i compile this,
including:

1. the name 'conn' does not exist in the current context.
2. the name 'command' does not exist in the current context.
3. 'string' does not contain a definition for 'CreateCommand'
4. the name 'ds' does not exist in the current context.
5. Only assignment,call,increment,decrement and new object expressions
can be used as a statement (referring to conn.Open)
6. the name 'adapter' does not exist in the current context.

Im pretty sure i followed the instruction correctly with the only
changes being to point it to my own DB.

Any ideas as to where i'm going wrong?
Thanks a lot.
Paul
 
K

Karthik D V

Hi,
you are lacking basic knowledge much!
Learn the scope of a variable and C# syntax.

For time being try the following code. (Take cout your Form_Load code)

private void button1_Click(object sender, EventArgs e)
{
string connString = @"Provider=Microsoft.JET.OLEDB.
4.0;data source=C:\Programme DB\Investment"+
"Programme_be.mdb";

dataGridView1.DataSource = null;
DataSet ds = new DataSet();

OleDbDataAdapter adapter = new OleDbDataAdapter
( "Select * From NewSites", connString);
adapter.Fill(ds);
dataGridView1.DataSource = ds;
}

Thanks
- karthik D V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top