Hi,
Go through the following code:
private void fnGetConnectedToDatabase()
{
//Connection string
string conStr="Provider=Microsoft.Jet.OLEDB.4.0;" +
" Data Source=..\\..\\studentDB.mdb";
try
{
//Instantiate OleDbConnection and open
//the connection to the database
myConn = new OleDbConnection(conStr);
myConn.Open();
}
catch(OleDbException ex) {
//get the error message if connection failed
MessageBox.Show("Error in connection ..."+ex.Message);
}
string sqlStr ="SELECT * FROM studentTable;";
//Instantiate a DataAdapter by passing the sqlStr and myConn.
//now data is in raw form
dAdapter = new OleDbDataAdapter(sqlStr,myConn);
//Instantiate a DataSet
dset = new DataSet();
//Gets a collection that provides the master mapping
// between a source table and a DataTable
dAdapter.TableMappings.Add("Table", "studentTable");
//A data adapter object utilizes the Fill method
//to populate a DataSet or a DataTable object with
//data retrieved by a SELECT command.
dAdapter.Fill(dset);
//When binding a DataSet, .NET automatically uses the corresponding
//DataViewManager provided through the DataSet.DefaultViewManager property
this.dviewmanager=dset.DefaultViewManager;
this.comboBox1.DataSource=this.dviewmanager;
//display "studentTable.StudentID" in the ComboBox
this.comboBox1.DisplayMember="studentTable.StudentID";
//DataBinding for the TextBox controls
this.textBox1.DataBindings.Add("Text",
this.dviewmanager,"studentTable.StudentID");
this.textBox2.DataBindings.Add("Text",
this.dviewmanager, "studentTable.StudentSubject");
this.textBox3.DataBindings.Add("Text",
this.dviewmanager, "studentTable.StudentName");
// Close the connection to the database.
this.myConn.Close();
}
Mark as answer if satisfied....
Regards,
Shree M.
Kavya Shree Mandapalli
Jopito, if this helps please login to Mark As Answer. | Alert Moderator