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







Full screen application (C#)

TopMost
Gets or sets a value indicating whether the form should be displayed as a topmost form.






private void Form1_Load(object sender, EventArgs e)
{
  this.MaximizeBox = false;
  this.MinimizeBox = false;
  this.TopMost = true;
  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}

Tuesday, January 22, 2013

Stop Statement (VB.NET)

VB.Net > StatementsStop

Stop is a programmatic alternative to setting a breakpoint and suspends the execution. When the debugger finds Stop statement, it breaks execution of the program.

Note: Unlike End, it does not close any files or clear any variables.

Example:

Dim i As Integer
Dim j As Integer
For i = 1 To 100
 j = i + 1
 Stop
Next i







With...End With Statement (VB.NET)

VB.NET > Statements > With...End With

Executes a series of statements that repeatedly refers to a single object or structure.

By using With...End With, you can perform a series of statements on a specified object without specifying the name of the object multiple times.

Example:


Public Class Form1
  Private Class Customer
    Public Property Id As String
    Public Property Name As String
  End Class
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    With New Customer
      .Id = 1
      .Name = "Elsoft"
      MessageBox.Show(.Name)
    End With
  End Sub




Monday, January 21, 2013

Temporary Tables (SQL Server)

SQL Server > Temporary tables

Local temporary tables
  • visible only to their creators during the same connection to an instance of SQL Server
  • deleted after the user disconnects from the instance of SQL Server
  • local temporary table name is stared with hash ("#") sign.
Example

Create temporary table

    Global temporary tables

    • visible to any user and any connection after they are created
    • deleted when all users that are referencing the table disconnect from the instance of SQL Server
    • global Temporary tables name starts with a double hash ("##")





    Get parent row RadGridView Telerik

    Telerik > Windows Forms > Get parent row RadGridView

    Example: Get parent row RadGridView Telerik

    Dim oParentRow As GridViewDataRowInfo = grd.CurrentRow.ViewInfo.ParentRow








    Terminate application (VB.Net)

    VB.NET > Statements > End

    End  force the application to stop running.
    • closes files opened Open statement
    • clears all the application variables

    Example:

    End application on button click

    Public Class Form1

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

        End Sub

        Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
            If MsgBox("Do you want to exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                End
            End If
        End Sub

    End Class