Click here to Skip to main content
15,921,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have combo box.in that combo box all faculty details are retrieved from the database. that coding is working.

then i have one button called View.when i click the view button all the data's from the database will be displayed in the datagridview using OLE DB connection.


i am using Ms Access Database.

in that View Button.how can i write the code using OLEDB and displayed in to the Datagridview.

Note:in windows application

datagridview.databind();
Posted
Comments
[no name] 30-Jan-13 3:44am    
Note:in windows application

datagridview.databind();
the above coding line will not work.
after.databind will not come in windows apppliation

1 solution

Hi,
Below code will get the data from access database and show it to the grid. Please modify the mdb path and file name and also modify the select query.
C#
private void button1_Click(object sender, EventArgs e)
       {
           System.Data.OleDb.OleDbConnection conn = new   System.Data.OleDb.OleDbConnection();
           // TODO: Modify the connection string and include any
           // additional required properties for your database.
           conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
               @"Data source= C:\myAccessFile.mdb";
           try
           {
               conn.Open();
               // Insert code to process data.

               string sqlQuery = "select * from table";
               OleDbDataAdapter da = new OleDbDataAdapter(sqlQuery, conn);

               DataTable dtTable = new DataTable();

               da.Fill(dtTable);

               dataGridView1.DataSource = dtTable;
           }
           catch (Exception ex)
           {
               MessageBox.Show("Failed to connect to data source");
           }
           finally
           {
               conn.Close();
           }
       }


Best Regards,
Muthuraja
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900