简体   繁体   中英

which of following code is better performance wise in ASP.NET 2.0

I have been using a datatable to display some fields from SQL 2005 database table in my website which is built in ASP.NET 2.0.

lately its been throwing timeout errors saying all the max connection pools have been reached.

here is my code using the SqlDataAdapter performing this on a Repeater OnDataItemBound.

#region repeater item databound
    protected void rProducts_OnItemDataBound(object source, RepeaterItemEventArgs e)
    {

        // displaying sale and original price //
        Label lblitemid = e.Item.FindControl("itemID") as Label;
        decimal strOP;
        decimal strSP;
        string salequery = "select SaleItemNo, OriginalPrice, SalePrice, OriginalPriceRange, SalePriceRange from SaleItems where CatalogItemId='" + lblitemid.Text + "'";
        SqlConnection myconnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString);
        SqlCommand SqlCmd1 = null;
        SqlCmd1 = new SqlCommand(salequery, myconnection1);
        SqlDataAdapter ad1 = new SqlDataAdapter(SqlCmd1);
        DataTable dt1 = new DataTable();
        ad1.Fill(dt1);

        Label lbloriprice = e.Item.FindControl("originalprice") as Label;
        if (dt1.Rows[0]["OriginalPrice"].ToString() == "" || dt1.Rows[0]["OriginalPrice"].ToString() == "0.00")
        {
            lbloriprice.Attributes.Add("style", "display:none");
        }
        else
        {
            strOP = Convert.ToDecimal(dt1.Rows[0]["OriginalPrice"].ToString());
            lbloriprice.Text = strOP.ToString("c");
        }

        Label lbloprange = e.Item.FindControl("opRange") as Label;
        if (dt1.Rows[0]["OriginalPriceRange"].ToString() != "")
        {
            lbloprange.Text = dt1.Rows[0]["OriginalPriceRange"].ToString();
            lbloprange.Visible = true;
            lbloriprice.Visible = false;
        }

        Label lblsaleprice = e.Item.FindControl("saleprice") as Label;
        if (dt1.Rows[0]["SalePrice"].ToString() == "" || dt1.Rows[0]["SalePrice"].ToString() == "0.00")
        {
            lblsaleprice.Attributes.Add("style", "display:none");
        }
        else if (dt1.Rows[0]["SalePriceRange"].ToString() != "")
        {
            lblsaleprice.Text = "Special <br />" + dt1.Rows[0]["SalePriceRange"].ToString();
        }
        else
        {
            strSP = Convert.ToDecimal(dt1.Rows[0]["SalePrice"].ToString());
            lblsaleprice.Text = "Special <br />" + strSP.ToString("c");
        }

    }

    #endregion

It's throwing errors on ad1.fill(dt1)

I redesigned my code and was wondering if this will reduce or remove the errors. Please advice

   #region repeater item databound
    protected void rProducts_OnItemDataBound(object source, RepeaterItemEventArgs e)
    {

        // displaying sale and original price //
        Label lblitemid = e.Item.FindControl("itemID") as Label;

        SqlDataSource SqlDataSource2 = new SqlDataSource();
        SqlDataSource2.ID = "SqlDataSource2";
        SqlDataSource2.ConnectionString = ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString;
        SqlDataSource2.SelectCommand = "select SaleItemNo, OriginalPrice, SalePrice, OriginalPriceRange, SalePriceRange from SaleItems where CatalogItemId='" + lblitemid.Text + "'";

        Repeater rep = e.Item.FindControl("rpt2") as Repeater;
        rep.DataSource = SqlDataSource2;
        rep.DataBind();

     }
#endregion

Your SqlCommand , SqlConnection , and SqlDataAdapter all need to be in using blocks. It's possible that your really are running out of connections because you're not closing them in a timely manner.


Example of using blocks:

using (SqlConnection myconnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBconnection"].ConnectionString))
{
    using (SqlCommand sqlCmd1 = new SqlCommand(salequery, myconnection1))
    {
        using (SqlDataAdapter ad1 = new SqlDataAdapter(sqlCmd1))
        {
            dt1 = new DataTable();
            ad1.Fill(dt1);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM