Monday, December 26, 2011

How to Export Data to Excel from an ASP.NET Application + Avoid the File Format Differ Prompt

 ExportToExcel class contains public scoped method Export, which converts data passed in the form of datatable to excel.


public static class ExportToExcel

{

///

/// This Method takes DataTable and Page as input parameter and

/// renders the converted excel data to given page instance.

///


/// DataTable instance

/// The Sytem.Web.UI.Page instance.

public static void Export(string fileName, GridView gv, string title)

{

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));

HttpContext.Current.Response.Charset = "";

//HttpContext.Current.Response.ContentType = "application/ms-excel";

HttpContext.Current.Response.ContentType = "application/vnd.xls";

using (StringWriter sw = new StringWriter())

{

HtmlTextWriter htw = new HtmlTextWriter(sw);

try

{

// render the table into the htmlwriter

RenderGrid(gv).RenderControl(htw);

// render the htmlwriter into the response

HttpContext.Current.Response.Write("" + title + "");

HttpContext.Current.Response.Write(sw.ToString());

HttpContext.Current.Response.End();

}

finally

{

htw.Close();

}

}

}
 
More Coding Mehtods:
 
http://blogs.msdn.com/b/erikaehrli/archive/2009/01/30/how-to-export-data-to-excel-from-an-asp-net-application-avoid-the-file-format-differ-prompt.aspx

Thursday, November 3, 2011

Unable to connect to database


hi friends,


You receive a "Cannot connect to the configuration database" error message when you access your Windows SharePoint Services Web site





You receive an "Unable to connect to database" error message when you install SharePoint Portal Server 2003 or Windows SharePoint Services



Troubleshooting Server and Database Connection Problems




Friday, October 28, 2011

DOTNet Sent Mail Coding

protected void SendMail(string FromUserName, string ToUserName, string strContent, string strSubject)
        {
            try
            {
                string mstrContents;
                //Retrives the contact mail ids
                if (FromUserName != null && FromUserName.Trim() != "" && ToUserName != null && ToUserName.Trim() != "")
                {
                    //Retrives the mailing contents

                    string strSiteName = DefaultSettings.SiteID;//ConfigurationManager.AppSettings["siteId"].ToString().Trim(); ;
                    SPSite site = new SPSite(strSiteName);
                    SPWeb web = site.OpenWeb();
                    string ToEmailid = web.AllUsers[ToUserName].Email;
                    string FromEmailId = ConfigurationManager.AppSettings["FromEmailId"].ToString().Trim(); ;
                    SmtpClient objSmtp = new SmtpClient(ConfigurationManager.AppSettings["SMTP"].ToString().Trim());


                    if (SPUtility.IsEmailServerSet(web))
                    {

                        MailAddress from = new MailAddress(FromEmailId);
                        MailAddress to = new MailAddress(ToEmailid);
                        MailMessage message = new MailMessage(from, to);
                        message.Subject = strSubject;
                        mstrContents = strContent;
                        message.Body = mstrContents;
                        objSmtp.Send(message);
                    }
                    site.Close();
                    site.Dispose();
                    web.Close();
                    web.Dispose();
                }
            }
            catch (Exception ex)
            {
                string strError = ex.Message.ToString();
                objErrorLog.WriteintoLogFile(ex.Message);
                objErrorLog.WriteintoLogFile(ex.StackTrace);
            }
        }

.Net SmtpClient e-mail send with attachments coding

 public void SendMail(string FromUserName, string ToUserName, string strContent, string strSubject, string strAttachmentPath, bool IsAttachment)
        {
            try
            {
                string mstrContents;
                //Retrives the contact mail ids
                if (FromUserName != null && FromUserName.Trim() != "" && ToUserName != null && ToUserName.Trim() != "")
                {
                    //Retrives the mailing contents

                    string strSiteName = DefaultSettings.SiteID;//ConfigurationManager.AppSettings["siteId"].ToString().Trim(); ;
                    SPSite site = new SPSite(strSiteName);
                    SPWeb web = site.OpenWeb();
                    string ToEmailid = web.AllUsers[ToUserName].Email;
                    string FromEmailId = ConfigurationManager.AppSettings["FromEmailId"].ToString().Trim(); ;
                    SmtpClient objSmtp = new SmtpClient(ConfigurationManager.AppSettings["SMTP"].ToString().Trim());


                    if (SPUtility.IsEmailServerSet(web))
                    {

                        MailAddress from = new MailAddress(FromEmailId);
                        MailAddress to = new MailAddress(ToEmailid);
                        MailMessage message = new MailMessage(from, to);
                        Attachment data = new Attachment(strAttachmentPath, MediaTypeNames.Application.Octet);
                        // Add time stamp information for the file.
                        ContentDisposition disposition = data.ContentDisposition;
                        if(strSubject.Contains("Workflow Status Report"))
                        {
                        disposition.FileName = "WorkflowStatusReport.xls";
                        }
                        if(strSubject.Contains("Live Agreement Report"))
                        {
                        disposition.FileName = "LiveAgreementReport.xls";
                        }
                        disposition.CreationDate = System.IO.File.GetCreationTime(strAttachmentPath);
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(strAttachmentPath);
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(strAttachmentPath);
                        // Add the file attachment to this e-mail message.
                        message.Attachments.Add(data);

                        message.Subject = strSubject;
                        mstrContents = strContent;
                        message.Body = mstrContents;
                        objSmtp.Send(message);
                        data.Dispose();
                    }
                    site.Close();
                    site.Dispose();
                    web.Close();
                    web.Dispose();
                }
            }
            catch (Exception ex)
            {
                string strError = ex.Message.ToString();
                objErrorLog.WriteintoLogFile(ex.Message);
                objErrorLog.WriteintoLogFile(ex.StackTrace);
            }
        }

Sharepoint Custom Search Coding


using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Query;


        public DataSet getSearchResults(string strSqlString)
        {
            DataSet dsResults = null;
            string strSiteUrl = ConfigurationManager.AppSettings["siteId"].ToString();
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(strSiteUrl))
                    {
                        FullTextSqlQuery query = new FullTextSqlQuery(site);
                        query.QueryText = strSqlString;
                        query.RowLimit = 30000;
                        query.ResultTypes = ResultType.RelevantResults;
                        ResultTableCollection coll = query.Execute();

                        if ((int)ResultType.RelevantResults != 0)
                        {
                            ResultTable tblResult = coll[ResultType.RelevantResults];
                            if (tblResult.TotalRows > 0)
                            {
                                DataTable dtResults = new DataTable("tblResults");
                                dsResults = new DataSet();
                                dsResults.Tables.Add(dtResults);
                                dsResults.Load(coll[ResultType.RelevantResults], LoadOption.OverwriteChanges, dtResults);
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dsResults;
        }

Sharepoint Server 2007 common Errors


You cannot visit any site collection from an Office SharePoint Server 2007 content database after you attach the content database to a SharePoint Server 2010 environment

You receive a "Cannot connect to the configuration database" error message when you connect to  your Windows SharePoint Services 2.0 Web site

The User Profile Synchronization service does not start on a stand-alone installation of SharePoint Server 2010


SharePoint Draft items not crawled


How to add lots of users to a site, to a list, or to a document library in Windows  SharePoint Services 3.0 and in SharePoint Server 2007


You cannot use the Item Updating event to retrieve an updated value in SharePoint  Server 2007 or in Windows SharePoint Services 3.0


Error message when you try to take an external list offline after you uninstall and then reinstall SharePoint Server 2010: "Failed to obtain signing certificate"


You receive a "Service Unavailable" error message  when you browse a Windows SharePoint Services 2.0 Web site


How to rename a computer that is running  SharePoint Portal Server 2003



How to automate the deletion of backups in SharePoint Server 2007 and in Windows SharePoint Services 3.0 by using a Visual Basic script


How to defragment Windows SharePoint Services 3.0  databases and SharePoint Server 2007 databases

Using Microsoft Windows SharePoint Services with a content database  that is configured as read-only in Microsoft SQL Server



Thursday, October 27, 2011

Microsoft Most important Sharepoint Sites

Microsoft Enterprise Search Blog

(Learn about the Enterprise Content Management features in SharePoint and Office 2010 and engage with the people who build and deliver the products)

Microsoft Records Management Team Blog

A blog about the business challenges we are all facing and the opportunities to address these challenges using technology, as we encounter the problems of records keeping, planning, retention, disposition, litigation response, holds, and so on.

Microsoft Project 2010 The official blog of the Microsoft Office product development group. Learn how to manage your work effectively 

Performance Point Services

Microsoft SharePoint Developer Documentation Team Blog
GetThePoint
Enterprise Project management Content

Microsoft Business Connectivity Services Team Blog

The Microsoft MVP Award Program Blog