Windows Phone Developers

Saturday, May 8, 2010

How to format Currency (Pound/Dollar/Euro etc) using C#/VB.NET/Csharp Writeline function

Formatting Currency in C#/VB.NET/Csharp Writeline function

You can use .NET's Composite Formatting to format the currency as shown below

    Double Incentive = 234.45;
            Console.WriteLine("The agreed incentive in local currency is {0:C} ", Incentive);
        

If you want to convert it into dollars/pounds etc you need to change the culture info :

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
            Console.WriteLine("The agreed incentive in USD is {0:C} ", Incentive);


            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB", false);
            Console.WriteLine("The agreed incentive in GBP is {0:C} ", Incentive);




The above code needs the following directives

using System.Threading;
using System.Globalization;
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

en-UK is an invalid culture identifier - VB.NET/C#/Csharp

Culture is not supported. Parameter name: name en-UK is an invalid culture identifier.


We are used to en-UK as culture. but for some reasons MS doesn't support en-UK (UK is used for Ukraine)

Hence replace
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-UK", false);
            

with

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB", false);
            
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Friday, May 7, 2010

How to pass an argument by reference in Csharp/C#/VB.NET

Pass by Reference/Pass by Value in VB.NET/C-Sharp/C#

Here is an example of passing an argument to a function by Value:

private static void PassByValueExample(double Salary)//
        {
            Salary = Salary * 1.2;
        }


Here is how it is passed as reference
private static void PassByRefExample(ref double Salary)//
        {
            Salary = Salary * 1.2;
        }


The following code shows the difference between the two

Console.WriteLine("Salary before hike is {0:F}", Sal);
            PassByValueExample( Sal);
            Console.WriteLine("Salary after hike is {0:F}", Sal);

            Console.WriteLine("Salary before hike is {0:F}", Sal);
            PassByRefExample(ref Sal);
            Console.WriteLine("Salary after hike is {0:F}", Sal);






See also:


Submit your website to 20 Search Engines - FREE with ineedhits!




Search Engine Optimization and SEO Tools




Software


webbhotell Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Input string was not in a correct format. - Csharp/VB.NET WriteLine Function

If you are using VB.NET/C# Writeline function and facing the following error, it has to do with formatting


Wrong :
double Sal = 1250.45;
        Console.WriteLine("Salary before hike is {0:D}", Sal); //Decimal instead of double
        Console.WriteLine("Salary before hike is {F}", Sal); //Index is missing
            

Correct:
double Sal = 1250.45;
        
Console.WriteLine("Salary before hike is {0:F}", Sal);


USe appropriate composite format string - decimal , double Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Friday, April 23, 2010

How to pass Database Field Name to SQLDataReader GetString Function

How to retrieve data from SQLDataReader using FieldNames in C# (.NET)

It is always better practice to use Field names instead of Index. You can decide which of the following is clear



string sPlayerName =  rdr[1].ToString();
             sPlayerName = rdr["PlayerFirstName"].ToString();


The best overloaded method match for 'System.Data.Common.DbDataReader.GetString(int)' has some invalid arguments
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Parametrized Query in C# / .NET

How to pass parameters in SQL Queries in .NET (C#)

Parametrized queries are more secure (avoids SQL Injection etc) and easy to handle. The following example shows an simple example.


The above table has details of Players like name, contact details etc.

Let us try to get the details of players whose first name is 'Sachin'. First make a connection to database :

string sConString = GetConnectionString("TeamDBConnectionString");
            SqlConnection Con = new SqlConnection(sConString);
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Con;

            Con.Open();


Then set the CommandText property of SQLCommand and pass the parameters

String sSQLCmdText = "Select * from PlayerDetails where PlayerFirstName  = @FirstName";
            Cmd.CommandText = sSQLCmdText;
            Cmd.Parameters.AddWithValue("@FirstName", "Sachin");

Once this done, declare a SQLDataReader and get the rows into it


SqlDataReader rdr = Cmd.ExecuteReader();

            while (rdr.Read() == true)
            {
              string sPlayerName =  rdr.GetString(1);
            }
            
            Con.Close();


You can also pass the FieldName as parameter to query Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

SQLDataReader - Invalid attempt to read when no data is present

The error occurs when there no data is returned / the data has not been read.

The first case can be checked using HasRows flag

If the data is not read it can be done as follows:



SqlDataReader rdr = Cmd.ExecuteReader();

            while (rdr.Read() == true)
            {
              string sName =  rdr.GetString(1);
            }
      


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon