Thursday, 27 June 2013

Display Data Using the DataGridview and oledbconnection

Display Data Using the DataGridview and oledbconnection

Create a New Project in VS .NET

  1. Create a new project in VS .NET by choosing File, New, and then choosing the Project option..
  2. When the New Project dialog box appears, choose  Visual C# Projects and Windows Applications. Name this project "oledbconnection1". This creates a default form for you to start from.
ADD DATA SOURCE
Go to server Explorer and right click on data Connections and clik then Add Connectin
<
choose oledb data source  for database name click the browse and give the path of database student.mdb

then  click the test connection. if text connection is succeeded click the ok button otherwise again try to connect to database.
 Adding the Button and DataGrid Controls





using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace using_oledbconn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void  showmark_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data 
Source=F:\\student.mdb");
            con.Open();
          OleDbDataAdapter ad = new OleDbDataAdapter("select * from marks", con);
          DataSet ds=new DataSet();
            ad.Fill(ds,"marks");
            ds.GetXml();
            dataGridView1.DataSource=ds.Tables[0];
      
        }
       
    }
}


No comments: