Function for Fetching Data from Database and Showing in DataGridView

// function for fetching data from database and showing in datagridview in window application and make a function

    public void displayDataGridView()
   {

        SqlConnection con = new SqlConnection("server = MUNESH\\SQL2008R2;Database=data;UID=sa;Password=123;");

        SqlCommand cmd = new SqlCommand("Select * from data1", con);

        try

        {

            con.Open();

            SqlDataAdapter da = new SqlDataAdapter();

            da.SelectCommand = cmd;

            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            con.Close();

        }

        catch (Exception ec)

        {

            MessageBox.Show(ec.Message);

        }

}
Ebook Download
View all
Learn
View all