Feeds:
Posts
Comments

SQL Server DateTime formatting

When you working with database one of most often problem you face is date time data. In this post I’m going to post code snippets that do different things with datetime data and links to the original posts!

Snippet 1
Converting datetime value to a given format using built in SQL Sever functions

select convert(varchar, getdate(), 10) -- returns 12-30-06
select convert(varchar, getdate(), 1)  -- returns 12/30/06

--Convert and replacing the divider
select replace(convert(varchar, getdate(),101),'/','')  -- returns 12302006

The source for this snippet http://www.mssqltips.com/tip.asp?tip=1145

A simple code snippet to demonstrate String to DateTime and DateTime to String conversions

     DateTime dt = DateTime.ParseExact("2010-05-21", "yyyy-MM-dd", null);

     Console.WriteLine(dt.ToLongDateString()); // Display the long date string
     Console.WriteLine(dt.ToString()); // Display the default date string
     Console.WriteLine(dt.ToShortDateString()); // Display the short date string
     Console.ReadLine();

Source : http://www.codeproject.com/KB/cs/String2DateTime.aspx

Reading a text file in .Net

            string path = @"c:\samplefile.txt";
            StreamReader streamReader = new StreamReader(path);
            string line=streamReader.ReadLine();
            while ( line!= null)
            {
                Console.WriteLine(line);
                line = streamReader.ReadLine();
            }
            Console.ReadLine();

Loading XML Documents in C#

Loading XML Document from a file.

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\samplexmlfile.xml");

Loading XML Document from string

string fileContent ="<Root><SomeNode>SomeValue</SomeNode></Root>";
XmlDocument doc = new XmlDocument();
TextReader rdr = new StringReader(fileContent);
doc.Load(rdr);

Hope this help!

During recent debugging of T-SQL stored procedures, I found adding following
code in your catch block will help to point the actual cause of error and make your programming life much easy!

 Begin Try
    Print 'Do Something'
 End Try
 BEGIN CATCH
         ROLLBACK TRANSACTION
         print 'Error Msg: ' + ERROR_MESSAGE()
         print 'Error Procedure: ' + ERROR_PROCEDURE()
         print 'Error Number: ' + convert(varchar(50),ERROR_NUMBER())
         print 'Error Line: ' + convert(varchar(50),ERROR_Line()
   END CATCH

Source : http://msdn.microsoft.com/en-us/library/ms178600.aspx

C# Conditional statement

A neat language syntax in C#

isTrue ? thenDoThis : ElseDoThis

bool isBodyHTML=onFileReceived.Attributes["isBodyHTML"].Equals("true")?true:false

C# 4.0 now supports using optional parameters with methods, constructors, and indexers (note: VB has supported optional parameters for awhile).

This is how you declare your method to have optional parameter

public Boolean SendMail( string[] toAddresses, string subject, string body,string sender = "bala.singam@hotmail.com",bool isBodyHTML=false)
{
 
try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From =new MailAddress(sender);
                mailMessage.IsBodyHtml = isBodyHTML;

                foreach (string address in toAddresses)
                    mailMessage.To.Add(new MailAddress(address));

                mailMessage.Subject = subject;
                mailMessage.Body = body;

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Send(mailMessage);

                return true;
            }
            catch (SmtpException ex)
            {
               Console.WriteLine(ex);
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
}

And this is how you call your method with optional parameters/named arguments

//I did't passed sender parameter value
mailer.SendMail(toAddresses:new string[] { "bala.singam@hotmail.com" },subject: "File Received", body:Encoding.UTF8.GetString(msg),isBodyHTML:true);

Source : http://weblogs.asp.net/scottgu/archive/2010/04/02/optional-parameters-and-named-arguments-in-c-4-and-a-cool-scenario-w-asp-net-mvc-2.aspx

%d bloggers like this: