Monday, January 21, 2008

Send Email using ASP.Net














Create a web page

and in Default.aspx.cs add the following code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
namespace Email
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblFrom;
protected System.Web.UI.WebControls.Label lblCC;
protected System.Web.UI.WebControls.TextBox txtFrom;
protected System.Web.UI.WebControls.TextBox txtCC;
protected System.Web.UI.WebControls.TextBox txtTo;
protected System.Web.UI.WebControls.Label lblTo;
protected System.Web.UI.WebControls.Label lblEmailService;
protected System.Web.UI.WebControls.TextBox txtBCC;
protected System.Web.UI.WebControls.Label lblBCC;
protected System.Web.UI.WebControls.Label lblSubject;
protected System.Web.UI.WebControls.TextBox txtSubject;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.TextBox txtMessage;
protected System.Web.UI.WebControls.Button btnSend;
protected System.Web.UI.HtmlControls.HtmlInputFile FileBrowse;
protected System.Web.UI.WebControls.Label lblAttachment;
protected System.Web.UI.WebControls.RadioButton rbtnAttach;
protected System.Web.UI.WebControls.RadioButton rbtnDetachment;
protected System.Web.UI.WebControls.Button btnCancel;
public string strAttachment;

private void Page_Load(object sender, System.EventArgs e)
{
lblMessage.Text="";
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.rbtnDetachment.CheckedChanged +=
new System.EventHandler(this.rbtnDetachment_CheckedChanged);
this.btnCancel.Click +=
new System.EventHandler(this.btnCancel_Click);
this.btnSend.Click +=
new System.EventHandler(this.btnSend_Click);
this.rbtnAttach.CheckedChanged +=
new System.EventHandler(this.rbtnAttach_CheckedChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion


//Method for sending the email
//***************************************************************
private void SendMail()
{
try
{
//Creating the Mail Message object.
MailMessage Email=new MailMessage();

//Storing the To value in the object reference.
Email.To=txtTo.Text;

//Storing the From value in the object reference.
Email.From=txtFrom.Text;

//Storing the CC value in the object reference.
Email.Cc=txtCC.Text;

//Storing the BCC value in the object reference.
Email.Bcc=txtBCC.Text;

//Storing the Subject value in the object reference.
Email.Subject=txtSubject.Text;

//Specifies the email body.
Email.Body=txtMessage.Text;

//Setting priority to the mail as high,low,or normal
Email.Priority=MailPriority.High;

//Formatting the mail as html or text.
Email.BodyFormat=MailFormat.Text;

//Checking whether the attachment is needed or not.
if(rbtnAttach.Checked)
{
//Adding attachment to the mail.
Email.Attachments.Add(
new MailAttachment(FileBrowse.Value));
}

//specifying the real SMTP Mail Server.
SmtpMail.SmtpServer.Insert(0,"127.0.0.1");
SmtpMail.Send(Email);//Sending the mail.

//calling the reset method to erase all the data
//after sending the mail.
Reset();
lblMessage.ForeColor=Color.Navy;

//User information after submission.
lblMessage.Text=
"*Your email has been sent successfully-Thank You";
}
//Catching Exception
catch(Exception exc)
{
Reset();
lblMessage.Text="Send error:"+exc.ToString();
lblMessage.ForeColor=Color.Red;
}
}

//Method to reset the text fields.
//***********************************************************
private void Reset()
{
//Seeking for the controls in the active webform.
Control myForm=Page.FindControl("Form1");

//Iterating each control.
foreach(Control ctl in myForm.Controls)
{
//Checking whether it is a text
//box and clear when it is a textbox.
if(ctl.GetType().ToString().Equals(
"System.Web.UI.WebControls.TextBox"))

((TextBox)ctl).Text="";
}
}

//Event fires for sending the mail.
//***********************************************************
private void btnSend_Click(object sender, System.EventArgs e)
{
SendMail();
}

//Event for cancelling the mail.
//***********************************************************
private void btnCancel_Click(object sender, System.EventArgs e)
{
Reset();
}

//Event for enabling or disabling the File browser.
//************************************************************
private void rbtnAttach_CheckedChanged(object sender,
System.EventArgs e)
{
FileBrowse.Disabled=false;
}

//Event for enabling or disabling the File browser.
//************************************************************
private void rbtnDetachment_CheckedChanged(object sender,
System.EventArgs e)
{
FileBrowse.Disabled=true;
}
//*************************************************************
}
}

Sunday, January 20, 2008

Question for Dot Net Interview (Part 5):

--* Difference between web form and web application:
1) Web Application consists of one or many web forms
2) the web form are nothing without the web application

--* What’s the top .NET class that everything is derived from?
System.Object.

--* Can you inherit multiple interfaces?
Yes.Dot NET does support multiple interfaces

--* Is it possible to Override Private Virtual methods.
No, as you cannot declare a method as ‘private virtual’

--* What is the syntax to inherit from a class in C#?
Example: class NewClassName : BaseClassName

--* what is the different between virtual method and abstract method :
1) Abstract methods are required to be overridden
2) Virtual methods are not required to be overridden
3) Abstract method is a method without body or immplementation
4) An abstract method can only appear in an abstract class.
5) Methods declared as "abstract" do not have an implementation so this forces the derived class to provide an implementation for them by overriding these abstract method
6) Methods declared as "virtual" have an implementation and the derived class has the option of overriding this method

--* What is the difference between control and component :
1) Component, which has no visible interface and adds specific functionality to project
2) Control, such as a Data Grid or simple Textbox, is a basic element of the user interface
3) Components are classes compiled into a DLL file
4) Controls are assemblies

--* The Difference Between the HAVING and WHERE Clauses in a SQL Query :
1) The WHERE clause can be used without the GROUP BY clause
2) The HAVING clause cannot be used without the GROUP BY clause but When GROUP BY is not used,HAVING behaves like a WHERE clause.
3) The WHERE clause selects rows before grouping
4) The HAVING clause selects rows after grouping
5) The WHERE clause cannot contain aggregate functions
6) The HAVING clause can contain aggregate functions
7) HAVING can be used only with the SELECT statement
8) The WHERE clause can be used with select , update , delete statements

--* Can DateTime variables be null?
DateTime is a value type, it cannot be null

--* State three different new features introduced in C# 2.0 :
1) Static classes
2) Compression and Decompression classes
3) Generics
4) Partial types

--* What is the difference between TRUNCATE and DELETE commands?
1) The DELETE statement removes one or more rows from a table or view by using a transaction.
2) The TRUNCATE TABLE statement is used to remove all rows from a table without using a transaction.
3) TRUNCATE is a Data Definition Language DDL command
4) DELETE is a Data Manipulation Language DML command
5) WHERE clause can be used with DELETE
6) WHERE clause can't be used with TRUNCATE.

--* what is difference between "Primary key" and "Unique key"?
1) unique key can allow one null but primariy key can't be null.
2) primariy key can be refrenced to other table as Foreign keys.
3) we can have multiple unique key in a table but primariy key is one and only one in a table.
4) primariy key in itself is unique key.
5) Primary Key Creates Clustered Index and Unique Key Creates NonClustered Index
6) Each table in database must has one primary key
7) No duplicate values are entered with unique key

--* A foreign key : is a column of a table that is the primary key of another table.

--* Microsoft SQL Server supports the following constraints:
1) PRIMARY KEY Constraints
2) UNIQUE Key Constraints
3) FOREIGN KEY Constraints
4) CHECK Constraints (>, <, <=, >=, <>, =)
5) NOT NULL Constraints
6) Default Constraints

--* What is Identity colomn?
1) It is used to automatically numbering a field based on the previous field values of existing rows
2) It is usually used for a primary key

--* What is the different between Batch and Script?
1) A batch is a set of Transact-SQL statements that are submitted together and executed as a group
2) Batches can be run as a part of script
3) script can include more than one batch of Transact-SQL statements.
4) If a syntax error exists in a batch, none of the statements in that batch executes.Execution begins with the next batch.
5) Use a GO statement at the end of a batch
6) A Script Is One or More Transact-SQL Statements Saved as a File Using the .sql Extension

--* Types of Transact-SQL Statements:
1) Data Definition Language (DDL) statements: which allow you to create objects in the database. (Create , Alter , Drop)
2) Data Control Language (DCL) statements, which allow you to determine who can see or modify the data. (Grand , Deny , REVOKE)
3) Data Manipulation Language (DML) statements, which allow you to query and modify the data. (Select , Insert,Update, Delete)

--* There are two type of comment in SQL Server:
1) In-line Comments (--)
2) Block Comments (/* lines */)

--* Types of Join in SQL Server:
1) Inner Join : displays only the rows that have matching rows in both the tables.
2) Outer Join: return all the rows from at least one of the table
3) Left outer join : returns all the records from left table and only matching records from right table.
4) Right outer join: returns all the records from right table and only matching records from left table.
5) A cross join or Full join: returns the sets of records from the two joined tables. If A and B are two sets then cross join = A X B.
6) Self Join : A table is join to itself in a self join.

--* What is difference between ExecuteNonQuery and ExecuteScaler?
1) ExecuteScalar will return only a single value i,e 1st row & 1st column.
2) ExecuteNonQuery executes commands and returns number of rows affected by Insert,Update & Delete operations

--* A Stored Procedure: Is a Precompiled Collection of Transact-SQL Statements

--* Stored Procedures Can:
1) Contain statements that perform operations
2) Accept input parameters
3) Return status value to indicate success or failure
4) Return multiple output parameters

--* Advantages of Using Stored Procedures:
1) Share Application Logic
2) Provide Security Mechanisms
3) Improve Performance
4) Reduce Network Traffic

--* A Trigger: Is a Special Type of Stored Procedure

--* Trigger can't be called directly and do not pass or accept parameters.

--* When i Excute Sql query the SQL do:
1) check if there is any syntix error in query
2) check the name of table in my database if exist or not
3) check colomn in my table if exist or not
4) Excute at the end

--* Where do you think the users names and passwords will be stored in sql server? They get stored in master database in the sysxlogins table

--* What is difference between ‘Count’ and ‘Count(*)’?
1) ‘Count’: Counts the number of non-null values in table
2) ‘Count(*)’: Counts the number of rows in the table, including null values and duplicates

--* What are the benefits of SQL ?
1. It is flexible, Powerful and easy to learn
2. It is a non-procedural language
3. It provides commands for a variety of tasks
4. All RDBMS supports SQL

--* What are the different types of caching :
ASP.NET has three kinds of caching strategies are
1) Output Caching : Caches the whole page
2) Fragment Caching : Caches a part of the page
3) Data Caching : Caches the data

--* How do I send e-mail from an ASP.NET application?using System.Web.Mail; MailMessage message = new MailMessage ();
message.From = ;message.To = ;
message.Subject = “Scheduled Power Outage”;
message.Body = “Our servers will be down tonight.”;
SmtpMail.SmtpServer = “localhost”;
SmtpMail.Send (message);

--* Access Modifier :They are public , protected , internal , internal protected , private

--* What is DTD in XML document ?
• DTD is Document Type Definition
• A DTD defines the legal elements of an XML document

--* What is an .ASP file?
It is a Text File that contains the combination of the following:
• Text
• HTML tags
• Script Commands

--* What is purpose of XPath ?
1) XPath is a language for finding information in an XML document.
2) XPath is used to navigate through elements and attributes in an XML document

--* What is a Static Class :
1) Static Class is a new feature in C# 2.0
2) It is a class that have a static keyword
3) All Static Class members are also static
4) Static classes cannot be instantiated
5) Static classes are sealed
6) Static members can use without create instance of it's class

--* The constructor of the base clase will be called before the constructor of the drived class

--* The protected member can be seen in the drived class

--* How do you deploy ASP.NET application?
You can deploy an ASP.NET Web application using any one of the following three deployment options.
1.XCOPY Deployment
2.Using the Copy Project option in VS .NET
3.Deployment using VS.NET installer

--* How many types of validation controls are provided in ASP.NET?
1) RequiredField Validator Control
2) Range Validator Control
3) RegularExpression Validator Control
4) Custom Validator Control
5) Validation Summary Control
6) CompareValidator control

--* List the types of Authentication supported by ASP.NET.
1) Windows authentication (default)
2) Forms authentication
3) Passport authentication
4) None or custom authentication (Security disabled)

--* Is it possible to see the code that ASP.NET generates from an ASPX file : Yes , By doing one of the following:
1) enabling debugging using a <%@ Page Debug="true" %> in page directive in the ASPX file
2) set a statement in Web.config

Question for Dot Net Interview (Part 4):

--* Difference between Serialization and Deserialization :
1) Serialization is the process of converting an object into a stream of bytes
2) Deserialization is the opposite process of creating an object from a stream of bytes

--* Difference between int and int32 ?
1) Both are same
2) System.Int32 is a .NET class
3) Int is an alias name for System.Int32.

--* What is the difference between superclass and subclass?
1) A superclass is a class that is inherited (base class)
2) A subclass is a class that does the inheriting. (drived class)

--* What are different types of directives in .NET?
@Page , @Control , @Import , @Implements , @Register , @Assembly , @OutputCache , @Reference

--* What is the difference between Value Types and Reference Types? --
1) Value type uses Stack to store the data
2) Reference type uses heap to store data
3) A data type is a value type if it holds the data within its own memory allocation4) A reference type contains a pointer to another memory location that holds the data.
5) Value types include the following:All numeric data types and Boolean, Char, and Dateand All structures, even if their members are reference types and Enumerations
6) Reference types include the following: String and All arrays, even if their elements are value types and Class types, such as Form and Delegates
7) Value types can not contain the value null.
8) Reference types can contain the value null

--* What's the difference between Response.Write() and Response.Output.Write()? Response.Output.Write() allows you to write formatted output

--* Does AJAX works with JAVA?
Yes it works

--* Should i use HTTP POST or GET for my AJAX calls? x
1) AJAX requests should use an HTTP GET request when retrieving data where the data will not change for a given request URL
2) An HTTP POST should be used when state is updated on the server.

--* Can you edit data in the Repeater control?
No, it just reads the information from its data source

--* Difference between Struct and Class :
1) Struct are Value type and are stored on stack, while Class are Reference type and are stored on heap.
2) Struct “do not support” inheritance, while class supports inheritance. However struct can implements interface.
3) Struct should be used when you want to use a small data structure, while Class is better choice for complex data structure.

--* The command is used to get back the privileges offered by the GRANT command is : RRVOKE

--* What is different between Constant and Read only :
1) Const = value assigned at Compile time and unchangeable once established.
2) readonly = value assigned at run time and unchangeable once established

--* What is difference between constants, readonly and, static ? x
1) Constants: The value can’t be changed
2) Read-only: The value will be initialized only once from the constructor of the class.
3) Static: Value can be initialized once.

--* WSDL : stands for Web Services Description Language

--* WSDL (Web Services Description Language) is a language that describes a web service. It contains information such as where you can find the web service, methods and properties that it supports, its data types, and the protocol used to communicate with the web service. WSDL is based on the XML format and it's used to create proxy objects. Basically, without a WSDL document, developers wouldn't be able to use web services simply because they wouldn't know which methods and properties they support and also which communication method any particular web service supports.

--* What is Partial classes ?
1) partial classes allow you to split the definition of a class into multiple files. 2) All partial classes must be written in the same language

--* Difference between dynamic query and static query :
1) when your query pass with value this is static query
2) when your query pass with parameter like(@user_name) this is dynamic query3) static query select * from TblEmpDetail where user_name='Kapil'
4) dynamic query select * from TblEmpDetail Where(user_name=@user_name)

--* what is the different between user control and custom control :
1) User Control is a page file with extension .ascx which can only be used within a single application.
2) custom controls are assemblies(dll files) that can be used in multiple applications.
3) User Controls cannot be added to the ToolBox of VS.NET . To use a user Control with in an aspx page u have to drag the user Control from the solution Explorer to designer page.
4) Custom Controls can be added to ToolBox of VS.NET.

--* Difference between windows and forms authentications:
1) if you window authentication it mean it is annonimas site every one can vesit it without login password.
2) if you Form authentication it means you should have username and password to visit pages

--* Difference between HTML and web Controls ?
1) HTML controls are static
2) Web controls are dynamic
3) HTML controls don't have runat server tag
4) Web controls have runat server tag