SQL SELECT Command

(ASP.NET)


Hi,
In this article i discuss about how to fetch records from database by using sql select command in ado.net.

.aspx

    <asp:GridView ID="grdvw_products" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>


Code behind:-

  //executing sql query
        private void binddatatoGridview()
        {
            try
            {
                //the connectionstring which i placed in web.config
         //       <add name="adodotnetConstring" connectionString="Data Source=GIRI-ADMIN-PC\SQLEXPRESS2012;Initial Catalog=Northwnd; integrated security = true;
         //Connection Timeout=30;Min Pool Size=5;Max Pool Size=20;" providerName="System.Data.SqlClient" />

                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["adodotnetConstring"].ToString()))
                {
                    string sql = "SELECT * FROM Products";
                    using (SqlDataAdapter adptr = new SqlDataAdapter(sql, con))
                    {

                        DataTable dt = new DataTable();
                        con.Open();
                        adptr.Fill(dt);
                        con.Close();

                        grdvw_products.DataSource = dt;
                        grdvw_products.DataBind();
                    }
                }
            }
            catch (Exception)
            { }
        }

Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
7/21/2013
7/21/2013
Download PDF

Comments