Search This Blog

Friday, March 28, 2014

Null keyword C#

C# > Keywords > Null

Null represent reference that does not refer to any object.
  • default value of reference type variables
  • ordinary value types cannot be null

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public class TestClass
    {
        public void TestMethod() { }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            TestClass tc=null;

            tc = new TestClass();
            tc.TestMethod();   // You can call TestMethod method.

            tc = null; // set tc to null => the object is no longer accessible and can now be garbage collected
           
            string s1 = null;

            //int k = null; // Error Cannot convert null to 'int' because it is a non-nullable value type  

            int? k = null; // use a nullable value type instead OK

        }
    }
}










Tuesday, March 25, 2014

Filter with LINQ DataTable C#

C# > System.Data   > DataTable > Filter with LINQ

Example:


DataTable dt = new DataTable("table");
dt.Columns.Add("id", Type.GetType("System.Int32"));
dt.Columns.Add("name", Type.GetType("System.String"));

DataRow dr = 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);

DataTable tbl = (from DataRow dr1 in dt.Rows
                     where dr1["id"].ToString() == "1"
                     select dr).CopyToDataTable();





Friday, March 21, 2014

DateValue Visual Basic Example

VB.NET Functions > DateValue 

Returns a date value containing the date information represented by a string.

Example

Dim dt As Date
dt = DateValue("March 21, 2014")

MessageBox.Show(dt)






Oracle Sequence Example

OracleSequence

Sequences are database objects from which multiple users can generate unique integers. The sequence generator generates sequential numbers, which can help to generate unique primary keys automatically, and to coordinate keys across multiple rows or tables.


Example:

CREATE SEQUENCE order_sequence
      INCREMENT BY 1
      START WITH 1
      NOMAXVALUE
      NOCYCLE
      CACHE 10;

INSERT INTO orders (order_no)
VALUES (order_sequence.NEXTVAL);





Wednesday, March 19, 2014

Var C# Example

C#Types > Var

Var is implicitly typed.

An implicitly typed local variable is strongly typed, but 

the compiler determines the type. 


Example


// var is required because the select clause specifies an anonymous type 

var query = from prod in products
            where prod.Category == "shoes"
            select new { prod.Name, prod.Price };

// var is required because item is an anonymous type
foreach (var item in query)
{
               

}




C# Void Type Example

C# > Types > Void

Void specifies that the method doesn't return a value.

Example


private void Log(string msg)
{
   // log method

}






SQL Server Drop

SQL Server > DDL > Drop

DROP statements removes existing entities.




DateDiff Visual Basic Example

VB.NET Functions > DateDiff

Returns a value specifying the number of time intervals 
between two dates.

Example:


Dim dt1 As Date = #3/1/2014#
Dim dt2 As Date = #4/1/2014#

MessageBox.Show("Day diff: " & DateDiff(DateInterval.Day, dt1, dt2) & " Week diff: " & DateDiff(DateInterval.Weekday, dt1, dt2))






Monday, March 17, 2014

C# Forms

C# > Forms

Contains classes for creating Windows-based applications using features available in the Microsoft Windows operating system.







Wednesday, March 12, 2014

MessageBox C# Example

C# > Forms > MessageBox

Examples:

A. Displays a message box with specified text.

    MessageBox.Show("Text message");

B. Displays a message box with specified text, caption, and buttons.

string message = "Do you want to close this windows?";
string caption = "Question";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
  this.Close();
}







Monday, March 10, 2014

AppActivate Visual Basic

VB.NET Functions > AppActivate

Activates an application that is already running.

Example:
Start and activate notepad

AppActivate(Shell("C:\Windows\System32\notepad.exe", AppWinStyle.NormalFocus))




CurDir Visual Basic

VB.NET Functions > CurDir 

Returns the current path.

Example:


MessageBox.Show(CurDir())







Thursday, March 6, 2014

C# Random Color

C# > Color > Random

Random rnd = new Random();


Color randomColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));





Change DataGridView column to Hyperlink Runtime

C# > DataGridViewDataGridViewLinkCell

Example: Change DataGridView column to Hyperlink Runtime


foreach (DataGridViewRow r in dgv.Rows)
{
DataGridViewLinkCell lc = new DataGridViewLinkCell();
       lc.Value = r.Cells[1].Value;
       dgv[1, r.Index] = lc;
}


private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
              return;
if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewLinkCell)
       {
             string link = "http://www...";
             if(e.ColumnIndex == 1)
                System.Diagnostics.Process.Start(link  +                          dgv.Rows[e.RowIndex].Cells[4].Value as string);
       }

}




Read save data to Registry C# Example

C# > Win32 > RegistryKey 

RegistryKey represents a key-level node in the Windows registry

Example:
Save user name to registry

RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("MyProgram");
if (regKey == null)
  regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("MyProgram");
if (regKey.GetValue("User", "") == null)
{
  regKey.SetValue("User", txtUser.Text);
}
else
{
  if (txtUser.Text != regKey.GetValue("User", "").ToString())
  {
    regKey.SetValue("User", txtUser.Text);
  }
}

regKey.Close();




Wednesday, March 5, 2014

Multiple Filter DataGridView DataSource

C# > DataGridViewMultiple Filter

RowFilter gets or sets the expression used to filter.

Example:

Filter by code or description


string rowFilter = string.Format("[{0}] like '%{1}%'", "Code", txtSearch.Text);
rowFilter += string.Format(" OR [{0}] like '%{1}%'", "Description", txtSearch.Text);

(DataGridView1.DataSource as DataTable).DefaultView.RowFilter = rowFilter;





Handle click event in Button Column in DataGridView

C#DataGridViewDataGridViewButtonColumn

Add DataGridViewButtonColumn to your DataGridView.
To handle button click use CellClickEvent.

Example

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
  if (e.ColumnIndex == dataGridView1.Columns["Browse"].Index && e.RowIndex >= 0)
  {
               
  }
}