Saturday, September 29, 2012

login failed for user ‘rob’ reason the account has expired

 

image

Promoting this error message my application has stopped working.The reason for that is expiration of the user which i have use to connect to sql server.later on i have investigate the user account in sqlserver and identified the root cause for that.

when i was creating the user i have checked the Enforce Password Expiration Check

Untitled

Finally i uncheck the check and changed the password again.now its work fine

Friday, August 10, 2012

In C#, how to check if a TCP port is available?

When hosting wcf service which is use net tcp binding and without port sharing in a windows service , we should make sure the availability of our wcf port number. Other wise service doesn't start.

There fore as a best practice its is better to have code which will check the availability of the port when starting the service.This code should included to the service startup event. 

   1: protected override void OnStart(string[] args)
   2:        {
   3:            try
   4:            {
   5:                if (serviceHost != null)
   6:                {
   7:                    serviceHost.Close();
   8:                }
   9:  
  10:                PortChecker pc = new PortChecker();
  11:  
  12:                if (pc.IsPortAvailable())
  13:                {
  14:                    serviceHost = new ServiceHost(typeof(M3ISPlusService));
  15:                    serviceHost.Open();
  16:                }
  17:            }


ANd this is the port checker class



   1: public bool IsPortAvailable()
   2:         {
   3:             bool isAvailable = false;
   4:             TcpListener tcpListner = new TcpListener(GetServerIp(), GetPort());
   5:             Logger logger = Logger.GetLogger();
   6:  
   7:             try
   8:             {
   9:                 tcpListner.Start();
  10:                 isAvailable = true;
  11:             }
  12:             catch (Exception ex)
  13:             {
  14:                 logger.Log(ex.Message);
  15:             }
  16:  
  17:             finally
  18:             {
  19:                 try
  20:                 {
  21:                     tcpListner.Stop();
  22:                     tcpListner = null;
  23:                 }
  24:                 catch (Exception)
  25:                 {
  26:                     tcpListner = null;
  27:                 }
  28:             }
  29:  
  30:             return isAvailable;
  31:         }

To get port in code please refer my previous post :- Read End point address programmatically


Happy Coding :)

Friday, August 3, 2012

Programmatically reading endpoint address from web.config file?

 

There are some instances where we have to read wcf endpoint url from the code.As following you can accomplish that task.

once we add a service to the code it will get added to web.config under <system.serviceModel> section.

so we can access web config like this.

ServicesSection servicesSection = (ServicesSection)ConfigurationManager.GetSection(BASE_ADDRESS_PATH);

var item = servicesSection.Services[0].Host.BaseAddresses[0].BaseAddress;
Uri baseAddress = new Uri(item);

Friday, June 29, 2012

Linq to data reader

using linq we can easily extract the data reader values without writing more coding.

example for populating anonymous object from datareader

using (SqlDataReader dataReader = command.ExecuteReader())
{
var annonymousObject = from IDataRecord record in dataReader
select new
{
Code = (string)record["Code"],
Description = (string)record["Description"]
};
}
example for populating custom object from datareader
using (SqlDataReader dataReader = command.ExecuteReader())
{
DataRecord dataRecord= (from IDataRecord record in dataReader
select new DataRecord
{
Code = (string) record["Code"],
Description = (string)record["Description"]
}).ToArray();
}

Wednesday, May 16, 2012

linq orderby custom comparer

I need to orderby Customer Collection.There fore i have to write new class extending with IComparer<T> interface.

public class EntityComparer<T> : IComparer<T> where T : class
    {
        #region IComparer<T> Members

        public int Compare(T x, T y)
        {
            string valuex = string.Empty;
            string valuey = string.Empty;

            if (x is StringField && y is StringField)
            {
                valuex = GetStringFieldValue(x as StringField);
                valuey = GetStringFieldValue(y as StringField);
            }

            if (x is BasicDataField && y is BasicDataField)
            {
                valuex = GetBasicDataFieldValue(x as BasicDataField);
                valuey = GetBasicDataFieldValue(y as BasicDataField);
            }

            return Compare(valuex, valuey);
        }

        #endregion

        #region Helpers

        private string GetBasicDataFieldValue(BasicDataField field)
        {
            string value = string.Empty;

            if (null !=  field && null != field.NewValue && !string.IsNullOrEmpty(field.NewValue.Description))
            {
                value = field.NewValue.Description;
            }

            return value;
        }

        private string GetStringFieldValue(StringField field)
        {
            string value = string.Empty;

            if (null != field && !string.IsNullOrEmpty(field.NewValue))
            {
                value = field.NewValue;
            }

            return value;
        }

        private  int Compare(string x, string y)
        {
            return string.Compare(x, y, true, CultureInfo.InvariantCulture);
        }

        #endregion
    }

Then we can do our linq ordering like bellows
bomLines.OrderBy(record => record.BOMCategoryType, new EntityComparer<BasicDataField>());
Here we have to create new instance of the our Custom comparer class.
Related Posts:http://prabodha-eranga.blogspot.com/2012/04/linq-order-by-multiple.html

Thursday, May 10, 2012

execute scalar function in sql server

As follows you can test your scalar function in sql server.Result holding variable type should be same as the function return type.

DECLARE @RESULT VARCHAR(2500)

EXEC @RESULT= FUNC_GET_VC_COLUMNS_ENCODED 'CO_LINE','PRI1234',0,1,1,1

PRINT @RESULT

Note:My sql server  version is 2008 R2.If you use sql server  2008 some time you might have to use schema also.

EXEC @RESULT= dbo.FUNC_GET_VC_COLUMNS_ENCODED 'CO_LINE','PRI1234',0,1,1,1

Wednesday, April 11, 2012

How to Order by Multiple in Linq

When Ordering custom collection in Linq  using multiple fields you can use OrderBy and ThenBy methods.

Example

private BOMLineDTO[] SortBOMLines(BOMLineDTO[] bomLines)
{
BOMLineDTO[] sortedBOMLines = null;

if (null != bomLines)
{
sortedBOMLines = bomLines.OrderBy(record => record.ItemDescription, new BOMLineComparer<StringField>()).
ThenBy(record => record.BOMCategoryType, new BOMLineComparer<BasicDataField>()).
ThenBy(record => record.GMTColor, new BOMLineComparer<BasicDataField>()).
ThenBy(record => record.GMTSize, new BOMLineComparer<BasicDataField>()).ToArray();
}
return sortedBOMLines;
}
Here BOMLineComparer<T> is My own class which is derived form IComparer<T> interface


Related Posts:http://prabodha-eranga.blogspot.com/2012/05/linq-orderby-custom-comparer.html