Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Tuesday, February 28, 2012

Underlying Connection was Closed Error in SharePoint 2010 SSL/TLS


Symptoms:-
                When you call a Microsoft ASP.NET Web service from an ASP.NET application, you may intermittently receive the following error message:
The underlying connection was closed: An unexpected error occurred on a send.
The call stack is similar to the following:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
 At System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request) at
System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request) at
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) at
...

Resolution:-
                supported hotfix is now available from Microsoft. However, it is intended to correct only the problem that is described in this article. Apply it only to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next Microsoft .NET Framework service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Customer Support Services to obtain the hotfix. For a complete list of Microsoft Customer Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:
Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time item in Control Panel.
   Date         Time   Version       Size       File name                    Platform
   ----------------------------------------------------------------------------------
   01-May-2003  12:48  1.0.3705.426     20,480  Perfcounter.dll              X86
   01-May-2003  12:48  1.0.3705.426  1,175,552  System.dll                   X86
   01-May-2003  12:48  1.0.3705.426    311,296  System.runtime.remoting.dll  X86
   01-May-2003  12:48  1.0.3705.426    503,808  System.web.services.dll      X86   
This hotfix is included in the .NET Framework 1.0 Service Pack 3 and in the .NET Framework 1.1 Service Pack 1.

For more information about how to obtain the latest .NET Framework 1.0 service pack, click the following article number to view the article in the Microsoft Knowledge Base:
318836  How to obtain the latest .NET Framework 1.0 service pack

Status:-
                Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.
More Information:-
                The error message that is mentioned in the "Symptoms" section may also occur for valid reasons. For example, the underlying connection may have been closed for reasons that are beyond the control of the client. In such circumstances, the Microsoft .NET Framework is behaving correctly.

For example, if the Web services that are called are hosted in a software or in a hardware load-balanced environment that contains servers that support HTTP 1.1 and keep-alive connections, the connection may have ended by the time that the client tries to reuse the connection. If this error still occurs after you apply this fix, try the following solutions:
  • Configure the Web site that hosts the Web services not to use keep-alive connections. To do this, follow these steps:
    1. Open the Microsoft Internet Information Server (IIS) Management Console and locate the Web server.
    2. Click the Web Site tab of the Properties page for the Web site.
    3. Click to clear the HTTP Keep-Alives Enabled check box.
  • Verify that your load-balancing hardware or software is correctly configured for your application requirements.
  • If you do not have control over the server environment that hosts the Web services that you are calling, you may have to disable keep-alive connections on the client side. To do this, follow these steps:
    1. Set the KeepAlive property of the HttpWebRequest class to false.
    2. Override the HttpWebRequest method in the Reference.cs file that is generated by Microsoft Visual Studio .NET for the Web client proxy. The following code overrides the HttpWebRequest method.
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);
 webRequest.KeepAlive = false;
 return webRequest;
}
References:-
                For more information about how to obtain the .NET Framework 1.1 Service Pack 1, click the following article number to view the article in the Microsoft Knowledge Base:
885055  How to obtain Microsoft .NET Framework 1.1 Service Pack 1

How to find Controls in GridView with Find Control Object while using Template Field

 if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label b2 = (Label)e.Row.FindControl("lblBody");
                    if (b2.Text.Length > 50)
                    {
                        b2.Text = b2.Text.Substring(0, 30);
                        b2.Text = b2.Text + "......";
                    }

                }

Managed Client Object Model Basics

Binding Data from SharePoint List to SpGridView using Client Object Model



using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Client;
using System.Linq;
using System.Data;
namespace PrasadVWebPart.PrasadVWebPart
{
    [ToolboxItemAttribute(false)]
    public class PrasadVWebPart : WebPart
    {
        string strError = string.Empty;
        SPGridView sgvTasks;
        DataSet dSet = new DataSet();
        DataTable dt = new DataTable();
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(strError);
            try
            {
                writer.Write("<Style type='Text/Css'>");
                writer.Write(".GridClass");
                writer.Write("{");
                writer.Write("text-Align:Left;font-Weight:bold;");
                writer.Write("}");
                writer.Write("</Style>");
                sgvTasks.RenderControl(writer);
            }
            catch (Exception ex)
            {

                writer.Write(ex.ToString());
            }
        }
        protected override void CreateChildControls()
        {
            try
            {
                sgvTasks = new SPGridView();
                sgvTasks.ID = "sgvTasks";
                sgvTasks.AutoGenerateColumns = false;
                this.Controls.Add(sgvTasks);

                SPWeb currentWeb = SPContext.Current.Web;
                HyperLinkField hTaskName = new HyperLinkField();
                hTaskName.HeaderStyle.CssClass = "GridClass";
                hTaskName.HeaderText = "Task Name";
                hTaskName.DataNavigateUrlFields = new String[] { "ID" };
                hTaskName.DataTextField = "Title";
                hTaskName.DataNavigateUrlFormatString = currentWeb.Url + "/Lists/Tasks/DispForm.aspx?ID={0}&Source=" + Page.Request.Url;
                sgvTasks.Columns.Add(hTaskName);

                BoundField bStatus = new BoundField();
                bStatus.HeaderStyle.CssClass = "GridClass";
                bStatus.HeaderText = "Status";
                bStatus.DataField = "Status";
                sgvTasks.Columns.Add(bStatus);

                BoundField bPriority = new BoundField();
                bPriority.HeaderStyle.CssClass = "GridClass";
                bPriority.HeaderText = "Priority";
                bPriority.DataField = "Priority";
                sgvTasks.Columns.Add(bPriority);

                BoundField bCreatedDate = new BoundField();
                bCreatedDate.HeaderStyle.CssClass = "GridClass";
                bCreatedDate.HeaderText = "Created Date";
                bCreatedDate.DataField = "Created";
                sgvTasks.Columns.Add(bCreatedDate);
                sgvTasks.DataSource = getTasksData();
                sgvTasks.DataBind();

               
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
        public DataSet getTasksData()
        {
            try
            {
                dt.Columns.Add("Title");
                dt.Columns.Add("Status");
                dt.Columns.Add("Priority");
                dt.Columns.Add("Created");
                dt.Columns.Add("ID");
                ClientContext context = new ClientContext(SPContext.Current.Web.Url);
                using (context)
                {
                    Web web = context.Web;
                    List list = web.Lists.GetByTitle("Tasks");
                    CamlQuery query = new CamlQuery();
                    query.ViewXml = @"<View><Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy></Query></View>";
                    Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(query);
                    context.Load(items);
                    context.ExecuteQuery();
                    foreach (var item in items)
                    {
                        string strTitle = string.Empty;
                        string strStatus = string.Empty;
                        string strPriority = string.Empty;
                        string strID = string.Empty;
                        string strCreatedDate = string.Empty;
                        if (item["Title"].ToString() != "")
                        {
                            strTitle = item["Title"].ToString();
                        }
                        else
                        {
                            strTitle = "--";
                        }
                        if (item["Status"].ToString() != "")
                        {
                            strStatus = item["Status"].ToString();
                        }
                        else
                        {
                            strStatus = "--";
                        }
                        if (item["Priority"].ToString() != "")
                        {
                            strPriority = item["Priority"].ToString();
                        }
                        else
                        {
                            strPriority = "--";
                        }
                        strID = item["ID"].ToString();
                        strCreatedDate = item["Created"].ToString();
                        DataRow drow = dt.NewRow();
                        drow["Title"] = strTitle;
                        drow["Priority"] = strPriority;
                        drow["Status"] = strStatus;
                        drow["ID"] = strID;
                        drow["Created"] = strCreatedDate;
                        dt.Rows.Add(drow);

                    }
       
                }
                dSet.Tables.Clear();
                dSet.Tables.Add(dt);
                return dSet;

            }
            catch (Exception ex)
            {
                strError += ex.ToString();
                return null;
            }
        }
    }
}