Tech Junkie Blog - Real World Tutorials, Happy Coding!: ADO.NET: Using System.Data.SqlClient.SqlDataAdapter To Fill System.Data.DataTable

Sunday, June 19, 2016

ADO.NET: Using System.Data.SqlClient.SqlDataAdapter To Fill System.Data.DataTable

In this example we will use the SqlDataAdapter to fill a DataTable then bind the data to a GridView

 1. Drag a GridView into your ASP.NET page

2. Use the following code to the fill the dtProducts DataTable with the adapter SqlDataAdapter, then bind it to the GridView1 control.


        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtProducts = new DataTable();

              string connString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].
ConnectionString;

              using (SqlConnection conn = new SqlConnection(connString))
                {
                    SqlCommand cmd = new SqlCommand("SELECT * FROM Products", conn);
                    conn.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dtProducts);

                    GridView1.DataSource = dtProducts;
                    GridView1.DataBind();

                }
        }


Next: SqlDbType.Date

3 comments:

  1. Thanks for sharing informative content…Find best digital marketing company to know more and Contact us on given no.…

    ReplyDelete
  2. Thanks for sharing this.,
    Leanpitch provides online training in A-CSPO during this lockdown period everyone can use it wisely.
    Join Leanpitch 2 Days A-CSPO Certification Workshop in different cities.

    a-cspo certification
    a-cspo training

    ReplyDelete

  3. Thanks for sharing this.,
    Leanpitch provides crash course in Brain hacks using NLP everyone can use it wisely.

    Brain hacks with NLP

    NLP training

    ReplyDelete

Search This Blog