views:

35

answers:

4

Howdy! :D How are ya?

I'm a PHP developer initiating my studies of ASP.NET, out of necessity, and I would like to know the easier way to retrieve some data from DB into an array and use this array to write some HTML. In PHP I'd pull the data, then use a foreach() loop to write, for example, rows of a table. But I don't have idea of how I would do it in ASP.NET.

Could anyone give me a light? :DDD

Thanks in advance.

A: 

A simple code example is:

using(var connection = new SqlConnection("connection_string_here"))
{
    connection.Open();
    using(var command = new SqlCommand("SELECT * FROM dbo.table"))
    {
        using(var adapter = new SqlDataAdapter(command))
        {
            DataSet data = new DataSet();
            adapter.Fill(data);
            foreach(DataRow row in data.Tables[0])
            {
                foreach(DataColumn column in data.Tables[0].Columns)
                {
                    Response.Write("Column name: {0} Column value: {1}", 
                        column.ColumnName, row[column]);
                }
            }
        }
    }
}

This code does the following:

  1. Opens a connection to the database
  2. Gets the data from a table called dbo.table
  3. Iterates over every row in the table
  4. For every column in that row, it prints out the name of the column and the value for that column.
Rob
Intuitive code, but I really don't know where to use it. Like I said, beginner's noobness. :D Thank you for you time and sorry.
Bored Elf
A: 

I am not a PHP developer but if you just want to write HTML then, I think, there should not be a big difference between PHP and ASP.net. Just hook-up data from db and write HTML using foreach.

BUT

If you want to learn ASP.net then learn AsP.Net.

Good source is:

http://www.asp.net/get-started

Muhammad Kashif Nadeem
I don't wanna learn ASP.NET, actually. But the college thinks I need LOL.
Bored Elf
A: 

By the tags it seems that you're using MVC or MVC2. You can connect to a database using the Entity Framework (EF), which is a library that abstracts the connection to the database and creating class objects that represent database objects.

By using those objects you are able to retrieve the data from the database and use the foreach to navigate through the data.

There's a great step-by-step guide here that will help you get started.

Hope this helps.

Nuno Ramiro
I read about EF, but I'm not there yet. :D
Bored Elf
A: 

It's not clear which project type you've got - ASP.NET webforms or ASP.NET MVC. The question is tagged MVC, but you refer to code-behind.

In terms of webforms and displaying data from a database, its big advantage is its set of server controls. They help get away from the mindset of 'loop through an array/dataset and write out HTML tags'. You'll end up getting a LOT more done in the same amount of time compared to writing the looping code.

ASP.NET webforms offers databinding into common controls - dropdown lists, grids, etc.

If you're targeting ASP.NET webforms, consider this article and its sample code in getting you started with databinding.

p.campbell
I'm sorry. I thought it was the same thing, since both code behind and MVC pattern puts the logic in another file. :D It seems that ASP.NET is really a whole other concept with its very distinct idea. Thank you.
Bored Elf