Wednesday, March 30, 2005

Exporting DataGrid values to Excel and Word

This is a simple code to show how to export datagrid values to Microsoft Excel spread sheet:

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
DataGrid1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
This shows how to export datagrid values to word file:
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
DataGrid1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

Tuesday, March 29, 2005

Sending arrayList using Profile object in Asp.net 2.0

Profile object is really cool in Asp.net 2.0 in which you can place anything about the user.

You can even place an arrayList inside the Profile object something like this:


System.Collections.ArrayList aList = new System.Collections.ArrayList();
aList.Add("Azam");
aList.Add("John");
aList.Add("Saif");
Profile["Name"] = aList;


Don't forget to add a type attribute in your web.config so that Asp.net will know the type of object you are putting inside the Profile object.



name="Name"
type="System.Collections.ArrayList"
defaultValue="??"
allowAnonymous="true" />



if you don't specify the type object you will get an error.

Thursday, March 24, 2005

Grid layout and flow layout in Asp.net 2.0

The first thing I did when I got Asp.net 2.0 was to drag something on the form and runat. To my amazement I found that by default the layout is set to flow instead of grid. So, then I had to click on my server control and than click on the layout and select the position to absolute which set it free to be being used as a Grid Layout.

But FlowLayout is a better option that is introduced in Asp.net 2.0 the reason is that when you have grid layout and transfer your pages to another machine everything seems to be out of proportion. By using FlowLayout everything remains intact and it also helps using Asp.net pages running on different browers.

Tuesday, March 22, 2005

SqlDataReader is readonly now I understand !

I was trying to return a parameter from a Stored procedure and I was performing this using the following lines of code:

SqlDataReader reader = myCommand.ExecuteReader();

// after few lines

int myID = Convert.ToInt32(myCommand.Parameters["@myID"].Value);

@myID was the output parameter added to the myCommand object.

Guess what ! myID always returned zero. Then I found out that you cannot return a parameter from the stored procedure if you are using SqlDataReader.

Later I used this code:

myCommand.ExecuteNonQuery();

and it worked like a charm.

:)

Sunday, March 20, 2005

My Email Server Control

I just developed a very simple email control which you can use to send emails.

The control can be downloaded with complete source code on my website at:

http://www.azamsharp.net/ServerControls.html

CDO Object mail error

For those of you who tried sending mail using Asp.net MailMessage class you must have encountered this error message.

"CDO Object could not be found".

There are many ways of resolving this error but here is what I did.

First make sure you include the server name. If you are sending mail from your computer than the server will be "localhost".

MailMessage mail = new MailMessage();
mail.To = _emailTextBox.Text;
mail.From = "testmail@tester.com";
mail.Subject = "Hello";
mail.Body = "Hello World";
try
{
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(mail);
_button.Text = "Mail Sent";
}
catch(Exception ex)
{
_emailTextBox.Text = ex.Message;
}


Also go to IIS and at the end you will see the Smtp settings. Right click on the "Default Smtp Virual Server" and select properties. Click on Access and than click on relay. Here you can either add the ip address or reject the ip address of any computer. I just entered one random ip address which is not allowed to see my website. You can add "127.0.0.1" in the option that says this computer can access the website.

Also click on the security tab and add the ASPNET user this will give the ASPNET user operator privilages to send emails.

Encrypting and Decryption the connection string

Most of use saves the connection string in the Web.Config file in plain format which is not secure enough.

Here is a little piece of code that you can use to encrypt and decrypt the connection string.

Encryption of the connection string:

private string EncryptConnectionString(string connectionString)
{
Byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(connectionString);
string encryptedConnectionString = Convert.ToBase64String(b);
return encryptedConnectionString;
}

Once the connection string is encrypted you can put it in the web.config file manually.


Descryption of the connection string:

private string DecryptConnectionString()
{
Byte[] b = Convert.FromBase64String(ConfigurationSettings.AppSettings["ConnectionString"]);
string decryptedConnectionString = System.Text.ASCIIEncoding.ASCII.GetString(b);
return decryptedConnectionString;
}



Friday, March 18, 2005

Asp.net 2.0 Codename Whidbey

Like all the .NET developers I am also eagerly waiting for the Asp.net 2.0 . I read on msdn about some of the new features of Whidbey like:

1. Master Pages: You can create a master page and that page will be used as a template for the other pages. So, you can have copyright information and images in the master pages and all other pages will have the same.

2) Profile class: Profile class is like a session object with one exception that if persists at the application level. You can store string, int and also object types in the Profile class. Mainly its used to keep information of a single user in his profile.

3) Skins: Skins will make it easier for developers to change the appearance of the server controls on the page.

4) New Controls: I have heard that there will be 40 new controls added to the toolbox.

This is pretty cool and I can hardly wait for its release !





Wednesday, March 16, 2005

New Project has been uploaded ( Complete Source Code)

I have just uploaded my new project which is the extension or the conversion of my personal website www.azamsharp.net in C# and asp.net.

You can download the full source code of the projects. Screen shots are also available on the website. The project uses Microsoft Data Access Application Block 2.0 for accessing database.

Below is the direct link to the Project page:

http://www.azamsharp.net/SharpConversion.html

Insertion, Deletion, Updation Messages

Whenver we insert, delete, update any field in the database we usually tell the user that the operation is successfull. Most of us do it like this:

bool isInserted = myClass.InsertData(firstname,lastname);

if(isInserted)
{
prints inserted on the label
}
else
{
prints not inserted
}

Even though this is the common approach that is being used by most of the developers. One can easily save alot of coding by making a Messages class whose main purpose is to display messeges depending on the operation perform by the user.

DBMessages class can contain static methods like this:

public static string IsInserted(bool isInserted)
{
if(isInserted)
{
return "Data Inserted";
}
else
{
return "Data not inserted";
}
}

In the calling program you can access this easily by using a single line of code:

bool isInserted = MyClass.InsertData(firstname,lastname);
lblMessage.Text = DBMessages.Inserted(isInserted);

Using this approach saves alot of coding and you can easily modify the messages by just editing them on one place rather going to each module.