Search This Blog

Tuesday, January 29, 2013

?: Operator (Ternary) (C#)

C# > OperatorsTernary (?:)

Ternary (conditional) operator (?:) returns one of two values depending on the value of a Boolean expression.

The syntax for the ternary operator is

condition ? first_expression : second_expression;

Example:




namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string OddOrEven(int x)
        {
            return (x % 2 == 0) ? "Number is even" : "Number is odd";
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string s;
            s = OddOrEven(10); // Number is even
            s = OddOrEven(11); // Number is odd
        }
    }
}










Verbatim String Literals (C#)

C# > Verbatim String Literals 

In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence.

Suppose you want to set a string as following:

string path = "C:\\Programs\\MyProgram\\My.exe";


You can use the verbatim string literal character (@) to build the string exactly as it appears within the double quotation marks.
string path = @"C:\Programs\MyProgram\My.exe";






Escape sequences (C#)



C# > Escape sequences

The special escape character is the backslash character (\).
\" Display a double quotation mark.
\'  Display a single quotation mark.
\\  Display a backslash.
\0 Null
\a Alarm
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
  
string msg = "Dan said: \"Hello world!\"";
Result is:
Dan said: "Hello world!"

string path = "C:\\MyDir";
Result is:
C:\MyDir






Filter DataTable with LINQ (Visual Basic)


VB.NET > Data > DataTable > Filter with LINQ

Use AsEnumerable method to return the input type DataTable as IEnumerable.
The CopyToDataTable method takes the results of a query and copies the data into a DataTable.






Example:

Public Class Form1

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim dt As DataTable = New DataTable("table")
    dt.Columns.Add("id", Type.GetType("System.Int32"))
    dt.Columns.Add("name", Type.GetType("System.String"))

    Dim dr As DataRow = dt.NewRow()
    dr("id") = 1
    dr("name") = "john"
    dt.Rows.Add(dr)

    dr = dt.NewRow()
    dr("id") = 2
    dr("name") = "dan"
    dt.Rows.Add(dr)

    Dim filteredTable As DataTable = (From n In dt.AsEnumerable()
        Where n.Field(Of Int32)("id") = 1
                    Select n).CopyToDataTable()

    ComboBox1.DataSource = filteredTable
    ComboBox1.DisplayMember = "name"
    ComboBox1.ValueMember = "id"
  End Sub

End Class






Thursday, January 24, 2013

Regular expression pattern Split method in C#

C# > Text > Regular Expressions > Split

Splits an input string into an array of substrings at the positions defined by a regular expression pattern.

Example:
string path = "some_text_words";
string[] substrings = Regex.Split(path, "_");
foreach (string match in substrings)
{}






Wednesday, January 23, 2013

Database Mail Views (SQL Server)

SQL Server > Catalog Views > Database Mail

These views are located in the msdb database.

Database Mail has views for displaying Database Mail e-mails information such as status of e-mails, any messages received and errors logged by Database Mail.

sysmail_allitems

Contains one row for each message processed by Database Mail.

Important columns


Name
Description
mailitem_id
Identifier of the mail item in the mail queue.
profile_id
The identifier of the profile used to send the message.
recipients
The e-mail addresses of the message recipients.
subject
The subject line of the message.
body
The body of the message.
sent_status
The status of the mail. Possible values are:
  • sent - The mail was sent.
  • unsent - Database mail is still attempting to send the message.
  • retrying - Database Mail failed to send the message but is attempting to send it again.
  • failed - Database mail was unable to send the message.
sent_date
The date and time that the message was sent.

sysmail_faileditems

Contains information about  messages were not successfully sent.
Has the same colums as but sent_status value is always failed.
To view the reason for the failure see onformation in the sysmail_event_log view.

sysmail_event_log

Contains one row for each message returned by the Database Mail system.
When troubleshooting Database Mail, search in sysmail_event_log view for events related to e-mail failures.

Important columns:


event_type
Errors, warnings, informational messages, success messages
log_date
The date and time the log entry is made.
description
The text of the message being recorded.
mailitem_id
Identifier of the mail item in the mail queue.

 




Set hour and minute to a date SQL Server


SQL Server > Datetime > Set hour and minute to a date

declare @d1 datetime

set @d1 = (Select DateAdd(hour, 6, cast(floor(cast(getdate() as float))as datetime)))
set @d1 = (Select DateAdd(minute, 45, @d1))

print @d1
Result:
Jan 23 2013 6:45AM