|
-
Nov 10th, 2005, 11:36 PM
#1
Thread Starter
Frenzied Member
New to C# - don't quite "get" the errors..
OK, so I have a very simple project I'm working on, one of many to teach myself C#, as I've gotten a good bit of experience with VB.NET, and used to know C when I was an infant and blah blah.. you know the drill.
Here's my sub.. err.. "void" that's populating a dataGridView with a simple select command taken out of a textbox. I know the SQL is fine, and I know the DB Connection is fine, but When I try to set the datagridview.datasource property to the datatable, I get some error saying I need to have columns in the datagridview first. So, I am trying to extract the column names from the datareader using the getschematable method. I get an exception saying "Column 'ColumnName' already belongs to another DataTable"" I don't get it.
Code:
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection myOLEConn = new OleDbConnection();
myOLEConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\\db1.mdb;User Id=admin;Password=;";
myOLEConn.Open();
OleDbCommand myOLEcmd = new OleDbCommand();
myOLEcmd.CommandText = textBox1.Text;
myOLEcmd.Connection = myOLEConn;
OleDbDataReader myReader = myOLEcmd.ExecuteReader();
DataTable myDt = new DataTable();
dataGridView1.Columns.Clear();
int i;
for (i = 0; i < myReader.GetSchemaTable().Columns.Count-1; i++)
{
myDt.Columns.Add(myReader.GetSchemaTable().Columns[i]);
}
while (myReader.Read() == true)
{
myDt.Rows.Add(myReader);
}
dataGridView1.DataSource = myDt;
myOLEConn.Close();
}
Any pointers would be appreciated.
Bill
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|