Friday, March 28, 2014

Cookies in ASP.NET C#

To store website's data into browser we use cookies. Today in this example we will learn how to store cookies into your browser by using C# in your ASP.NET project.

For this you need to know about class HttpCookie. Its under System.Web. For more info you can go here.

First we will check how to store the cookies into browser.

So, open a new project, named it as Cookies_Demo and add a new form into it.

Now create a simple log in form like this one.

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <h1>Cookies Demo</h1>
        <asp:TextBox ID="txtEmail" runat="server" placeholder="Email"></asp:TextBox>
        <br />
        <asp:TextBox ID="txtPass" runat="server" placeholder="Password" TextMode="Password"></asp:TextBox>
        <br />
        <asp:CheckBox ID="ckbRememberMe" runat="server" Text="Remember me" />
        <asp:Button ID="Button1" runat="server" Text="Login" onclick="Button1_Click" />

It will look like..

Now on button click write the code to store cookies into the user's browser.

/* Store Cookies into browser here at login time */
                /* Place into Coockies */
                //Creting a Cookie Object
                HttpCookie _userInfoCookies = new HttpCookie("UserInfoDemo");

                //Setting values inside it
                _userInfoCookies["EmailDemo"] = txtEmail.Text.Trim();
                _userInfoCookies["PasswordDemo"] = txtPass.Text.Trim();
                //Adding Expire Time of cookies
                _userInfoCookies.Expires = DateTime.Now.AddDays(10);

                //Adding cookies to current web response

It will store your data into browser for 10 days as a name of  "userInfoCookies".

Now its time to get the data of cookies while we are opening the page. So write down the code in the Page_Load event.

if (HttpContext.Current.Request.Cookies["UserInfodemo"] != null)
                    string email = Request.Cookies["UserInfoDemo"]["EmailDemo"].ToString();
                    txtEmail.Attributes.Add("value", email);

                    string pass = Request.Cookies["UserInfoDemo"]["PasswordDemo"].ToString();
                else if (HttpContext.Current.Request.Cookies["UserInfoDemo"] == null)
                    txtEmail.Text = "";

It will check for the cookies present in the browser and if found then put the value into the text boxes. After this when you will run it you will get a form with value in the text boxes.

You can download the full source code from here.

Wednesday, March 26, 2014

Add own template in ASP project

Adding your own template is a very essential part of a web site. We fist design the template layout and then cut it according to portions and add this into the asp project. Now replace the controls with ASP controls. Like replace a text box with asp text box and add the class name into that asp textbox control.

Here, in this article I am going to show you something like this.

So first download a template first. You can download from here.

This is our template. Now create a new asp project and copy the css & image folder into solution explorer.And then add a new aspx page and named it as you need.

Now cut the html head portion and paste into aspx page head. And same in the body section.

Check out the form location and name. Its very important in asp. Add the class name into the form. Now replace the textbox and buttons with asp controls. Add the css class name and now then go to the designer view and by double clicking on the button create event to do process.

Now run your project and enjoy.

Download the source code now.

Sunday, March 23, 2014

Create PDF files in C#

Creating PDF files is a very important thing in web tech. You need to generate PDF files to show some result of user. To create you need to import few namespaces.

 using iTextSharp.text;
 using iTextSharp.text.pdf;
 using iTextSharp.text.html;
 using System.IO;
 using System.Text.RegularExpressions;
 using iTextSharp.text.html.simpleparser;

Now create a project and add  page named PdfDemo. Now copy bellow code and paste into the c# page of your created page.

    <form id="form1" runat="server">
    <div id="print" runat="server">
     This is the prining div......
     <br />
     ASP <i>With Arka</i>

And C# code is..

 Response.ContentType = "application/pdf";
 Response.AddHeader("content-disposition", "attachment;filename=demo.pdf");
 StringWriter sw = new StringWriter();
 HtmlTextWriter w = new HtmlTextWriter(sw);
 string htmWrite = sw.GetStringBuilder().ToString();
 htmWrite = Regex.Replace(htmWrite, "</?(a|A).*?>", "");
 htmWrite = htmWrite.Replace("\r\n", "")
 StringReader reader = new StringReader(htmWrite);
 Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
 string pdfFilePath = Server.MapPath(".") + "/PDFFiles";
 HTMLWorker htmlparser = new HTMLWorker(doc);
 PdfWriter.GetInstance(doc, Response.OutputStream);
 catch (Exception ex)

Now run your project and enjoy your pdf.
Download the source code here.

Friday, March 21, 2014

File uploading using FTP in C#

Sometimes we need to upload files into a directory of a different website from our site, even just yesterday I found this kind of problem. My actual site is in its own server, I wanted to upload files from another server(admin panel). So for that I found this way very useful. And now I am sharing my code with you.

On a button click, get the FileUploader to get the file name and the user-name, password of the ftp server.

Now write the following code into the Button_Click event.

Upload("""user-name""password", "<Your file name from FileUploader>");

And in the Upload method write down the code.

private static void Upload(string ftpServer, string userName, string password, string filename)
   using (System.Net.WebClient client = new System.Net.WebClient())
      client.Credentials = new System.Net.NetworkCredential(userName, password);
      client.UploadFile(ftpServer + "/" + new FileInfo(filename).Name, "STOR", filename);

If your website does not have a ftp server then open the parallel plux and create a ftp server with user-name and password, and then try to upload the files.

For more ftp upload info you can visit the MSDN website of Microsoft.

Ref :

Wednesday, March 5, 2014

AJAX Control Toolkit in ASP.NET

AJAX stands for Anonymous Javascript and XML. It helps to send data to server without refreshing the page. In visual studio it has given some predefine AJAX controls like Script Manager, Script Manager Proxy, Update Panel, Timer, Update Progress. But apart from all these there are many other tools of AJAX to use and make the application much more flexible. Such as is AJAX Toolkit. You can download this from here. And to day I will show you how to add this toolkit into your visual studio.

First download the toolkit from here. And then find the AjaxControlToolkit.dll from that folder. You an download as per your framework, like for visual studio download AJAX Toolkit 4.0 and for Visual studio

Tuesday, March 4, 2014

ASP default Login form using C#

To use the ASP default Login form you need to add a login control into your web form .

File uploading in ASP.NET using C# with file checking

File uploading is an important thing for any kind of websites, and in this post I will show you how to upload a file from your website to server using C#.

For this you need to add a control called FileUploder and a button. So take a web form and add a file uploader control with a button. For uploading files you need to use a method named  FileUpload.SaveAs. Here using this method upload the file into the desire path in the server.

Quiz software using C# (Windows application) with Ms. Access

In the previous post I had show you how to use Ms. Access as your database. Now in this article I will explain you how to create a Quiz software. First take a look about the database.

Here I have done all the program in just one form. And using panel I did all the hiding and showing data.

Table Question (tblQuestion)

  • ID (Number auto increment by 1)
  • Question (Text)
  • Option1 (Text)
  • Option2 (Text)
  • Option3 (Text)
  • Option4 (Text)
  • Answer (Text)

MS. Access connection with C# in windows or web application

Ms. Access is very useful database for the standalone application, where you need to run the program without installing the database in user's machine like a quiz or any other examination system. So lets have a look how to code the program to connect your software with Ms. Access database.

If you have done previously to connect the SQL server and done fetching or retrieving data from database it is more or less same for you. Everything is same except the connection string.

The Ms. Access connection string is here for you, with example of how to run a query(get or insert) in web application or windows application using of  C#.

You must hear about the OleDbConnection in .NET, we are going to use this one to connect your Ms. Access database.

Popular Posts