Tuesday, July 22, 2008

Some Common Obstacles While Implementing WCF

1)Error:Unknown directive ServiceHost in WCF service
Solution:
Reinstall the VS2008 for fixing the error .Confliction in version 2.0.0.0 of Windows Communication Foundation (pre-release) as well as version 3.0.0.0 (rtm)causes this error

2)Sending large amount of Data with WCF service
Solution:1)WCF Message Streaming
If InsufficientMemoryException is thrown when writing to the channel
then you need to stream both request and response data from client and server
respectively..

Some more problems and solutions yet to be added.Comments are welcome from the readers

Thursday, July 17, 2008

Exporting Data From Excel to SqlServer

Following are the ways to export data from excel to sql server
1)With SQLBulkCopy
create file info.xls and create table in respective database named ExcelData
Source code as follows
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Common;
public partial class ExportFromExcelToSQL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


// Connection String to Excel Workbook

string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\excel\Info.xls;Extended Properties=Excel 8.0";


// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Sheet1$]", connection);


connection.Open();

// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())

{

// SQL Server Connection String
string sqlConnectionString = "Data Source=ITCUBE-4M1HVRHY; Initial Catalog=ITC;User ID=cp;Password=cp;";

// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "dbo.ExcelData";
bulkCopy.WriteToServer(dr);
}
}
}




}
}

2)With DTS utility of SQL

Friday, July 4, 2008

Catching Errors in Event Log in root folder

Hello All,

This function helps you write the errors to event.log
which can be called in the catch block of try and catch.

public void CatchError(Exception objErr)

{
//Exception objErr = Server.GetLastError().GetBaseException(); --This is
//to be used in application_error and Page_error events


string err = "Error Caught in try and catch block \n" +
"Error in: " + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString() +
"\nStack Trace:" + objErr.StackTrace.ToString() ;
err = err + System.DateTime.Today.ToString() +" " + txtPhoneNumber.Text +"\n";



// EventLog.WriteEntry("SAWeb", err, EventLogEntryType.Error); This s //to be used while writing to event log
//Server.ClearError();
// additional actions...
// TextWriter tw = new StreamWriter("C:/Inetpub/wwwroot/SAWeb/logs/error.log",true);

TextWriter tw = new StreamWriter(Request.PhysicalApplicationPath.ToString() + "logs\\error.log", true);
// write a line of text to the file
tw.WriteLine(err);

// close the stream
tw.Close();



}