DataBinding and displaying it in a datagrid :along with Code i have written
-
Hi Friends, May be this will help you guys to help me :). I have in the form a textbox,a enter button and datagrid. I want to enter data into my DB Table named "Self" on the click of button "Enter". Till there its working fine. Now on form load am trying to display the contents in my database (for simlicity i have only one coloumn in my database:"Name") in a datagrid. The problem am facing is in writting that part of the code. Shown below is my code.Please correct me. private void Form1_Load(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(); con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\My Documents\Selftest.mdb"; OleDbCommand cmd = new OleDbCommand(); cmd.CommandText = "select * from Self"; cmd.Connection = con; OleDbDataAdapter adap = new OleDbDataAdapter(); adap.SelectCommand = cmd; DataSet ds = new DataSet(); adap.Fill(ds); } Now i will display my entire code if it might help. 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; using System.Data.Odbc; using System.IO; namespace My_self_Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(); con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\My Documents\Selftest.mdb"; con.Open(); OleDbCommand cmdObj = new OleDbCommand(); cmdObj.CommandText = "Insert into Self(Name) values ('" + txtName.Text + "')"; cmdObj.Connection = con; cmdObj.ExecuteNonQuery(); } private void txtDisplay_Click(object sender, EventArgs e) { } private void gridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void Form1_Load(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(); con.Conn
-
Hi Friends, May be this will help you guys to help me :). I have in the form a textbox,a enter button and datagrid. I want to enter data into my DB Table named "Self" on the click of button "Enter". Till there its working fine. Now on form load am trying to display the contents in my database (for simlicity i have only one coloumn in my database:"Name") in a datagrid. The problem am facing is in writting that part of the code. Shown below is my code.Please correct me. private void Form1_Load(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(); con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\My Documents\Selftest.mdb"; OleDbCommand cmd = new OleDbCommand(); cmd.CommandText = "select * from Self"; cmd.Connection = con; OleDbDataAdapter adap = new OleDbDataAdapter(); adap.SelectCommand = cmd; DataSet ds = new DataSet(); adap.Fill(ds); } Now i will display my entire code if it might help. 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; using System.Data.Odbc; using System.IO; namespace My_self_Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(); con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\My Documents\Selftest.mdb"; con.Open(); OleDbCommand cmdObj = new OleDbCommand(); cmdObj.CommandText = "Insert into Self(Name) values ('" + txtName.Text + "')"; cmdObj.Connection = con; cmdObj.ExecuteNonQuery(); } private void txtDisplay_Click(object sender, EventArgs e) { } private void gridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void Form1_Load(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(); con.Conn
I have written like this and this works: dbConnetion1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db.mdb"; dbConnetion1.Open(); oleDbCommand1.CommandText = "select * from Name"; oleDbCommand1.CommandType = CommandType.Text; oleDbCommand1.Connection = dbConnetion1; oleDbCommand1.ExecuteNonQuery(); oleDbDataAdapter1.SelectCommand = oleDbCommand1; DataTable table1 = new DataTable(); oleDbDataAdapter1.Fill(table1); dataGridView1.DataSource = table1; //or this one DataSet dataset1 = new DataSet(); oleDbDataAdapter1.Fill(dataset1, "Name"); dataGridView2.DataSource = dataset1; dataGridView2.DataMember = "Name"; dbConnetion1.Close();
Sourie
-
I have written like this and this works: dbConnetion1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db.mdb"; dbConnetion1.Open(); oleDbCommand1.CommandText = "select * from Name"; oleDbCommand1.CommandType = CommandType.Text; oleDbCommand1.Connection = dbConnetion1; oleDbCommand1.ExecuteNonQuery(); oleDbDataAdapter1.SelectCommand = oleDbCommand1; DataTable table1 = new DataTable(); oleDbDataAdapter1.Fill(table1); dataGridView1.DataSource = table1; //or this one DataSet dataset1 = new DataSet(); oleDbDataAdapter1.Fill(dataset1, "Name"); dataGridView2.DataSource = dataset1; dataGridView2.DataMember = "Name"; dbConnetion1.Close();
Sourie
Thank You Sourie. It works. Thanks a lot :)
-
Thank You Sourie. It works. Thanks a lot :)