ADO.NET: DataSet, DataGridView
-
This is my first time trying to work with a database. Let me explain what I've done real quick, then post my code, and you can tell me how I'm retarded. (Please) First, I added an existing database to the project as a new data source. I did not have it automatically generate a typed dataset. Next, in the design window, I added an untyped dataset to my form window. Then I placed a datagridview object in the window. Under the DataGridView properties, I set the datasource to my untyped dataset. I then modified my form code, as so:
public partial class PTAnalyzer : Form { // DATA ACCESS CODE public OleDbConnection conn; public OleDbDataAdapter dAdapter; public DataSet dSet; public string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\ptrack6.mdb"; public PTAnalyzer() { InitializeComponent(); // DATA ACCESS INITIALIZATION try { conn = new OleDbConnection(conString); dAdapter = new OleDbDataAdapter("SELECT * FROM game WHERE player_id = 11", conn); dSet = new DataSet(); //refreshes rows in the DataSet dAdapter.Fill(dataSetPT, "game"); } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } //try-catch } }
No errors, it runs fine. But nothing is displayed within the datagridview object. What am I doing wrong here? Thanks. -
This is my first time trying to work with a database. Let me explain what I've done real quick, then post my code, and you can tell me how I'm retarded. (Please) First, I added an existing database to the project as a new data source. I did not have it automatically generate a typed dataset. Next, in the design window, I added an untyped dataset to my form window. Then I placed a datagridview object in the window. Under the DataGridView properties, I set the datasource to my untyped dataset. I then modified my form code, as so:
public partial class PTAnalyzer : Form { // DATA ACCESS CODE public OleDbConnection conn; public OleDbDataAdapter dAdapter; public DataSet dSet; public string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\ptrack6.mdb"; public PTAnalyzer() { InitializeComponent(); // DATA ACCESS INITIALIZATION try { conn = new OleDbConnection(conString); dAdapter = new OleDbDataAdapter("SELECT * FROM game WHERE player_id = 11", conn); dSet = new DataSet(); //refreshes rows in the DataSet dAdapter.Fill(dataSetPT, "game"); } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } //try-catch } }
No errors, it runs fine. But nothing is displayed within the datagridview object. What am I doing wrong here? Thanks.budidharma wrote:
dSet = new DataSet(); //refreshes rows in the DataSet dAdapter.Fill(dataSetPT, "game");
Did you mean dSet in that last line? Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/
-
budidharma wrote:
dSet = new DataSet(); //refreshes rows in the DataSet dAdapter.Fill(dataSetPT, "game");
Did you mean dSet in that last line? Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/
Sorry, that dSet isn't actually used. I should delete that line. The data set is "dataSetPT."
-
Sorry, that dSet isn't actually used. I should delete that line. The data set is "dataSetPT."
In the code you posted, you didn't hook your adapter up to your DataGridView. Can you post that code? Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/
-
In the code you posted, you didn't hook your adapter up to your DataGridView. Can you post that code? Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/
Sure. I'm using VS 2005 rc3. The code was autogenerated by the IDE. Here's the entire two files, most was autogenerated, excluding the stuff I posted in that first post. 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 PTAnalyzer { public partial class PTAnalyzer : Form { // DATA ACCESS CODE public OleDbConnection conn; public OleDbDataAdapter dAdapter; public DataSet dSet; public string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\ptrack6.mdb"; public PTAnalyzer() { InitializeComponent(); // DATA ACCESS INITIALIZATION try { conn = new OleDbConnection(conString); dAdapter = new OleDbDataAdapter("SELECT * FROM game WHERE player_id = 11", conn); dAdapter.Fill(dataSetPT, "game"); } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } //try-catch } } } namespace PTAnalyzer { partial class PTAnalyzer { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.dataSetPT = new System.Data.DataSet(); this.dataGridViewResults = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.dataSetPT)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridViewResults)).BeginInit(); this.SuspendLayout();
-
Sure. I'm using VS 2005 rc3. The code was autogenerated by the IDE. Here's the entire two files, most was autogenerated, excluding the stuff I posted in that first post. 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 PTAnalyzer { public partial class PTAnalyzer : Form { // DATA ACCESS CODE public OleDbConnection conn; public OleDbDataAdapter dAdapter; public DataSet dSet; public string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\ptrack6.mdb"; public PTAnalyzer() { InitializeComponent(); // DATA ACCESS INITIALIZATION try { conn = new OleDbConnection(conString); dAdapter = new OleDbDataAdapter("SELECT * FROM game WHERE player_id = 11", conn); dAdapter.Fill(dataSetPT, "game"); } catch (Exception ex) { MessageBox.Show("Error : " + ex.Message); } //try-catch } } } namespace PTAnalyzer { partial class PTAnalyzer { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.dataSetPT = new System.Data.DataSet(); this.dataGridViewResults = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.dataSetPT)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridViewResults)).BeginInit(); this.SuspendLayout();
Specifically, in the second file, this is where the datagridview's datasource is set. this.dataGridViewResults.DataSource = this.dataSetPT; Really though, I'm trying to stumble through this and have no idea what I'm doing.
-
Specifically, in the second file, this is where the datagridview's datasource is set. this.dataGridViewResults.DataSource = this.dataSetPT; Really though, I'm trying to stumble through this and have no idea what I'm doing.
Finally just figured it out. I had to specifically set the datagridview datasource to a table index contained within the dataset. I was just setting it to the dataset.