Archive for the ‘.Net’ Category

Sharing a small tip 🙂 If you want to add spaces to the end of a string,C# gives us method PadRight to do that.If the length of string is specified as 16 for eg and incase user enters only three characters and you want to add spaces to create a total length of 16,you can write as below.This example gets input from a textbox and adds spaces to the end of string entered.

this.txtLoginName.Text.ToString().PadRight(16);

Advertisements

It’s been quite some time since I shared anything and today I am going to share something about Enums. When you use Enums, it makes your code easily understandable.

Lets take a small Enum as example

public enum Status

        {

            New = 1,

            Active = 2,

            Inactive = 3

        }

  If you want to change an Enum into array of strings, you can use the Enum class GetNames method:

string[] Status = Enum.GetNames(typeof(Status));

You can bind the output from GetNames to a control to get the list of the names in the Enum displayed in UI.

There is a common scenario where we want to convert  a string containing one of the names in an Enum into the Enum item itself when you are reading the data from database.

Parse method below does that job,but throws an exception “System.ArgumentException was unhandled” if the string value you are passing to Parse isn’t an item in the Enum.

string custStatus = “Test”;

Status sts = (Status)Enum.Parse(typeof(Status), custStatus);

This exception can be avoided if you use the Enum’s TryParse method.

  Status stat;

    if (Enum.TryParse<Status>(custStatus, true, out stat))

    {

        Console.WriteLine(“Status Found: “+stat.ToString());

    }

    else

    {

        Console.WriteLine(“Status not found!”);

    }

An even better solution  is to use the Enum’s IsDefined method to see if the item exists before you try to get it .

if (Enum.IsDefined(typeof(Status), custStatus))

            {

                Status sts = (Status)Enum.Parse(typeof(Status), custStatus);

            }

            else

            {

                Console.WriteLine(“Status not found!”);

            }

 

Hope you enjoyed reading this.

Some people are really fast in creating properties and you might have thought how they do it.This tip is for you if you did not know it before 🙂

You can type “prop” and then press tab twice, that will generate the following.

public int MyProperty
{
get;
set;
}

You can also get the full property by typing “propfull” and tabbing twice, that would generate the field and the full property.

private int myVar;

public int MyProperty
{
get
{
return myVar;
}
set
{
myVar = value;
}
}

Past two days I was so upset with my computer that whenever I tried running my workflow xamlx,it started throwing exception “A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections” .It was then I realized that my recent effort to install SQL Express 2012 has messed up my SQL Express 2008 with VS 2010.I tried to repair,remove,add etc options inorder to get my SQLExpress back & get my workflow back to normal.No luck & I was almost at the verge of crying thinking about what the admin guys are going to tell me if I take my computer to them “Just Re-image the machine,that’s the only thing we can do” .Today evening inspite being a holiday and a having bad cold,i decided to give a try once again if I can do something.I knew that if I delete the entries of SQLServer from registry it may help.My hubby darling was advicing me saying you are gonna mess up your windows as well if you play with registry.I wanted to listen to him,but I thought if I have to reimage the system next morning,there is nothing wrong in playing with registry.Here is what I did & that infact successfully helped me install SQLExpress 🙂
1) Open Run Menu and type regedit to open Registry Editor
2)Go to following location.
HKEY_LOCAL_MACHINE > Software > Microsoft > Microsoft SQL Server > 90
3)Remove Registry Key 90.

I manually removed the SQLServer instances from Control Panel\All Control Panel Items\Programs and Features and after that I ran visual studio setup and installed Microsft SQL Express.It got installed successfully & I could run my workflow after that :)It was worth giving a try.Eureka!!

Today I was having a look at one of our WCF Restful projects.To start with the code understanding,I decided to host that in my local host and then take a look.To my surprise,I was unable get a svc url as we usually get, for example: http://wordpress.com/Service1.svc .Digging further I realized, being a wcf restful service it’s not ideal having an extension this way and it has been achieved through System.Web.Routing integration .Routing allows to host a WCF restful service that responds to URIs without an extension .Cool right?It’s a new feature with .Net 4.0.I am avoiding a rewriting of an example code here as msdn has got a beautiful simple explanation over here.Take a look!

Today’s post is a small code snippet for you to write byte array to an xml file using FileStream and Binary Writer class’s Write method.Hope this helps 🙂

private void ByteArrayToFile(string fileName, byte[] byteArray)
{
FileStream fs = new FileStream(fileName + ".xml", FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs, Encoding.Unicode);
bw.Write(byteArray);
bw.Close();
fs.Close();
}

Sharing a small tip here.If you want to check whether the OracleClob Parameter value from database is null or not and assign value,make use of the IsNull property as below.

if (!((OracleClob)(command.Parameters[2].Value)).IsNull)
{
OracleClob clob = (OracleClob)command.Parameters[2].Value;
}