Html Report Using Inline Code

Ankitsrist
Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 3949 red flag
Rating: 4 out of 5  
 1 vote(s)

In this Article lets see how to make Html reports using “Embedded code” or Inline code.

Introduction


In this Article lets see how to make Html reports using “Embedded code” or Inline code.

Server side code is not allowed directly in .aspx page(html code), so for that purpose we have to use “Inline server tags” which allows inline code or embedded code.

Server tags look like:- <% %>, your inline sever code goes inside this tags only.

Inline server code:-
Inline server code enables you to embedded server side code along with html code.
This is good approach to make html reports which fetch data directly from database tables
Firstly define class named dbclass, this class simply contains connection string and readdata function which defines how to fetch data from database table by using SqlDataReader

Dbclass.cs
public class dbclass


{
     public SqlConnection sqlcon = new SqlConnection();
     public SqlCommand cmd = new SqlCommand();   public SqlDataReader dr; public void myconnection()
        {
            sqlcon.Close (); // You need to access the project's connection string here.
sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["mycon"].ToString();
            sqlcon.Open();
        }
  //readdata function to read data from the database table
    public void readdata(string query)
    {         myconnection();         cmd = new SqlCommand(query, sqlcon);
 // Invoke ExecuteReader method.   dr = cmd.ExecuteReader();
    }
}

And then make one table and supply sample data

CREATE TABLE [dbo].[tbl_nri]


(
      [id] [int] IDENTITY(1,1) NOT NULL,
      [name] [varchar](200) NULL,
      [address] [varchar](500) NULL,
      [city] [varchar](50) NULL,
      [zip] [decimal](18, 0) NULL,
      [state] [varchar](50) NULL,
      [country] [decimal](18, 0) NULL
)

Write following html code in .aspx page
Htmlreport.aspx

<center>
        <table border=”1px”>
         <tr><td>name: </td><td></td> </tr>      
       <tr> <td>Address: </td><td></td></tr> 
       <tr> <td>City: </td><td></td></tr> 
        <tr><td>Zip: </td><td></td></tr> 
        <tr><td>State: </td><td></td></tr> 
        <tr><td>Country: </td><td></td></tr>
       </table>
</centre>

And finally the main part Inline or Embedded code which goes in .aspx page as follows
Htmlreport.aspx
 
<center>


        <table border=”1px”> <tr><td><%db.myconnection();
                                          db.readdata("SELECT [id],[ name],[address],[city],[zip],[state],[country] FROM [test].[dbo].[tbl_nri]");
                                          if (db.dr.HasRows)
                                          {
                                              while (db.dr.Read())
                                              {
// get the results of each column string name =db.dr["name"].ToString(); string address =db.dr["address"].ToString(); string City =db.dr["city"].ToString(); string Zip =db.dr["zip"].ToString();
string State =db.dr["state"].ToString(); string Country =db.dr["country"].ToString();
  %></td></tr>
         <tr><td>name: </td><td><%=name%></td></tr>        <tr> <td>Address: </td><td><%= address %></td></tr>       
        <tr> <td>City: </td><td><%= City %></td></tr>        <tr> <td>Zip: </td><td><%= Zip %></td></tr>        <tr> <td>State: </td><td><%= State %></td></tr>        <tr> <td>Country: </td><td><%= Country %></td></tr>
<tr>
<%}
     } %></tr>
       </table>
</centre>

The above inline coding is same as we generally writes in code behind file(.aspx.cs) by using SqlDataReader which reads database rows one-by-one. It reads in forward order from an SQL database. The SqlDataReader type can be used in a loop to read multiple rows from an SQL database
Hope this article is helpful
thanks
Page copy protected against web site content infringement by Copyscape

About the Author

Ankitsrist
Full Name: ankit saxena
Member Level: Starter
Member Status: Member
Member Since: 11/16/2012 11:33:51 PM
Country: India

http://www.dotnetfunda.com
hello, i have done engineering from I.T. branch, recently got placed in endroit technologies mumbai as a trainee engineer its my first company ...i was java developer but during job my interest towards asp.net has emerged :)

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)