Welcome to my website

Read and Write to Text Files in c#


There are many ways to read and write to text files. We will see one option here. Be sure to add using System.IO namespace.
This is a function to write to a Text File.
private bool writeToTextFile()
    {
        bool success = true;
        //path and filename
        string fileName = Server.MapPath("abc.txt");
        //data to be written in the file
        //\r\n is to insert a new line
        string data = "This is the first line \r\n" +
                      "This is the second line \r\n\r\n" +
                      "This is the third line";
        try
        {
          File.WriteAllText(fileName, data);//write the data to the text file
        }
        catch (Exception e)
        {
            Response.Output.Write(e.Message);
            success = false;
        }
        return success;
    }
In the above function,\r\n is used to insert a new line.
This function is used to read from text file.
 
      private bool readFromTextFile()
    {
        bool success = true;
        //path and filename
        string fileName = Server.MapPath("abc.txt");
        try
        {
            string data = File.ReadAllText(fileName);//read from text file
            data = data.Replace("\r\n", "");//\r\n is replaced by
            Response.Output.Write(data);           
        }
        catch (Exception e)
        {
            Response.Output.Write(e.Message);
            success = false;
        }
        return success;
    }

Open the webpage in new window


On Button click open the webpage in new window

Create the button using the code below
<asp:Button runat="server" id="printPreviewBtn" Text="Print Preview1"  />

On the click of a button if you want to open the resulting webpage in a new window write the following code in Page_Load event.

string url = "dload.aspx ";
string fullURL = "window.open('" + url + "', '_blank', 'height=500,width=800,status=yes,toolbar=no,menubar=yes,location=no,scrollbars=yes,resizable=yes,titlebar=no' );";
printPreviewBtn.Attributes.Add("OnClick",fullURL);

In the above code
height and width refer to the height and width of the window in which the website will open. We can change it to any suitable values.
status refers to wheather we want the status bar on our new window.It can take two values where ‘yes’ means to show the status bar and ‘no’ means to hide the status bar.
toolbar refers to wheather we want to display the toolbar(like goole toolbar etc.) on our new window. It can take two values where ‘yes’ means to show the toolbar and ‘no’ means to hide the toolbar.
menubar refers to wheather we want to display the menubar(file, edit, view, insert etc) on our new window. It can take two values where ‘yes’ means to show the toolbar and ‘no’ means to hide the toolbar.
location here we can specify the locations in terms of x and y points where we want the window to be displayed.
scrollbar refers to wheather we want the scrollbar to be shown for the window. It takes two values where ‘yes’ means to show the scrollbar and ‘no’ means to hide it.
resizable refers to wheather we can resize the window. It can take two values where ‘yes’ means to show the scrollbar and ‘no’ means to hide it.
titlebar refers to wheather we want the titlebar to be shown for the window. It takes two values where ‘yes’ means to show the titlebar and ‘no’ means to hide it.

On Hyperlink click open the webpage in new window.

We have to set the Target property to _blank.This will cause the webpage to open in a new window.
  <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default2.aspx" Target="_blank">HyperLinkasp:HyperLink>

Various values for Target and their meaning as given in MSDN are as follows
_blank
Renders the content in a new window without frames.
_parent
Renders the content in the immediate frameset parent.
_search
Renders the content in the search pane.
_self
Renders the content in the frame with focus.
_top
Renders the content in the full window without frames.

Custom Validation Control


Consider a case where we have a text box with AJAX Calendar Extender to accept dates from the user. We have to restrict the dates entered by the user based on two conditions.
1)      If the date entered is less than 90 days I have to Display the error message “Please select a date >=” (Today’s date – 90 days )
2)      If the date enter is future date I have to display the error message “Information is not available for future dates”
Create an Ajax Enabled website
Add a text box, Ajax calendar extender and a custom validation control


<asp:TextBox ID="TextBox1" runat="server" >asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1">
cc1:CalendarExtender>
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidateFun"                 ControlToValidate="TextBox1" ErrorMessage="CustomValidator"> asp:CustomValidator>

JavaScript function
<script type="text/javascript" language="javascript">
    function ValidateFun(source, arguments)
    {
        //get the date that user has entered
        var DateChooser = document.getElementById("TextBox1");
        var dt = new Date(DateChooser.value);
        //today's date
        var today = new Date();
        //valid date i.e. today's date - 90 days
        var validDate = new Date();
        validDate.setDate(today.getDate() - 90);
        //calculate the number of milliseconds in 1 day
        var one_day = 1000*60*60*24;
        //calculate the difference in days for the value entered by the user and today's date      
        var diff = Math.floor((today.getTime() - dt.getTime())/(one_day));        
          if(diff > 90)// if the date entered is beyond 90 days from today
          {
            source.innerText = "Please enter a date >= " + (validDate.getMonth()+1)+"/"+validDate.getDate()+"/"+validDate.getFullYear();
            arguments.IsValid = false;
          }        
          else if(diff < 0)// if we entered a future date
          {
                source.innerText = "Information is not available for future dates";
                arguments.IsValid = false;
          }
          else
                arguments.IsValid = true;      
    }
    script>

How to redirect from current page to other page in Asp.Net Page 2

A Toolbox appear as below figure, you see 9-tabs in toolbox you see button control in standard tab. I will explain all controls in furthur classes. 
Add Two textboxes from same tab as shown below



Adding New-Item ( New Page for Inbox Page )
                          Go to solution explorer right-click on the project click on Add-New Item, Select web-form from the templates window. Name=Inbox click on "ADD" button.

Go to default web page button-click on Button you see a page look as below

In the button Click event you need to add code as below to redirect to our Inbox page

protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "abc" && TextBox2.Text == "123")
{
Response.Redirect("inbox.aspx");
}}

Now its ready to run program. Before running as per implementing project the home page is default web page, to set the home page

Go to solution Explorer right click on the default web-page click on set as start page.

To run press F5(to debug)
you see as shown in below picture
So enter abc and 123 to get your Inbox page and click on button and you see our first page redirected to inbox page as shown below


If you want you can add some text on your inbox.aspx page. double click on your inbox.aspx page you see coding part there you see inbox.aspx.cs just add given code below

protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Welcome to inbox page");
}

Back to First Page
See you in Next Topic

How to redirect from current page to other page in Asp.Net

by Emmaneale Mendu

Redirect: This method is used to call web-pages from the client machine, while calling the web-pages the client page posting information to the server machine, if calling web page located in the same physical directory. we have to refer only web-page name in the redirect method, when web-pages having in the different physical directories we have to give virtual path for calling the web pages.

Example re-direct Method::


To create your Login Page Open File === select New and select website as you see below





There you see New window select Asp.Net website in templates options nad see location and language are same as shown below


Press OK an you see a source code, dont worry about the html code. select design mode right center-below and add a button control from toolbox 

What is IIS Webserver

by Emmaneale Mendu
Internet Information services webserver:
                   The ASP.Net is server side web specification, In the ASP.Net we can work with IIS web server, The web server maintains collection of proxy machines. Very flexibly, Depending on the request the web server gives response, The total web application controlled by web server.

                   Webserver features are not getting with operating system manually we have to install IIS webserver.

Installing IIS Webserver:
   To install the web-server go to add remove programme. By clicking on start menu, settings control panel, click on the Add/remove programs. From the Add/remove programs click on Add/remove windows components tab, getting list of components from the operating system. From this list of components select internet information services checkbox. Click on the next button, web server installed.

Virtual directory:
       Virtual directory is logical linked between physical directory and webserver memory, All virtual directories are located in the webserver memory, All physical directories are located in the server machine memory, using one virtical directory. we can communicate with only one physical directory.

Creating virtual directory
      Create physical directory named with "yahoo" right click on the yahoo folder click on properties select websharing tab. select share this folder radio button.
     Alias name="My yahoo"-->OK, created virtual directory
    When we are installing IIS web server automatically created folder in the administrative tools named with internet information services, All virtual directories are located in the IIS folder.

What is Asp.Net

by Emmaneale Mendu
Active Server Pages . Networking enterprise tool

              Asp.Net is server side specification, Using Asp.Net we can implement entire architecture projects, In the Asp.net the microsoft encluded object-oriented programming rules, Asp.net supports by default http high level protocol, in the Asp.Net we can access internet information services web server(IIS).

Note: Rules available in ASp.Net(No Features)

A specification specifies set of rules to implement the technology, for implementing ASP.Net specification rules we have to integrate with .Net languages.



Protocols:
     The protocols are used to transfer the data, the protocols we can classify into two types
1)Low Level protocols
2)High Level protocols

Low Level protocols:
       All desktop applications by default depending on the low level tcp/ip protocol, all low level protocols are stateful protocols remembers all requests and response values, In the low-level values the Network consumption is more decreasing the application performance, The low-level protocols are suitable for implementing single tire architecture projects, two tire architecture projects, three tire architecture projects

High level protocol: All web specifications by default depending on the http high level protocol, all high level protocols are stateless protocols, remembers only current request and current response values. In the high level protocols Network consumption is very less So,high level protocols increasing the application performance, High level protocols are suitable for implementing entire architecture projects.