1

I am a PHP developer but now I am learning asp.net with C# as it is a requirement of our company. I am familiar with the basics of C# and oops. The problem I am facing is that I learned how to retrieve values from the database and make a data table. Now from this I know that I can bind a grid control to display the data but this technique restricts me to do some of my own stuff to the datagrid. Now what I want is that I used to hard code the table in PHP if I needed to build a table like:

<?php
*/
retrive the data from the database and store it in an array using $row_sql = mysql_fetch_array() function
/*
....
....
?>
<table>
<tr>
<td><?php $row_sql['fieldname']; ?></td>
</tr>
....
....

</table>
?>

I know that I can do such a thing with a repeater but not sure that I want to use such a control or its just because that i am not so familiar with asp.net style.

So can anyone please guide me how can I do this? Or please tell me what should I do to learn asp.net if I know how to do it in PHP?

Update: Ok here is what I did but I am still getting an error.

Firstly I created a database by filling the dataset from dataadapter, then I used the code as suggested but I am getting the following error:

foreach statement cannot operate on variables of type 'System.Data.DataTable' because 'System.Data.DataTable' does not contain a public definition for 'GetEnumerator'

1
  • The easiest quickest most simple way to generate your table is to use a native asp.net gridview control. There are other ways too but I suggest looking into the gridview first. Commented Jan 15, 2012 at 22:26

2 Answers 2

1

You're not limited to using only the Grid controls at your disposal - you can generate your own markup by looping through the data rows in the specific data table. You can for instance do something like this:

<%
foreach (DataRowView dr in myDataTable.AsDataView())
{
    Response.Write("<tr>");
    Response.Write("<td>" + dr["Column1"].ToString() + "</td>");
    Response.Write("<td>" + dr["Column2"].ToString() + "</td>");
    Response.Write("<td>" + dr["Column3"].ToString() + "</td>");
    Response.Write("</tr>");
}
%>

That will loop through each data row in the data table and write out the contents of columns 1-3 (assuming their names are "Column1", "Column2" and "Column3", of course), each in their own TD, wrapped in a TR. But I'm sure you figured that out already...

My suggestion would be to rather implement an MVC framework (whether it be the ASP.NET MVC framework or your own), as it would be easier to adapt, coming from a PHP background.

EDIT I updated the code snippet to use the DataView type for enumeration... Just remember to include the System.Data namespace to use the "AsDataView()" extension method. Alternatively, you could get a DataView as follows:

<%
foreach (DataRowView dr in new DataView(myDataTable))
{
    // code...
}
%>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer I needed @FarligOpptreden.. I beleive that working with a framework like Zend makes your code more maintainable. and as for the method suggested above I will try it and get back to you. and if you can help me on one more thing... From where I can find some good tutorials on MVC. I don't want to be a bad programmer when it comes to .NET
Pleasure ankit! If my answer helped you and addressed your question, you might consider marking it as the answer. :)
sorry for the late reply but i am still getting some problem. I have created a datatable named abc and i am using that table in for each table as you suggested but i am getting the error like: foreach statement cannot operate on variables of type 'System.Data.DataTable' because 'System.Data.DataTable' does not contain a public definition for 'GetEnumerator'
You can also try using type DataView (and the corresponding DataRowView)... When looping through, just do: foreach (DataRowView row in myDataTable.AsDataView())
1

There are several ways you can accomplish this:

  1. Using GridView or similar severside control. For More look this example or this example
  2. Looping through data set and manually creating html. More like what you have been in PHP by mixing ASP.NET code with plain HTML. More like what @FarligOpptreden suggested

Something like:

        <%
           foreach (var row in myDataTable)
               {%>
              <tr>
                 <td> + <%=row[1].ToString()%> + </td>
                 <td> + <%=row[1].ToString()%> + </td>
                 <td> + <%=row[1].ToString()%> + </td>
                 .....
                 <td> + <%=row[n].ToString()%> + </td>
              <% }
             %> 

3 . Use table server control.

For someone with PHP experience I think number 2 will make more sense.

1 Comment

Thanks for the superb answer Emmanuel N and yes definitively the second solution is making more sense to me. I will give a try to the methods you suggested above and get back to you. Seriously thank you for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.