Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • mmmoustache 50 posts 79 karma points
    Jun 12, 2012 @ 16:59
    mmmoustache
    0

    Iterate through MySQL table data with razor

    Hi All,

    I'm trying to display data from a MySQL table onto a webpage and would like to iterate through each row using razor (and/or .NET). So far I've successfully connected to the database but I would then like to create a foreach loop (or the equivalent) so that I can render out the table data in the desired format and retrieve the different columns of each row.

    Here is my current code, where the foreach causes the script to error (" 'MySql.Data.MySqlClient.MySqlConnection' does not contain a definition for 'Query' "):

    @using MySql.Data.MySqlClient;
    @{
        MySqlConnection con new MySqlConnection("server=localhost;database=database;user id=user;password=password");
        con.Open();
      
        string sql "SELECT * FROM table";
        MySqlCommand cmd new MySqlCommand(sqlcon);  
      
        @foreach(var row in con.Query(sql){
            <ul>
                <li>@row.name - @row.description</li>
            </ul>
        }
        con.Close();
    }

    Is this possible to do in Umbraco without the aid of premium plugins? What needs to replace the foreach loop?

    Kind Regards,
    mmmoustache

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 12, 2012 @ 17:14
    Tom Fulton
    0

    Hi,

    I don't think it's anything to do with Umbraco - the error is stating that your con variable (a MySqlConnection object) does not have a method called Query.

    Are you sure that's a valid method?

    This might be another way to do it.

    Thanks,
    Tom

  • mmmoustache 50 posts 79 karma points
    Jun 12, 2012 @ 18:37
    mmmoustache
    0

    Okay, thought that might be the case. Cheers for the info anyway Tom.

Please Sign in or register to post replies

Write your reply to:

Draft