ASP.NET How to work with Repeater

Today we are going to see how to work with Repeater Control.

Here is an example how are we going to get a repeater after we finished coding.

Here I am using AdventureWorksDW2008R2 in SQL SERVER which can be downloaded from http://www.codeplex.com.

The table I am using is DimCustomer and fields are :

1)FirstName

2)MiddleName

3)LastName

4)BirthDate

5)CustomerKey

Step 1 :  Upload the control in the page in .aspx side let it be default.aspx

<form id=”frmExample” runat=”server”>

<asp:Repeater ID=”rptList” runat=”server” OnItemDataBound=”rptList_ItemDataBound”>

<HeaderTemplate>

<table border=”1″ cellpadding=”0″ cellspacing=”0″>

12px; color:White;”>

<th>

<asp:Label ID=”Label14″ Text=”Login” runat=”server”></asp:Label>

</th>

<th>

<asp:Label ID=”Label1″ Text=”First Name” runat=”server”></asp:Label>

</th>

<th>

<asp:Label ID=”Label2″ Text=”Last Name” runat=”server”></asp:Label>

</th>

<th>

<asp:Label ID=”Label3″ Text=”Date Added” runat=”server”></asp:Label>

</th>

<th>

<asp:Label ID=”Label6″ Text=”Delete” runat=”server”></asp:Label>

</th>

</tr>

</HeaderTemplate>

<ItemTemplate>

<tr>

<td>

<asp:LinkButton ID=”lbnLogin” Text='<%# Eval(“FirstName”) %>’ runat=”server”</asp:

OnClick=”lnkClient_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>

LinkButton>

</td>

<td>

<asp:LinkButton ID=”lbnFirstName” Text='<%# Eval(“MiddleName”).ToString() %>'</asp:

runat=”server”

OnClick=”lnkClient_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>

LinkButton>

</td>

<td>

<asp:LinkButton ID=”LinkButton2″ Text='<%# Eval(“LastName”).ToString() %>'</asp:

runat=”server”

OnClick=”lnkClient_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>

</asp:LinkButton>

</td>

<td>

<asp:LinkButton ID=”LinkButton1″ Text='<%# string.Format(“{0:d}”, Eval(“BirthDate”)) %>’ runat=”server”

OnClick=”lnkClient_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>

</asp:LinkButton>

</td>

<td>

<asp:LinkButton ID=”lbnDelete” Text=”delete” runat=”server”

OnClick=”lbnDelete_Click” CommandArgument='<%# Eval(“CustomerKey“).ToString() %>’>

</asp:LinkButton>

</td>

</tr>

</ItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

</asp:Repeater>

</form>

NOTE: Dont forget to make the form tag as runat = “server “

otherwise an error will occur because you are trying to insert the data from .cs

side but u can only access it when u make runat = “server “

STEP 2:  Now insert the data from the code behing

int Page_Load event

SqlConnection con = new SqlConnection(“Server=localhost;UID=xx;PWD=xx;

Database=AdventureWorksDW2008R2″);

string sSQL = “Select top 5 * from dbo.DimCustomer”;

SqlCommand cmd = new SqlCommand(sSQL, con);

con.Open();

SqlDataReader dtrClient = cmd.ExecuteReader();

rptList.DataSource = dtrClient;

rptList.DataBind();

con.Close();

/*along with it*/ write these dummy function later you can implement them as you like .

protected void lnkClient_Click(object sender, EventArgs e)

{ }

protected void lbnDelete_Click(object sender, EventArgs e)

{ }

protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)

{ }

The last 2 lines binding the data to the repeater and starting remaining lines is to establish connection to database.

the output will come as shown above .

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

Advertisements

What is HTTP

HTTP Stands for Hypertext Transfer Protocol.

It is basically used to communicate web pages to web browsers,(HTTP) is used by web servers.When your browser connects to a web server, requests a web page from the server, and downloads the page,HTTP is used. It is a common standard that enables any browser to connect to any server, anywhere in the world.

HTTP was originally designed by Tim Berners-Lee to support the special demands of web communications and with an emphasis on efficiency, and a target page load time of under a tenth of a second. Modern Internet networks can support this type of response provided the page isn’t too large and the server is too far away.

To download different parts of a web page, downloading the text, graphics, and other objects on the page in different orders,your browser can open more than one HTTP connection at once. That is why you may see the status messages in the bottom of your browser window switch between a message like “downloading 38% of 47K” to “downloading 62% of 15K” on the same page — there are different HTTP connections.

To specify the download priorities,some browsers enable you so that, for example, graphics are not loaded, text is loaded before graphics, everything is loaded at once, etc and check your individual browser preferences

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

DOT NET – What is a cookie

Recently I got a message from one of the reader of my site that what exactly is a cookie. So I decided to wire a post dedicated to it .

  1. A cookie is name=value pair like variable which is stored as a file(persistant cookie) and which travels automatically between every request and response.
  2. Cookies are based on domains(website). As long as user is working with current web site cookie will be available.
  3. Cookies are maintained by browser but not by server which means cookies based websites are dependent on client.
  4. Cookies can be created as
    (i) In-memory cookie
    (ii) Persistant Cookie

An In-memory cookie means as long as user is working with app it will be available – also called as Session Cookie.

Persistant cookie means it will be stored in client physical location and available online as well as offline.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

C Sharp (programming language) and History

C# is an object-oriented programming language developed by Microsoft as part of the .NET initiative and later approved as a standard by ECMA and ISO . Anders Hejlsberg leads development of the C# language, which has a procedural, object-oriented syntax based on C++ and includes influences from aspects of several other programming languages (most notably Delphi and Java) with a particular emphasis on simplification.

History

During the development of .NET, the class libraries were originally written in a language called Simple Managed C (SMC). In January 1999, Anders Hejlsberg formed a team to build a new language at the time called Cool. By the time the .NET project was publicly announced at the July 2000 Professional Developers Conference (PDC), the language had been renamed C# and the class libraries and ASP.NET runtime had been ported to C#.

C#’s principal designer and lead architect at Microsoft is Anders Hejlsberg, who was previously involved with the design of Visual J++, Borland Delphi, and Turbo Pascal. In interviews and technical papers he has stated that flaws in most major programming languages (e.g. C++, Java, Delphi, and Smalltalk) drove the fundamentals of the Common Language Runtime (CLR), which, in turn, drove the design of the C# programming language itself.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )

Dot Net – How to upload excel sheet into databse using Dataset in ASP.NET

I thought of writing this blog post because there can a number of occasion where in you might need to upload something from word file or excel sheet or it can be any kind of  file need to be inserted into the database what we can do many things.

But probably what I feel good and follow most of the time is doing this way so that we can preserve the statements for future reference what we have actually used and where it is going to exactly affect.

Step 1: Create  a website and then add default page on to it.

Step 2: Write the following code in Page_Load(object sender, EventArgs e) function

DataSet ds = new DataSet();

OleDbConnection myCon = new OleDbConnection(@”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=folderpath\sheet.xls;Extended Properties=Excel 8.0;”);

OleDbCommand myComm = new OleDbCommand(“select * from [sheet1$] where State<>””, myCon);

OleDbDataAdapter da = new OleDbDataAdapter(myComm);

da.Fill(ds);

for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )

{

Response.Write(“insert into dbo.Temp2 values(‘” + ds.Tables[0].Rows[i][0].ToString() + “‘,'” + ds.Tables[0].Rows[i][1].ToString() + “‘,'” + ds.Tables[0].Rows[i][2].ToString() + “‘,'” + ds.Tables[0].Rows[i][3].ToString() + “‘)<br />”);

}

myCon.Close();

Step 3: Create a table in SQL Server names Temp2 where in u want insert the excel sheet into.

NOTE: The table name can be any name  you like its just an example never take it as standard, its for learing.

create table Temp2(Id varchar(10),country varchar(100),natAnimal varchar(50),lang varchar(50))

Step 4: Create or if u have  excel sheet just make sure that that contains header for each column it will be easy to understand

EX:

State Country Animal Language
1 Australia Kangroo English
2 India Peacock Hindi
3 USA American Biston English

This is a sample excel sheet name it as sheet and inside as Sheet1 it will be applied by default so just hit the save button.

So all set!!!

now save the excel sheet in the folder of your website and run the web site the expected Output will be something like this will appear in your broswer

insert into dbo.Temp2 values(‘1′,’Australia’,’Kangroo’,’English’)

insert into dbo.Temp2 values(‘2′,’India’,’Peacock ‘,’Hindi’)

insert into dbo.Temp2 values(‘3′,’USA’,’American Biston’,’English’)

then copy it and paste in ssms (SQL server Query Editor and hit F5 to execute) Finally records are inserted into the database.

There can be many more ways I am sure but this is also handy when you have production and development section seperate.

Reference : Dilip Kumar Jena ( https://mstechexplore.wordpress.com )