This Blog contains code snippets which I came across when programming in C#.These short code snippets will facilitate easy programming in C#.Any code snippets you wish to perceive will also be uploaded on request.
Popular Posts
-
1. Client Side Scripting In client side scripting the client requests a web page and the server returns an HTML document and an embedded a...
-
PROCEDURE How to obtain details of the server u want to send an email through on command prompt follow the following steps 1...
-
using .... //include all the libraries you want to include namespace trialprograms { public partial class Form1 : Form { ...
-
Partial classes give you the ability to split a class into more than one source code .cs file. If a class becomes particularly long and int...
-
Make Life Easy: Code to send email through C# .net using Gmail ser... : "PROCEDURE How to obtain details of the server u wan...
-
What exactly happens when an ASP.NET receives a request for the Codename.aspx First the request for the page is sent to the web server.If ...
-
HERE :FormName is the name of the Form formobject is the object of the form CODE SNIPPET: FormName formobject =...
-
To obtain the path of where the vb.net windows form is loaded: string path = System.Windows.Forms.Application.StartupPath; ...
-
Make Life Easy: Behind the scenes of a ASP.NET code : "What exactly happens when an ASP.NET receives a request for the Codename.aspx F...
-
*dtpDate.Value > DateTime.Now OR *dtpDate.Value<DateTime.Now OR *dtpDate.Value<=DateTime.Now You may apply any operator in b...
Thursday, June 23, 2011
Tuesday, June 14, 2011
how to compare any date value with the current system date in c#
*dtpDate.Value > DateTime.Now
OR *dtpDate.Value<DateTime.Now
OR *dtpDate.Value<=DateTime.Now
You may apply any operator in between the two values in order to compare.
Here the value marked star can be any value you wish to compare with the system date.
Tuesday, April 12, 2011
Make Life Easy: Behind the scenes of a ASP.NET code
Make Life Easy: Behind the scenes of a ASP.NET code: "What exactly happens when an ASP.NET receives a request for the Codename.aspx First the request for the page is sent to the web server.If y..."
Friday, March 11, 2011
Behind the scenes of a ASP.NET code
What exactly happens when an ASP.NET receives a request for the Codename.aspx
First the request for the page is sent to the web server.If you're running a live site ,the web server is almost certainly IIS.
IF YOUR RUNNING THE PAGE IN VISUAL STUDIO:
The request is sent to the built in Visual Studio's test server.
2.The web server determines the .aspx extension is registered with ASP.NET and passes it to the ASP.NET worker process.If the file extension belonged to another service(as it would for .asp or .html files),ASP.NET would never get evolved.
3.If this is the first time the page in this application has been requested,ASP.NET automatically creates the application domain .It also compiles all the web page code for optimum performance ,and caches the compiled files in the directory C:\Windows\Microsoft.NET\framework\v2.0.570727\Temporary ASP.NET files.If this task has already been performed ,ASP.NET will reuse the compiled version of the page.
4.The compiled Codename.aspx page acts like a miniature program.It starts firing events(most probably in the
Page.Load event).However,you haven't created an event handler for the event,so no code returns.At this stage,everything is working together as a set of in-memory .NET objects
5.When the code is finished,ASP.NET asks every control in the web page to render itself into the corresponding HTML markup.
6.The final page is sent to the user,and the application ends.
First the request for the page is sent to the web server.If you're running a live site ,the web server is almost certainly IIS.
IF YOUR RUNNING THE PAGE IN VISUAL STUDIO:
The request is sent to the built in Visual Studio's test server.
2.The web server determines the .aspx extension is registered with ASP.NET and passes it to the ASP.NET worker process.If the file extension belonged to another service(as it would for .asp or .html files),ASP.NET would never get evolved.
3.If this is the first time the page in this application has been requested,ASP.NET automatically creates the application domain .It also compiles all the web page code for optimum performance ,and caches the compiled files in the directory C:\Windows\Microsoft.NET\framework\v2.0.570727\Temporary ASP.NET files.If this task has already been performed ,ASP.NET will reuse the compiled version of the page.
4.The compiled Codename.aspx page acts like a miniature program.It starts firing events(most probably in the
Page.Load event).However,you haven't created an event handler for the event,so no code returns.At this stage,everything is working together as a set of in-memory .NET objects
5.When the code is finished,ASP.NET asks every control in the web page to render itself into the corresponding HTML markup.
6.The final page is sent to the user,and the application ends.
Wednesday, March 9, 2011
Make Life Easy: Code to send email through C# .net using Gmail ser...
Make Life Easy: Code to send email through C# .net using Gmail ser...: "PROCEDURE How to obtain details of the server u want to send an email through on command prompt fo..."
Monday, March 7, 2011
Code to send email through C# .net using Gmail server(complete detailed description)
PROCEDURE
How to obtain details of the server u want to send an email through
on command prompt follow the following steps
1.c:\nslookup
2.>set type=mx
3.>enter the server name (example : google.com)
4.Press enter
COMPLETE PROCEDURE:
(Follow these steps step-wise in your command prompt)
c:\>nslookup
Default Server: UnKnown
Address: 218.248.241.4
> set type=mx
> google.com
Server: UnKnown
Address: 218.248.241.4
Non-authoritative answer:
google.com MX preference = 300, mail exchanger = google.com.s9b1.psmtp.com
google.com MX preference = 400, mail exchanger = google.com.s9b2.psmtp.com
google.com MX preference = 100, mail exchanger = google.com.s9a1.psmtp.com
google.com MX preference = 200, mail exchanger = google.com.s9a2.psmtp.com
google.com nameserver = ns2.google.com
google.com nameserver = ns3.google.com
google.com nameserver = ns4.google.com
google.com nameserver = ns1.google.com
google.com.s9a1.psmtp.com internet address = 74.125.148.10
google.com.s9a2.psmtp.com internet address = 74.125.148.11
google.com.s9b1.psmtp.com internet address = 74.125.148.13
google.com.s9b2.psmtp.com internet address = 74.125.148.14
ns1.google.com internet address = 216.239.32.10
ns2.google.com internet address = 216.239.34.10
ns3.google.com internet address = 216.239.36.10
ns4.google.com internet address = 216.239.38.10
>
UR C# .NET CODE:
DESIGN
FORM :
CODE:
(copy this code in the forms code)
(please verify the namespace)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
namespace Mailing
{
public partial class Mail : Form
{
public Mail()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
message.Priority = MailPriority.High;
message.IsBodyHtml = true;
SmtpClient emailclient = new SmtpClient();
emailclient.Credentials = new System.Net.NetworkCredential("your email address","youremailpassword");
emailclient.Port = 587; //port number for gmail
emailclient.Host = "smtp.gmail.com"; //gmail host SMTP server
emailclient.EnableSsl = true; //for security purpose
try
{
emailclient.Send(message);
MessageBox.Show("Message Sent");
}
catch (Exception ae)
{
MessageBox.Show(ae.ToString());
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Monday, February 21, 2011
Good feature of C#:Partial classes
Partial classes give you the ability to split a class into more than one source code .cs file.
If a class becomes particularly long and intricate you might decide to break it into pieces as shown below
//This file is stored in the Product.cs
public partial class Product
{
private string name;
private decimal price1;
public string Name
{
}
public decimal Price
{
}
public string ImageUrl
{
}
}
public partial class Product
{
//continued code here
}
these two partial classes comprise one class Product and are divided to simplify coding.
If a class becomes particularly long and intricate you might decide to break it into pieces as shown below
//This file is stored in the Product.cs
public partial class Product
{
private string name;
private decimal price1;
public string Name
{
}
public decimal Price
{
}
public string ImageUrl
{
}
}
public partial class Product
{
//continued code here
}
these two partial classes comprise one class Product and are divided to simplify coding.
Handling events in VB.NET C#
Copy this code as it is to the code part of your form.
Debug the code step by step to understand how events are called.
Delegates are used for calling events to implement event based system elegantly.
How to debug:
Click on the left side pane of your coding window to add a debug point.
press Ctrl+F11 to debug the code step wise.
CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace trialprograms3
{
public partial class Form1 : Form
{
//declaring the event handler delegate
delegate void ButtonEventHandler(object source,int clickCount);
class Button
{
//declaring the event
public event ButtonEventHandler ButtonClick;
//Function to trigger the event
public void clicked(int count)
{
MessageBox.Show("\nInside Clicked !!!");
//Invoking all the event handlers
if (ButtonClick != null)
ButtonClick(this,count); //the parameters sent to the event should match those of the above declared delegate
//delegate is like a function pointer
//onButtonAction event function is called from here.
}
}
public class Dialog
{
public Dialog()
{
Button b = new Button();
//Adding an event handler
b. ButtonClick += new ButtonEventHandler(onButtonAction);
//Triggering the event
b.clicked(1); //public void click is called here and a count of 1 is passes as parameter
b.ButtonClick += new ButtonEventHandler(onButtonAction);
b.clicked(1);
//Removing an event handler
b.ButtonClick -= new ButtonEventHandler(onButtonAction);
b.clicked(1);
b.ButtonClick -= new ButtonEventHandler(onButtonAction);
b.clicked(1);
}
//Event Handler function
public void onButtonAction(object source,int clickCount)
{
MessageBox.Show("Inside Event Handler !!!");
}
}
public Form1()
{
InitializeComponent();
new Dialog();
}
}
}
Sunday, February 20, 2011
Code Snippet to paint a panel border with a specific color.
e.Graphics.DrawRectangle(Pens.Green, 0, 0, ((Panel)sender).Width - 1, ((Panel)sender).Height - 1); base.OnPaint(e);
Saturday, February 19, 2011
Difference between Client Side and Server side programming
1.
Client Side Scripting
In client side scripting the client requests a web page and the server returns an HTML document and an embedded applet and the application is run on the client side.
Server Side Scripting
In server side scripting the client requests a web page an a server side application is run on the server and the server returns an HTML document to the client.
2.
Client Side Scripting
In client side scripting client side code can't access server side resources.
Server side scripting
Client side code can access server side resources.
Client Side Scripting
In client side scripting the client requests a web page and the server returns an HTML document and an embedded applet and the application is run on the client side.
Server Side Scripting
In server side scripting the client requests a web page an a server side application is run on the server and the server returns an HTML document to the client.
2.
Client Side Scripting
In client side scripting client side code can't access server side resources.
Server side scripting
Client side code can access server side resources.
SERVER SIDE WEB APPLICATION |
CLIENT SIDE WEB APPLICATION |
Wednesday, February 16, 2011
Make Life Easy: To display a form as a MDI child
Make Life Easy: To display a form as a MDI child: "HERE :FormName is the name of the Form formobject is the object of the form CODE SNIPPET: &..."
To display a form as a MDI child
HERE :FormName is the name of the Form
formobject is the object of the form
CODE SNIPPET:
FormName formobject = new FormName();
MasterPage MDI = (MasterPage)this.ParentForm;
formobject.MdiParent = this.ParentForm;
formobject.Location = new Point(0, 0);
formobject.StartPosition = FormStartPosition.Manual;
formobject.Show();
formobject.TopLevel = false;
formobject.Parent = MDI.panel1;
formobject.Dock = DockStyle.Fill;
formobject.AutoScroll = true;
this.Close();
Tuesday, February 15, 2011
Make Life Easy: code to View any file through vb.net
Make Life Easy: code to View any file through vb.net: "//path is the full path of the file System.Diagnostics.Process.Start(path); 1.U may also start .exe files with this line of code."
Sunday, February 13, 2011
Delegates Made Easy
using .... //include all the libraries you want to include
namespace trialprograms
{
public partial class Form1 : Form
{
//declare a delegate
public delegate void Del(string message);
//Del is the name of the delegate
//x,y are the arguments to the delegate
//int is the return type of the delegate
private void DelegateMethod(string message)
{
MessageBox.Show(message);
}
public void func_checkdelegate(int x,int y,Del callback)
{
//invoke the delegate and send the parameters
callback("The sum of the two numbers is "+ (x + y).ToString());
}
public Form1()
{
InitializeComponent();
//instantiate the delegate
//Here it points to the function Delegate Method
Del handler = DelegateMethod; //the return type the signature of the delegate and the function it points to must match
//sending the object of the delegate with the function
func_checkdelegate(2,3,handler);
}
}
Friday, February 11, 2011
C# VB.NET
To obtain the path of where the vb.net windows form is loaded:
string path = System.Windows.Forms.Application.StartupPath;
path = path.Replace(@"\bin\Debug", "");
Subscribe to:
Posts (Atom)