Exporting a Server Control into Microsoft Excel
public void DownLoad(string strFileName)
{
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Panel1.RenderControl(htw); // here u can use gridview, panel etc.....
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
public override void VerifyRenderingInServerForm(Control control) // u must put this method on the code behind page.
{
}
Wednesday, August 18, 2010
Sending Emails using Gmail User Account in Asp.Net
To send an email you can use System.Net.MailMessage class. The following example demonstrates how to send an email using your own Gmail user account.
MailMessage mailMsg = new MailMessage();
mailMsg.From =new MailAddress( yourmail@domain.com);
mailMsg.To.Add( jobinjohn@live.com);
mailMsg.Subject = "Hai";
mailMsg.Body = "Chumma";
mailMsg.Priority = MailPriority.High;
SmtpClient c = new SmtpClient("smtp.gmail.com",587); // or u can use port 465
NetworkCredential cred = new NetworkCredential("username", "password"); //ur account username and password to authenticate your request.
c.Credentials = cred;
c.EnableSsl = true;
try
{
c.Send(mailMsg);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
MailMessage mailMsg = new MailMessage();
mailMsg.From =new MailAddress( yourmail@domain.com);
mailMsg.To.Add( jobinjohn@live.com);
mailMsg.Subject = "Hai";
mailMsg.Body = "Chumma";
mailMsg.Priority = MailPriority.High;
SmtpClient c = new SmtpClient("smtp.gmail.com",587); // or u can use port 465
NetworkCredential cred = new NetworkCredential("username", "password"); //ur account username and password to authenticate your request.
c.Credentials = cred;
c.EnableSsl = true;
try
{
c.Send(mailMsg);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Tuesday, August 17, 2010
Working with Configuration Files in C#
Configuration
A common issue when developing applications is how to retrieve configuration information, such as database connection strings, event log names, or other parameters.
The System.Configuration namespace provides the functionality for reading configuration files.
The location for application settings depends on the type of application you are creating. For web applications, settings are stored in the web.config, in console or WinForms gui applications, settings are stored in app.config file.
appSettings Section
An application configuration file follows a specific XML schema. The appSettings section is a predefined section of the configuration file designed to make it very easy to retrieve a value based on a given name. This is the easiest way to add application-specific settings into an application configuration file. The appSettings section of the configuration file consists of a series of "add" elements with "key" and "value" attributes.
Eg: Create a console application and add app.config file.
< configuration >< appSetting s>
< add key="SourceFile" value="d:\test\source.txt" / >
< add key="DestinationFile" value="d:\test\destination.txt" />
< /appSettings>
< / configuration >
--------
string source = ConfigurationSettings.AppSettings["SourceFile"];
string dest = ConfigurationSettings.AppSettings["DestinationFile"];
Console.WriteLine(source);
Console.WriteLine(dest);
Or
NameValueCollection nvc = ConfigurationManager.AppSettings;
Console.WriteLine(nvc["SourceFile"]);
Console.WriteLine(nvc["DestinationFile"]);
Eg:
< connectionStrings >
< add name="MyConnetion" providerName="System.Data.SqlClient" connectionString="data source=.\sqlexpress;initial catalog=MYDB;integrated security=true" / >
< / connectionStrings >
string constr = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
Console.WriteLine(constr);
Or
ConnectionStringSettingsCollection coll = ConfigurationManager.ConnectionStrings;
foreach (ConnectionStringSettings cs in coll)
{
if (cs.ProviderName == "System.Data.SqlClient")
{
Console.WriteLine(cs.ConnectionString);
}
}
A common issue when developing applications is how to retrieve configuration information, such as database connection strings, event log names, or other parameters.
The System.Configuration namespace provides the functionality for reading configuration files.
The location for application settings depends on the type of application you are creating. For web applications, settings are stored in the web.config, in console or WinForms gui applications, settings are stored in app.config file.
appSettings Section
An application configuration file follows a specific XML schema. The appSettings section is a predefined section of the configuration file designed to make it very easy to retrieve a value based on a given name. This is the easiest way to add application-specific settings into an application configuration file. The appSettings section of the configuration file consists of a series of "add" elements with "key" and "value" attributes.
Eg: Create a console application and add app.config file.
< configuration >< appSetting s>
< add key="SourceFile" value="d:\test\source.txt" / >
< add key="DestinationFile" value="d:\test\destination.txt" />
< /appSettings>
< / configuration >
--------
string source = ConfigurationSettings.AppSettings["SourceFile"];
string dest = ConfigurationSettings.AppSettings["DestinationFile"];
Console.WriteLine(source);
Console.WriteLine(dest);
Or
NameValueCollection nvc = ConfigurationManager.AppSettings;
Console.WriteLine(nvc["SourceFile"]);
Console.WriteLine(nvc["DestinationFile"]);
Eg:
< connectionStrings >
< add name="MyConnetion" providerName="System.Data.SqlClient" connectionString="data source=.\sqlexpress;initial catalog=MYDB;integrated security=true" / >
< / connectionStrings >
string constr = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
Console.WriteLine(constr);
Or
ConnectionStringSettingsCollection coll = ConfigurationManager.ConnectionStrings;
foreach (ConnectionStringSettings cs in coll)
{
if (cs.ProviderName == "System.Data.SqlClient")
{
Console.WriteLine(cs.ConnectionString);
}
}
Monday, August 16, 2010
Create and alter Tables
Create and alter Tables:
A table is a repository for data, with items of data grouped in one or more columns. Tables contain zero or more rows of information.
Eg: Creating tables
CREATE TABLE Student
(
rollNo INT,
name VARCHAR(50),
mark1 INT,
mark2 INT
)
Altering tables:
If we need to change the definition of a table after creating it, we can use the ALTER TABLE statement.
We can use ALTER TABLE to add additional columns to a table, remove columns from the table, add and remove constraints, disable and enable constraints and disable and enable triggers.
Eg:
ALTER TABLE Student
ADD Total INT
ALTER TABLE Student
DROP COLUMN Total
A table is a repository for data, with items of data grouped in one or more columns. Tables contain zero or more rows of information.
Eg: Creating tables
CREATE TABLE Student
(
rollNo INT,
name VARCHAR(50),
mark1 INT,
mark2 INT
)
Altering tables:
If we need to change the definition of a table after creating it, we can use the ALTER TABLE statement.
We can use ALTER TABLE to add additional columns to a table, remove columns from the table, add and remove constraints, disable and enable constraints and disable and enable triggers.
Eg:
ALTER TABLE Student
ADD Total INT
ALTER TABLE Student
DROP COLUMN Total
Jagged array in C#
Jagged array:
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays".
Declaration: int[][] array;
Initialization: array=new int[3][];
array[0]=new int[2];
array[1]=new int[5];
array[2]=new int[3];
Eg:
using System;
class jaggedarray
{
int[][] ja;
public void Accept()
{
Console.WriteLine("Enter the size of jagged array :");
int jsize = Convert.ToInt32(Console.ReadLine());
ja = new int[jsize][];
for (int i = 0; i < jsize; i++)
{
Console.WriteLine("Enter size of Single dimension array ");
int ssize = Convert.ToInt32(Console.ReadLine());
ja[i] = new int[ssize];
Console.WriteLine("Enter {0} elements ", ssize);
for (int j = 0; j < ssize; j++)
{
ja[i][j] = Convert.ToInt32(Console.ReadLine());
}
}
}
public void Display()
{
Console.WriteLine("Jagged Array Is :");
foreach (int[] a in ja)
{
foreach (int n in a)
{
Console.Write(n + "\t");
}
Console.WriteLine();
}
}
}
class Mainclass
{
public static void Main()
{
jaggedarray obj = new jaggedarray();
obj.Accept();
obj.Display();
}
}
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays".
Declaration: int[][] array;
Initialization: array=new int[3][];
array[0]=new int[2];
array[1]=new int[5];
array[2]=new int[3];
Eg:
using System;
class jaggedarray
{
int[][] ja;
public void Accept()
{
Console.WriteLine("Enter the size of jagged array :");
int jsize = Convert.ToInt32(Console.ReadLine());
ja = new int[jsize][];
for (int i = 0; i < jsize; i++)
{
Console.WriteLine("Enter size of Single dimension array ");
int ssize = Convert.ToInt32(Console.ReadLine());
ja[i] = new int[ssize];
Console.WriteLine("Enter {0} elements ", ssize);
for (int j = 0; j < ssize; j++)
{
ja[i][j] = Convert.ToInt32(Console.ReadLine());
}
}
}
public void Display()
{
Console.WriteLine("Jagged Array Is :");
foreach (int[] a in ja)
{
foreach (int n in a)
{
Console.Write(n + "\t");
}
Console.WriteLine();
}
}
}
class Mainclass
{
public static void Main()
{
jaggedarray obj = new jaggedarray();
obj.Accept();
obj.Display();
}
}
Performing site navigation in Asp.Net
Performing site navigation
Page navigation is the process of moving between one actual page of your Web site and another.
There are many ways to navigate from one page to another in ASP.NET.
Client-side navigation: Client-side code or markup allows a user to request a new Web page. Your client code or markup requests a new Web page in response to a client-side event, such as clicking a hyperlink or executing JavaScript as part of a button click.
Cross-page posting: A control and form are configured to PostBack to a different Web page than the one that made the original request.
Client-side browser redirect: Server-side code sends a message to the browser, informing the browser to request a different Web page from the server.
Server-side transfer: Server-side code transfers control of a request to a different Web page.
Page navigation is the process of moving between one actual page of your Web site and another.
There are many ways to navigate from one page to another in ASP.NET.
Client-side navigation: Client-side code or markup allows a user to request a new Web page. Your client code or markup requests a new Web page in response to a client-side event, such as clicking a hyperlink or executing JavaScript as part of a button click.
Cross-page posting: A control and form are configured to PostBack to a different Web page than the one that made the original request.
Client-side browser redirect: Server-side code sends a message to the browser, informing the browser to request a different Web page from the server.
Server-side transfer: Server-side code transfers control of a request to a different Web page.
Subscribe to:
Posts (Atom)