Archive for August, 2007

Dynamic Progress page for ASP.NET

Thursday, August 16th, 2007

Example for create dynamic progress page for asp.net as follows.

private void Page_Load(object sender, System.EventArgs e)
{

Response.Write(”

    “);
    WebClient wc= new WebClient();
    for(int i= 1; i<40;i++)
    {
    byte[] b=wc.DownloadData(”http://www.tryangled.com”);
    Response.Write(”

  • tryangled solutions ” + i.ToString()+” Length: ” + b.Length.ToString());
    }
    Response.Write(”

“);
}

Posted By:V.Mahesh

Posted by Mahesh ( Tryangled )

How to select particular row in Gridview control?

Thursday, August 16th, 2007

Gets or sets the index of the selected row in a Grid View control.Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

Examples:

Code:[VB.NET]

<%@ Page language=“VB” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=“server”>

  Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    ‘ Get the currently selected row using the SelectedRow property.
    Dim row As GridViewRow = CustomersGridView.SelectedRow

    ‘ Display the company name from the selected row.
    ‘ In this example, the third column (index 2) contains
    ‘ the company name.
    Message.Text = “You selected “ & row.Cells(2).Text & “.”

  End Sub

  Sub CustomersGridView_SelectedIndexChanging(ByVal sender As Object, ByVal e As GridViewSelectEventArgs)

    ‘ Get the currently selected row. Because the SelectedIndexChanging event
    ‘ occurs before the select operation in the GridView control, the
    ‘ SelectedRow property cannot be used. Instead, use the Rows collection
    ‘ and the NewSelectedIndex property of the e argument passed to this 
    ‘ event handler.
    Dim row As GridViewRow = CustomersGridView.Rows(e.NewSelectedIndex)

    ‘ You can cancel the select operation by using the Cancel
    ‘ property. For this example, if the user selects a customer with 
    ‘ the ID “ANATR”, the select operation is canceled and an error message
    ‘ is displayed.
    If row.Cells(1).Text = “ANATR” Then

      e.Cancel = True
      Message.Text = “You cannot select “ + row.Cells(2).Text & “.”

    End If

  End Sub

</script>

<html  >
  <head runat=“server”>
    <title>GridView Select Example</title>
</head>
<body>
    <form id=“form1″ runat=“server”>

     <h3>GridView Select Example</h3>

     <asp:gridview id=“CustomersGridView”
       datasourceid=“CustomersSource”
       autogeneratecolumns=“true”
       autogenerateselectbutton=“true”
       allowpaging=“true”
       selectedindex=“0″
       onselectedindexchanged=“CustomersGridView_SelectedIndexChanged”
       onselectedindexchanging=“CustomersGridView_SelectedIndexChanging”
       runat=“server”>

       <selectedrowstyle backcolor=“LightCyan”
         forecolor=“DarkBlue”
         font-bold=“true”/>  

     </asp:gridview>

      <br/>

      <asp:label id=“Message”
        forecolor=“Red”
        runat=“server”/>

      <!– This example uses Microsoft SQL Server and connects  –>
      <!– to the Northwind sample database. Use an ASP.NET     –>
      <!– expression to retrieve the connection string value   –>
      <!– from the Web.config file.                            –>
      <asp:sqldatasource id=“CustomersSource”
        selectcommand=“Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]”
        connectionstring=“<%$ ConnectionStrings:NorthWindConnectionString%>”
        runat=“server”/>

    </form>
  </body>
 
</html>
 
 
 
Code:[C#.NET]
 
<%@ Page language=“C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=“server”>

  void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
  {

    // Get the currently selected row using the SelectedRow property.
    GridViewRow row = CustomersGridView.SelectedRow;

    // Display the company name from the selected row.
    // In this example, the third column (index 2) contains
    // the company name.
    Message.Text = “You selected “ + row.Cells[2].Text + “.”;

  }

  void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
  {

    // Get the currently selected row. Because the SelectedIndexChanging event
    // occurs before the select operation in the GridView control, the
    // SelectedRow property cannot be used. Instead, use the Rows collection
    // and the NewSelectedIndex property of the e argument passed to this 
    // event handler.
    GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];

    // You can cancel the select operation by using the Cancel
    // property. For this example, if the user selects a customer with 
    // the ID “ANATR”, the select operation is canceled and an error message
    // is displayed.
    if (row.Cells[1].Text == “ANATR”)
    {

      e.Cancel = true;
      Message.Text = “You cannot select “ + row.Cells[2].Text + “.”;

    }

  }

</script>

<html  >
  <head runat=“server”>
    <title>GridView Select Example</title>
</head>
<body>
    <form id=“form1″ runat=“server”>

     <h3>GridView Select Example</h3>

     <asp:gridview id=“CustomersGridView”
       datasourceid=“CustomersSource”
       autogeneratecolumns=true
       autogenerateselectbutton=true
       allowpaging=true
       selectedindex=“0″
       onselectedindexchanged=“CustomersGridView_SelectedIndexChanged”
       onselectedindexchanging=“CustomersGridView_SelectedIndexChanging”
       runat=“server”>

       <selectedrowstyle backcolor=“LightCyan”
         forecolor=“DarkBlue”
         font-bold=true/>  

     </asp:gridview>

      <br/>

      <asp:label id=“Message”
        forecolor=“Red”
        runat=“server”/>

      <!– This example uses Microsoft SQL Server and connects  –>
      <!– to the Northwind sample database. Use an ASP.NET     –>
      <!– expression to retrieve the connection string value   –>
      <!– from the Web.config file.                            –>
      <asp:sqldatasource id=“CustomersSource”
        selectcommand=“Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]”
        connectionstring=“<%$ ConnectionStrings:NorthWindConnectionString%>”
        runat=“server”/>

    </form>
  </body>
</html> 
Posted By: V.Mahesh


Posted by Mahesh ( Tryangled ) 				

Is there problem in maintaining session in subfolders?

Thursday, August 16th, 2007

session_save_path() returns the path of the current directory used to save session data.

use

session_save_path(”../”);

on the top of file in the subfolder one level down.

By this way the session can be maintained to all level of folders.

the other way is to change php.ini file . but most of servers will not allow to do so .

Courtesy : Research Result from Tryangled Solutions Pvt Ltd 

Posted by Suresh B

how to use trim function? (uses of trim function)

Wednesday, August 15th, 2007

trim( ) - removes the unwanted spaces from front and back of the string.

1. check for empty username and other controls in a form .

example:

<?

if(trim($_POST[unsername])==”")

echo “Username is empty”;

?>

Posted by Suresh B

What are Error Control Operator in php ?

Monday, August 13th, 2007

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

Note :

The “@” error-control operator prefix will disable error reporting for critical errors that will terminate script execution. This is a drawback .This means that if we use “@” to ignore errors from a  function or has been mistakenly typed, the script will die right at the same area with no indication or display.

Posted by Suresh B

What are the file system access methods in php?

Monday, August 13th, 2007

<?php  $fp = fopen(”tryangled.txt”, “w”);?>

If we use the program like above .. we can access the file by the following methods

r we can only read
r+ we can read and write
w we can write & create the file if it doesn’t exist in the directory
w+ we can read , write & create the file if it doesn’t exist in the directory
a we can only write , create if it doesn’t exist, and place a file position pointer at the end of the file (appends records to an existing file)
a+ we can reade and write, create if it doesn’t exist and place file position pointer at the end of the file.

Courtesy : http://www.tryangled.com

Posted by Suresh B

How to Save Sessions in a File?

Monday, August 13th, 2007

this works only if fsock is open :

<?php

session_register(”username”);
session_register(”password”);
session_register(”ordernumber”);

$username = “Goody”;
$password = “mypass”;
$ordernumber = “1234″;

$sessionfile = fopen(”sessionfile.txt”, “w”);
fputs($sessionfile, session_encode( ) );
fclose($sessionfile);

?>

how to retrive the session from a fiel ?

$sessionfile = fopen(”sessionfile.txt”, “r”);
global $username, $password, $ordernumber;
session_decode(fputs($sessionfile,  4096) );
fclose($sessionfile);

Posted by Suresh B

How to maintain sessions in php?

Monday, August 13th, 2007

How does Sessions Work in php?

Sessions in PHP are started by using the session_start( ) function.  Like the setcookie( ) function, the session_start( ) function must come before any HTML, including blank lines, on the page.  It will look like this:

<?php
session_start( );
?>The session_start( ) generates a random Session Id and stores it in a cookie on the user’s browser(this will be only  stored on the client browser.)  The default name for the cookie is PHPSESSID, although this can be changed in the PHP configuration files on the server. To reference the session Id in you PHP code, we reference the variable $PHPSESSID (it’s a cookie name; remember that from Cookies?)

We can think when we come to the second pass through our page and reach the session_start( ) the second time .  PHP knows that there is already a session is on progress and so that it  ignores many instances of the session_start( ) function.

How to Use Session Data?

As we have created a session, we can now create, store and retrieve information relating to that session.  Session variables are created by registering them for the session, using the session_register( ) function.  Here’s an example using session register:

<?php
session_start( );
?>
<html>
<head>

<title>we are using session register</title>

</head>

<body>

<?php

print “here is the session id: “;

print $PHPSESSID;

?>

<br />

<?php

session_register(”name”);

$name = “Suresh”;

print “this is your name in session: “;

print $user;

?>

</body>

</html>

The  name variable will have the value “suresh”.

Posted by Suresh B

How to handle Cookie in php?

Monday, August 13th, 2007

How to Creat a Cookie?

<?php
setcookie (”nameofcookie”, “here is the value of cookie”);
?>

How to Reading a cookie?

<?php
print “The value of cookie is  $cookiename”;
?>

Deleting a cookie

When cookies are created, they are set by default to be deleted when the users closes their browser window.  We can also override that by setting a time for the cookie’s expiration by the below example:

<?php
setcookie (”cookiename”, “value of the cookie”, time( ) + 3600);
?>

Posted by Suresh B

what are Server Response Codes?

Monday, August 13th, 2007
100-199 SRCs provide confirmation that a request was received and is being processed. (silent)
100 This is good. The request was completed and the process can move along.
101 Request to switch protocols (like from HTTP to FTP) was accepted.
200-299 SRCs report that requests were performed successfully. (silent)
200 It simply means all is OK. What the client requested is available
201 This means a new address was successfully created through a CGI or posting form data.
202 The client’s request was accepted, although not yet acted upon.
203 The accepted information in the entity header is not from the original server, but from a third party.
204 There is no content in the requested click. Let’s say you click on an image map section not attached to a page. This allows the server to just sit there waiting for another click rather than throwing an error.
205 This allows the server to reset the content returned by a CGI.
206 Only partial content is being returned for some reason.
300-399 Request was not performed, a redirection is occurring.(usually silent)
300 The requested address refers to more than one entity. Depending on how the server is configured, you get an error or a choice of which page you want.
301 Page has been moved permanently, and the new URL is available. You should be sent there by the server.
302 Page has been moved temporarily, and the new URL is available. You should be sent there by the server.
303 This is a “see other” SRC. Data is somewhere else and the GET
method is used to retrieve it.
304 This is a “Not Modified” SRC. If the header in the
request asks “If Modified Since”, this will return how long it’s been since the page
was updated.
305 This tells the server the requested document must be accessed
by using the proxy in the Location header (i.e. ftp, http.)
400-499 Request is incomplete for some reason.
400 There is a syntax error in the request. It is denied.
401 The header in your request did not contain the correct
authorization codes. You don’t get to see what you requested.
402 Payment is required. Don’t worry about this one. It’s not in
use yet.
403 You are forbidden to see the document you requested. It can
also mean that the server doesn’t have the ability to show you what you want to see.
404 Document not found. The page you want is not on the server nor has it ever been on the server.
Most likely you have misspelled the title or used an incorrect capitalization pattern in the
URL.
405 The method you are using to access the file is not allowed.
406 The page you are requesting exists but you cannot see it because
your own system doesn’t understand the format the page is configured for.
407 The request must be authorized before it can take place.
408 The request timed out. For some reason the server took too much time
processing your request. Net congestion is the most likely reason.
409 Conflict. Too many people wanted the same file at the same time. It glutted the server. Try again.
410 The page use to be there, but now it’s gone.
411 Your request is missing a Content-Length header.
412 The page you requested has some sort of pre-condition set up.
That means that If something is a certain way, you can have the page. If you get a 412,
that condition was not met. Oops.
413 Too big. What you requested is just too big to process.
414 The URL you entered is too long. Really. Too long.
415 The page is an unsupported media type, like a proprietary file made specifically for a certain program..
500-599 Errors have occurred in the server itself.
501 What you requested of the server cannot be done by the server. Stop doing that you!
502 Your server has received errors from the server you are trying to reach.
This is better known as the “Bad Gateway” error.
503 The format or service you are requesting is temporarily unavailable.
504 The gateway as timed out. This is a lot like the 408 error except the
time-out occurred specifically at the gateway of the server.
505 The HTTP protocol you are asking for is not supported.
Posted by Suresh B