Saturday, September 22, 2012

A Little More Fun with Datalist Controls

We have seen in the earlier post (atleast I have written it, don't know whether you have seen it or not), how the datalist iterates data from the database. In the earlier post I had taken the example of an airline code.

Now lets see something else. What you can do with a control within a datalist.

In this post I will make the airline code a link which will direct me to another page where the details of that airline will be available.

So let's get started....

First of all lets see the page which has the air_code as Link.

figure 1


Here you can see that inside the datalist "dal_practice" there is a linkbutton "lbl_aircode" which has an OnClick event "lnk_aircode_Click". This means that all the linkbuttons will point to the same event or method.

Now lets see the event and what does it do.

figure 2

Here you can see that there is a method "lnk_aircode_Click". Now when a link is clicked, this method will execute. I have created a new LinkButton. It will hold the reference of the link clicked. "sender", as you can see tells the compiler which link has been clicked.

Then we are using Response.Redirect() method. This will simply direct the user to the page "FlightDetails.aspx" with the reference of the link clicked. Here we have passed something like "?code=". This is simply a parameter that has been passed to the querystring.

You will understand this with the example below.

figure 3

In the image above I am clicking on the link "AF C105" which is an airline code. Now when a user clicks on it, he will be redirected to FlightDetails.aspx page which will have the code as AF C105.

Now when the user is redirected to  FlightDetails.aspx page the details of that flight will be shown as below.

figure 4


Above you can see the URL as FlightDetails.aspx?code=AF%20C105. 

Now lets see the design and the code and wrap up this post.

figure 5

I have declared five labels which will hold data from five different fields for a particular airline.

Let's see the code for it.

figure 6


I have written a query in which I am passing the airline code as parameter.

The data returned will be displayed in five different labels as shown above in figure 4.

I hope this article was quite simple and easy to understand.

Will be back with new post and some other control soon....Till then Happy Coding.....

Sunday, September 9, 2012

Datalist Control

In my previous post I wrote about repeater control.

We have a similar control to repeater i.e. a Datalist.

There is a difference in repeater control and a datalist control if you see coding wise. Datalist gives you an option of repeat direction i.e. if you want to repeat the items horizontally or vertically. Also, it gives you an option of into how many columns do you want your data to be listed.

There is one more difference between repeater and datalist which can be seen only in the view source of the page. Repeater generates only a span tag and datalist generates a table tag as you can see below. The above table tags are generated by datalist and the below span tags are generated by repeater control.



Business logic is similar to that of repeater.

Lets see it. Below is the design source code for datalist. There you can see that I have given repeat direction and repeat columns.


And lets see the business logic of  it below.


You can decide for yourself when to use repeater and when to use datalist.

Happy Coding!!!

  

Saturday, September 8, 2012

Repeater Control

Playing around with repeater controls is quite fun when you code.....Ofcourse you cant play with them at your workplace because your boss doesn't pay you for that....

Repeater as the name suggests it iterates itself i.e. it repeats itself.....

Let us take an example....You have a table in the database that contains the codes of airlines and you have to show it on your website....

What would be the first control that comes to Your mind? Gridview? Why GridView? Because it arranges the data in a tabular format.....That's right....

The  specialty that a repeater provides is that you can make the data from the database as a control from toolbox....I am sure you are confused about what is this that I am writing....I mean you can make the data a linkbutton or a hyperlink, etc.

Ok too much of talking...now let me show you how can a repeater help you.....



Above you can see I have added a repeater control to the html file.
That is it....now lets go to coding i.e. writing business logic for this repeater so that the data is bound to the repeater control.

I am assuming that you know how to create connection with the server....If not please refer to my earlier post http://codersandprogrammers.blogspot.in/2012/09/database-connection-in-webconfig.html....
I have a table in my database which gives information about the airlines..

In the repeater control I have added a label to which the airline code will be bound

I will list down the airline codes in this repeater control....The airline codes are not authentic....I have just created them for my practice....

Let's see the business logic for this...

Here you can see that I have created a connection and using that connection I have retrieved data from the database using a simple query "select air_code from OAAirline"....

Then using SqlDataReader  I have loaded the datatable which will become the data source for repeater control....

When rptr_practice.DataBind() is executed it refers to the label in the repeater control the text of which is bound from database with the column air_code....

You will see the output as follows....


Here you will see that all the codes are not properly aligned.....

This problem can be solved with a datalist which gives you formatting option as well as repeat Direction i.e. whether you want to repeat the data horizontally or vertically....

We will see the DataList in my next post.....



Database Connection in web.config

Let me show you how to give connection string in web.config and then use it in your .aspx pages or other classes.

Below is the webconfig where I have defined the connection string...





Above you can see that connectionstring has four things -:

1. Data Source - This is the name of the server where the application database will be hosted. I have written it as localhost because my database is on my local machine. Instead of writing localhost you ca simply write dot(.). That will also work fine.

2. Initial Catalog - This is the name of your database.

3. User Id - As the name suggests it is the username.

4. Password - As the name suggests it is the password.   

You give the connectionstring a name. You will refer to the connection string in your application by using this name. For example in my application I have named it as "Practice_ConnectionString".

And lastly you have the providername i.e. System.Data.SqlClient. This is important as this tells the application that it has to refer to the ms sql server.

Now we are done with adding the connectionstring in web.config. But hey, how to use it in the application...

Relax...I will show you how....Goto Default.aspx.cs in your application

Here you will include these three statements before continuing....

using System.Configuration;
using System.Data;
using System.Data.SqlClient;



Here above you will see that I have created a variable conStr  which will hold the reference to the connectionstring defined in the web.config.

After that I have initialized a new instance of SqlConnection class con  and passed the conStr  to it...

So here we are done with connecting application to database with connection string defined in web.config....

Happy Connecting!!!!









Monday, June 11, 2012

Welcome Programmers!!!

One evening I was just gazing at my computer screen trying to figure out what should I do in my spare time?

It clicked to me then, that I should start writing (since I like to type on my keyboard)....but "What"?

Thinking for a few minutes, I came up with the idea of blogging (Whatever knowledge that you possess, its always better to share it with others so that it can help them)....So here I am writing a blog.....

My blog title reads "Programmers"....Why so? I could have mentioned any particular language, but I kept it general....because I love to learn new Languages (computer languages...actually I am quiet a poor linguist)...and also I want that many should benefit from my blog.....

So you will have codes from many languages on this blog (atleast I will try to keep it that way)....

That's all....I won't bore you much with my first post since I want you to come back and read other posts that I write...So I thank you all to read about "Programmers" with patience and once again "Welcome"!!!!

Please Note - : Writing a blog does not make me an expert at programming and I am a learner as all of you are.....so criticisms and comments will be welcomed in a healthy way by me....and I assure you that I will crosscheck my articles thoroughly before posting...