Sunday 28 October 2012

Date Format as dd/MM/yyyy


To Format DateTime

    txDate.Text=DateTime.Now.ToString("dd-MM-yyyy");

Tuesday 4 September 2012

Reorder Data Table Coloums or to Change the Data Table Index

// Code to change the index of the Data Table Coloums


dt_Data.Columns["SlNo"].SetOrdinal(0);

Monday 3 September 2012

Grid View Nested Items Adding


 
// code shows how to add items into the nested Grid Views

protected void Gd_ItemList_RowDataBound(object sender, GridViewRowEventArgs e)
       {
         

              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                 
 GridView gv = (GridView)e.Row.FindControl("Gd_Specs");
  Label lblItemId = (Label)e.Row.Cells[0].FindControl("lblItemId");
  int itemid = Convert.ToInt32(lblItemId.Text);
 string[] p_5 = { "Stat"};
 string[] v_5 = {  itemid.ToString()  };
 gv.DataSource = ObjBL.SelectDataTableUsingSp("Report", p_5, v_5);
 gv.DataBind();
           }
    }

Friday 31 August 2012

Date Time Formats

http://www.dotnetperls.com/datetime-format



C# DateTime Format

DateTime type
Many DateTime formats are available. C# programs use the ToString method on the DateTime type. ToString receives many useful formats. These formats have confusing syntax forms. Correct formatting of dates and times is essential to many programs.

Format string

To start, we see an example of how you can use a specific formatting string with DateTime and ToString to obtain a special DateTime string. This is useful when interacting with other systems, or when you require a precise format.
Program that uses DateTime format [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime time = DateTime.Now;              // Use current time
 string format = "MMM ddd d HH:mm yyyy";    // Use this format
 Console.WriteLine(time.ToString(format));  // Write to console
    }
}

Output

Feb Fri 27 11:41 2009

Format string pattern

MMM     display three-letter month
ddd     display three-letter day of the WEEK
d       display day of the MONTH
HH      display two-digit hours on 24-hour scale
mm      display two-digit minutes
yyyy    display four-digit year
Note:The letters in the format string above specify the output you want to display. The final comment shows what the MMM, ddd, d, HH, mm, and yyyy will do.

Modify format

String type
Continuing on, we see how you can modify the DateTime format string in the above example to get different output with ToString. We change some of the fields so the resulting value is shorter.
Program that uses different format [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime time = DateTime.Now;             // Use current time
 string format = "M d h:mm yy";            // Use this format
 Console.WriteLine(time.ToString(format)); // Write to console
    }
}

Output

2 27 11:48 09

Format string pattern

M       display one-digit month number          [changed]
d       display one-digit day of the MONTH      [changed]
h       display one-digit hour on 12-hour scale [changed]
mm      display two-digit minutes
yy      display two-digit year                  [changed]
Format string usages. You will also need to specify a format string when using DateTime.ParseExact and DateTime.ParseExact. This is because those methods require a custom pattern to parse.

Single-letter format

Note
Next you can use a single character with ToString or DateTime.ParseExact to specify a preset format available in the framework. These are standard formats and useful in many programs. They can eliminate typos in the custom format strings.
Program that tests formats [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime now = DateTime.Now;
 Console.WriteLine(now.ToString("d"));
 Console.WriteLine(now.ToString("D"));
 Console.WriteLine(now.ToString("f"));
 Console.WriteLine(now.ToString("F"));
 Console.WriteLine(now.ToString("g"));
 Console.WriteLine(now.ToString("G"));
 Console.WriteLine(now.ToString("m"));
 Console.WriteLine(now.ToString("M"));
 Console.WriteLine(now.ToString("o"));
 Console.WriteLine(now.ToString("O"));
 Console.WriteLine(now.ToString("s"));
 Console.WriteLine(now.ToString("t"));
 Console.WriteLine(now.ToString("T"));
 Console.WriteLine(now.ToString("u"));
 Console.WriteLine(now.ToString("U"));
 Console.WriteLine(now.ToString("y"));
 Console.WriteLine(now.ToString("Y"));
    }
}

Output

d    2/27/2009
D    Friday, February 27, 2009
f    Friday, February 27, 2009 12:11 PM
F    Friday, February 27, 2009 12:12:22 PM
g    2/27/2009 12:12 PM
G    2/27/2009 12:12:22 PM
m    February 27
M    February 27
o    2009-02-27T12:12:22.1020000-08:00
O    2009-02-27T12:12:22.1020000-08:00
s    2009-02-27T12:12:22
t    12:12 PM
T    12:12:22 PM
u    2009-02-27 12:12:22Z
U    Friday, February 27, 2009 8:12:22 PM
y    February, 2009
Y    February, 2009

Date strings

Here we see the ToLongDateString, ToLongTimeString, ToShortDateString, and ToShortTimeString methods on DateTime. These methods are equivalent to the lowercase and uppercase D and T methods shown in the example above.
Program that uses ToString methods [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime now = DateTime.Now;
 Console.WriteLine(now.ToLongDateString());  // Equivalent to D
 Console.WriteLine(now.ToLongTimeString());  // Equivalent to T
 Console.WriteLine(now.ToShortDateString()); // Equivalent to d
 Console.WriteLine(now.ToShortTimeString()); // Equivalent to t
 Console.WriteLine(now.ToString());
    }
}

Output

ToLongDateString     Friday, February 27, 2009
ToLongTimeString     12:16:59 PM
ToShortDateString    2/27/2009
ToShortTimeString    12:16 PM
ToString             2/27/2009 12:16:59 PM
Note:The default ToString method on DateTime shown above is equivalent to the simple "G" formatting string in the previous example. In other words, ToString("G") and ToString() do the same thing.

Format characters

Format
When you use DateTime.ParseExact, or ToString(), you need to specify a formatting string, which is a sequence of characters that designate how the final result will look. What follows are my notes on the strings from MSDN.
d:Use this to specify the numeric value for the day of the month. It will be one or two digits long.

dd:This is the same as a single d, except there are always two digits, with a leading 0 prepended if necessary.

ddd:This displays a three-letter string that indicates the current day of the week.

dddd:This displays the full string for the day of the week.

f:ff:fff:ffff:fffff:ffffff:fffffff:F:FF:FFF:FFFF:FFFFF:FFFFFF:FFFFFFF:Use the lowercase f to indicate the seconds to one digit length. Use two lowercase fs to indicate the seconds to two digits. The uppercase F patterns do the same but work differently on trailing zeros.

gg:Use this to display A.D. on your date. It is unlikely that this will be B.C. in most programs.

h:Display the hours in one digit if possible. If the hours is greater than 9, it will display two digits. Range is 1-12.

hh:Display the hours in two digits always, even if the hour is one digit. The range here will be 01-12.

H:This represents the hours in a range of 0-23, which is called military time in some parts of the world.

HH:This represents the hours in a range of 00-23. The only different here between the single H is that there is always a leading zero if the number is one digit.

K:Use this to display time zone information.

m:mm:This formats the minutes in your date format string. Here, the one m means that there is only one digit displayed if possible. The two ms means that there are always two digits displayed, with a leading zero if necessary.

M:MM:These display the months in numeric form. The one uppercase M does not have a leading zero on it. The two uppercase Ms will format a number with a leading zero if it is required.

MMM:This displays the abbreviated three-letter form of the month represented in the DateTime.

MMMM:This displays the full month string, properly capitalized.

s:ss:The lowercase s displays seconds. A single lowercase s means that you do not require a leading zero. Two lowercase s characters means you always want two digits, such as 00-59.

t:Use the lowercase t to indicate A, when the time is in the AM, and P, for when the time is in PM.

tt:Use two lowercase tts to display the full AM or PM string. You will normally want this for when you are displaying the string to a user.

y:yy:yyy:yyyy:yyyyy:These display the year to different digits. In your programs, you won't need three digits for the year, or five. Therefore, you should only consider one y, two ys, or four ys.

z:zz:zzz:These represent the offset from the UTC time on the local operating system.

colon ":"This is the time separator.

slash "/"This is the date separator.
Programming tip
Difference between d and dd. It is important to note that d and dd (one and two ds) mean something entirely different than ddd and dddd (three and four ds). d and dd indicate the day of the month, while ddd and dddd indicate the day of the week, in a word.

Three-letter days

In some systems it may be useful to display the day of the week in a three-letter form. Here we see a simple program that prints out the days of the week in three-letter format. This will vary based on the language installed on the computer.
Program that tests days [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime now = DateTime.Today;
 for (int i = 0; i < 7; i++)
 {
     Console.WriteLine(now.ToString("ddd"));
     now = now.AddDays(1);
 }
    }
}

Output

Thu
Fri
Sat
Sun
Mon
Tue
Wed

Complete day

Often you need to display the complete day of the week in your C# code, and the four ds together will do this for you. This simple program shows all seven different day strings you can get from the dddd.
Program that shows day strings [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime now = DateTime.Today;
 for (int i = 0; i < 7; i++)
 {
     Console.WriteLine(now.ToString("dddd"));
     now = now.AddDays(1);
 }
    }
}

Output

Thursday
Friday
Saturday
Sunday
Monday
Tuesday
Wednesday

Era

Framework
The .NET Framework allows you to display the date with the era or period, which is usually A.D. or B.C. It is unlikely that you will need to use B.C., except in a rare theoretical application. Nevertheless, here is what the two gs will print. Use the code "DateTime.Now.ToString("gg");".

Month

You may need to display the month name in a three-letter format. This is equivalent, in English, to taking a substring of the first three letters, but using the three Ms next to each other may be easier and more terse for your code. Additionally, you may want full month strings.
DateTime.Month Property

AM/PM

This isn't something you are likely to need, but interesting to find out. When you specify one t, you can get the first letter of the AM or PM string. This is equivalent to using Substring or getting the first char of the tt string. There is a space at the end of the format string because the value "t" can mean something else in the format string.
Full string. Here we see how you can get the string AM or PM in your DateTime ToString code. The code adds 12 to ensure the second iteration is in the other half.
Program that displays AM and PM [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime now = DateTime.Now;
 for (int i = 0; i < 2; i++)
 {
     Console.WriteLine(now.ToString("tt "));
     now = now.AddHours(12);
 }
    }
}

Output

PM
AM
Note:There are no periods in the output of tt in the example above. Therefore, if you require periods in your AM or PM, you would have to manipulate the string.

Year

You can vary the number of digits displayed in the year string. You will always want to use y, yy, or yyyy for your programs. The .NET Framework accepts different numbers, but they are impractical in the real world. Occasionally two ys is useful for a user-oriented program, but for your back end code, you will want to use four ys. You do not need uppercase Ys.
Program that displays years [C#]

using System;

class Program
{
    static void Main()
    {
 DateTime now = DateTime.Now;
 Console.WriteLine(now.ToString("y "));
 Console.WriteLine(now.ToString("yy"));
 Console.WriteLine(now.ToString("yyy"));   // <-- Don't use this
 Console.WriteLine(now.ToString("yyyy"));
 Console.WriteLine(now.ToString("yyyyy")); // <-- Don't use this
    }
}

Output

9
09
2009
2009
02009

Summary

We saw lots of information and examples regarding DateTime format strings. We covered single-letter preset format strings, and more complicated custom format strings with different character codes. We saw an overview of the custom code letters. Finally, we saw enumerations of all the values for days of the week, months, and also three-letter versions of those.
string.Format Method

Tuesday 21 August 2012

Code to make the Rounding of the 12.5 (Example ) To 13 or 12


// Code to make the Rounding of the  12.5 (Example )  To  13 or  12 

You can specify the behavior of Math.Round using an overload:

Math.Round(2.5, 0, MidpointRounding.AwayFromZero); // gives 3

Math.Round(2.5, 0, MidpointRounding.ToEven); // gives 2

Friday 10 August 2012

SQL Query to make Stored Procedure for Making Hash Table and to concatenate Fields


// SQL Query to make Stored Procedure for Making Hash Table and to concatenate Fields 


Create PROCEDURE [dbo].[SP_Report_Category] 
 (
  @SupID int=null,
  @Campusid int=null,
  @Status varchar(150)

)
as
     
if(@Status='Category')
BEGIN

create table #temptablecategory ( CatId int,cname varchar(max))

declare @cname varchar(max)
declare @tempcname_1 varchar(200)
declare @tempcname_2 varchar(200)
declare @tempcname_3 varchar(200)
declare @tempcname_4 varchar(200)
declare @tempcname_5 varchar(200)

set @cname=''

insert into #temptablecategory(CatId)  -- inserting into the temperory table
select supplierid from Supplierregistration where supplierid=@SupID

set @cname=''
set @tempcname_1=''
set @tempcname_2=''
set @tempcname_3=''
set @tempcname_4=''
set @tempcname_5=''

SELECT     
@tempcname_1= case when manufacturer=1 then 'Manufacturer' else '' end
,
@tempcname_2= case when distributer=1 then ',Distributer' else '' end
,
@tempcname_3= case when retail=1 then ',Retail Supplier' else '' end
,
@tempcname_4= case when Service=1 then ',Service' else '' end
,
@tempcname_5= case when Partner=1 then ',Business Partner' else '' end

FROM       
Supplierregistration

WHERE    
(deleted = 0) AND (campusid = @Campusid)and supplierid=@SupID

set @cname = @tempcname_1+@tempcname_2+@tempcname_3+@tempcname_4+@tempcname_5

update #temptablecategory set cname=@cname where CatId=@SupID

SELECT    
SupplierId, cname as categorynames
FROM        
Supplierregistration
inner join
#temptablecategory on Supplierregistration.supplierid=#temptablecategory.CatId
WHERE    
(deleted = 0) AND (campusid = @Campusid)and supplierid=@SupID

drop table #temptablecategory

END

Tuesday 7 August 2012

Writing the Data To the XML File


Writing the Data To the XML File


string fileName = RemoveSpecialCharacters(DateTime.Now.ToString()) + FileUpload1.FileName;
XmlWriterSettings st = new XmlWriterSettings();
st.Indent = true; ;
st.OmitXmlDeclaration = true;
st.Encoding = Encoding.ASCII;

// to append to the existing files
try
{
XElement xml = XElement.Load(Server.MapPath(".\\XML\\data.xml"));
xml.Add(new XElement("Software", new XAttribute("Name", TextBox1.Text),
new XElement("Version", TextBox2.Text),

xml.Save(Server.MapPath(".\\XML\\data.xml"));
}
catch(Exception ex)
{
using (XmlWriter writer = XmlWriter.Create(Server.MapPath(".\\XML\\data.xml"), st))
{
writer.WriteComment("This xml file for the Database Purpose ");

writer.WriteStartElement("Version");
writer.WriteValue(TextBox2.Text);
writer.WriteEndElement();
writer.Flush();
}
}