Extension Methods (Simple example in C# language)
Posted by hammadsiddiqui at 6:44 PM 0 comments
Labels: C#, Convert.ToInt32, Extention, method
How To Read XML Data into DataSet by Using C# .NET
I tried to create a simple User Interface. (isn't it) :)
By clicking on Browse button, a file dialogue will open just select an xml file (Note: this dialogue will only allow you to select *.xml files) and i will show you that how this useful feature can be utilised in our application to filter files in OpenFileDialog control.
When you select an xml file, this simple application will open this xml into the below Data Grid View Control (Like this):
The very simple code behind of this application is following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace XmlViewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
try
{
ShowXmlData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ShowXmlData()
{
//IT WILL SHOW THE DIALOG BOX FOR BROWSING XML FILE
//AND SAVING THEN SHOW IT TO TEXTBOX
openFileDialog1.ShowDialog();
txtFilePath.Text = openFileDialog1.FileName;
if (ValidateFileName(txtFilePath.Text))
{
//IT WILL READ DATA FROM SELECTED XML
//AND SHOW IT TO DATA GRID VIEW CONTROL
DataSet ds = new DataSet();
ds.ReadXml(txtFilePath.Text);
dgvXmlData.DataSource = ds.Tables[0];
}
else
{
throw new Exception("Invalid Data");
}
}
private bool ValidateFileName(string fileName)
{
//YOU CAN WRITE YOUR OWN LOGIC HERE
//TO FILTER FURTHER
if (fileName != "")
{
return true;
}
else
{
return false;
}
}
}
}
How to restrict OpenFileDialog for *.xml files:
- Select OpenFileDialog from Form and View the Properties.
- Select "Filter" field.
- Type "xml files|*.xml".
Posted by hammadsiddiqui at 9:09 AM 0 comments
Labels: C#, Dataset, ds.ReadXml(), Read Xml, Read Xml into Dataset, Xml
Constructor Sequence
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstructorSequence
{
class One
{
public One()
{
Console.WriteLine("Constructor of class One");
}
static void Main(string[] args)
{
Console.WriteLine("Begin Main Method");
Two t = new Two();
Console.WriteLine("End Main Method");
Console.Read();
}
}
class Two : One
{
public Two()
{
Console.WriteLine("Constructor of class Two");
}
}
}
- First, Main() method will be run and according to first line of this method, result will be "Begin Main Method".
- Second line of Main() method will going to create object of Class Two by the statement "Two t = new Two();".
- Remember, whenever new object has going to create the following three things will be done:
- Constructor of the class for which an object creation is requested will called.
- Memory space will be consumed to store that object.
- Return the reference of that object to do any thing in future with this object.
- As per above knowledge base, the constructor of class Two will be called and again Remember one thing that before going into the body of called constructor while creating object there is one thing which is implicitly defined i.e. "Calling of Base/Parent Class constructor". This is called containment.
- So, before going to the line "Console.WriteLine("Constructor of class Two");" constructor of Class One will be called and which resulted "Constructor of class One".
- After this, actual code of Class Two's constructor will be run and resulted "Constructor of class Two".
- Now above mentioned three steps for creating object has been performed and its time to leave the line "Two t = new Two();". So, next printed line will be "End Main Method".
Posted by hammadsiddiqui at 4:02 PM 1 comments
Labels: Constructor, Constructor Sequence, Consumed, Memory, Object, Reference, Sequence
Method Overloading from Inherited Class
This is very basic example but hopefully it will going to help you to understand the concept of overloading of a parent class method in child class.
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Overloading
{
class One
{
static void Main(string[] args)
{
Console.WriteLine("Begin Main Method");
Two t = new Two();
t.overload();
t.overload(0);
Console.WriteLine("End Main Method");
Console.Read();
}
public void overload()
{
Console.WriteLine("Method of class One");
}
}
class Two : One
{
public void overload(int number)
{
Console.WriteLine("Method of class Two");
}
}
}
Output will be:
Posted by hammadsiddiqui at 1:03 PM 0 comments
Labels: inherit, method, Method overloading, Method Overloading from Inherited Class
Efficiency means
“Efficiency is doing things right; effectiveness is doing the right things.”
[by Peter F. Drucker]Are you following this?
Posted by hammadsiddiqui at 1:25 AM 0 comments
Check whether date is valid or not
{
private String dob;
{
if (getDob())
{
if (isAvailable(txt_emailAddress.Text))
insertStudent();
else
lbl_error.Text = "Email already exists";
}
else
{
lbl_error.Text = "Please enter valid date";
}
}
{
dob = ddl_month.SelectedValue + " " + ddl_day.SelectedValue + ", " +
ddl_year.SelectedValue;
try
{
DateTime.Parse(dob);
return true;
}
catch
{
return false;
}
}
{
}
Posted by hammadsiddiqui at 2:51 PM 1 comments
Labels: date, valid date
C# Dynamic DropDownList of Year
private void fillDropdown()
{
int year = DateTime.Now.Year;
year -= 1;
for (int i = 0; i <= 100; i++)
{
ddl_year.Items.Add(year.ToString());
year--;
}
}
Posted by hammadsiddiqui at 8:27 PM 0 comments
Labels: Dropdownlist, year
Get Internal IP of machine in C#
{
String ip = IPHost.AddressList[0].ToString();
Posted by hammadsiddiqui at 7:06 PM 0 comments
Labels: internal ip, IP
SQL Server: Insert records from .txt file
start_ip CHAR(15) NOT NULL,
end_ip CHAR(15) NOT NULL,
start_long INT NOT NULL,
end_long INT NOT NULL,
country_code CHAR(2) NOT NULL,
country_name VARCHAR(50) NOT NULL);
BULK INSERT geoip
FROM 'd:\geoip.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
Download 'geoip.txt' here
Posted by hammadsiddiqui at 12:56 PM 0 comments
Labels: insert data, sql txt file, txt file
Using the Dynamic Keyword in C# 4.0
C# 4 provides a new dynamic keyword that enables dynamic typing in what has traditionally been a strongly typed language.
According to Dino Esposito's Article:
Dino Esposito is the author of the upcoming “Programming ASP.NET MVC” from Microsoft Press and coauthor of “Microsoft .NET: Architecting Applications for the Enterprise” (Microsoft Press, 2008). Esposito, who is based in Italy, is a frequent speaker at industry events worldwide. You can join his blog at weblogs.asp.net/despos.
Using the Dynamic Keyword in C# 4.0
Dino Esposito
Statically Typed or Dynamically Typed
- Every expression is of a type known at compile time.
- Variables are restricted to a type known at compile time.
- The compiler guarantees that type restrictions on assignments of expressions into variables meet the restrictions on the variables.
- Semantic analysis tasks, such as overload resolution, occur at compile time and the results are baked into the assembly.
Dynamic Types in C#
dynamic number = 10;
Console.WriteLine(number);Using dynamic
public void Execute() {
dynamic calc = GetCalculator();
int result = calc.Sum(1, 1);
}class Program {
static void Main(string[] args) {
// The dynamic variable gets the return
// value of a function call and outputs it.
dynamic x = DoubleIt(2);
Console.WriteLine(x);
// Stop and wait
Console.WriteLine(“Press any key”);
Console.ReadLine();
}
// The function receives and returns a dynamic object
private static dynamic DoubleIt(dynamic p) {
// Attempt to "double" the argument whatever
// that happens to produce
return p + p;
}
}dynamic vs. System.Object
dynamic p = GetSomeReturnValue();
p.DoSomething();var vs. dynamic
Posted by hammadsiddiqui at 12:44 PM 0 comments
Swap two variables without create any third temp variable
int a = 2;
int b = 3;
a = b + a; // a is 5
b = a - b; // now b is 2
a = a - b; // now a is 3
Console.Write("a = ");
Console.WriteLine(a);
Console.Write("b = ");
Console.WriteLine(b);
Console.ReadLine();
Posted by hammadsiddiqui at 6:38 PM 1 comments
Labels: swap variables, temp variable








