April 28, 2011

LINQ to SQL using JavaScriptSerilizor

For serilization of code in linq to sql might review this code which resolve your error if occurs

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetEmployee(int employeeId)
{
string returnString = "";
DbTransaction getTransaction = null;
DatabaseDataContext dataContext = null;

try
{
dataContext = new DatabaseDataContext(Classes.Constants.GetConnectionString());
dataContext.Connection.Open();
getTransaction = dataContext.Connection.BeginTransaction();
dataContext.Transaction = getTransaction;

ArrayList ar = new ArrayList();

List getEmployee = dataContext.spEmpSelect(Convert.ToInt64(employeeId)).Select(
p =>
new Employee()
{
A= p.A,
B = p.B,
FirstName = p.FirstName,
LastName = p.LastName

}).ToList();

List getEmAddr= dataContext.sp_getEmAddr(employeeId).Select(
p =>
new EmployeesAddress()
{
Addr = p.Addr,
Address1 = p.Address1,
City = p.City,
Country = p.Country,
ContactNumber1 = p.ContactNumber1,
ContactNumber2 = p.ContactNumber2
}).ToList();


var jss = new JavaScriptSerializer();
ar.Add(getEmployee);
ar.Add(getEmAddr);
returnString = jss.Serialize(ar);
getTransaction.Commit();
}
catch (Exception exception)
{
returnString = exception.Message;
getTransaction.Rollback();
}
finally
{
if (dataContext.Connection.State == System.Data.ConnectionState.Open)
{
dataContext.Connection.Close();
}
}
return returnString;
}

No comments:

Post a Comment