mySQL get all Same value in the List

in my Database have 7 different of data. But in my Result all return the same value. What logic error in my code??? which [0]-[6] is 1 result with same value. The result like this [0] ="tester" [1]="tester" [2]="tester"....util [6]

public class StreamWcf : IStreamWcf
{
    public List<streamData> getMusic()
    {
        try
        {
            MySqlConnection con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["ComeVoxConnectionString"].ToString());
            MySqlDataReader myReader;

            streamData user_stream = new streamData();
            List<streamData> userStream = new List<streamData>();

            //string cmdText = "SELECT m.id, s.avatar, s.username, m.title, m.path, m.description, m.date FROM users s, musics m WHERE (musics.userID = users.id)";
            string cmdText = "SELECT musics.id as MID, users.avatar as UserPic, users.username as userName, musics.title as MTitle, musics.path as MusicPath, musics.description as Mdesc, musics.date as MDate FROM `users`, `musics` WHERE musics.userID = users.id;";

            con.Open();
            MySqlCommand cmd = new MySqlCommand(cmdText, con);

            myReader = cmd.ExecuteReader();

            if (myReader.HasRows)
            {
                while (myReader.Read())
                {
                    user_stream.musicID = Convert.ToInt32(myReader["MID"].ToString());
                    user_stream.Title = myReader["MTitle"].ToString();
                    user_stream.FilePath = myReader["MusicPath"].ToString();
                    user_stream.desc = myReader["Mdesc"].ToString();
                    user_stream.Artists = myReader["userName"].ToString();
                    user_stream.releaseDate = Convert.ToDateTime(myReader["MDate"].ToString());
                    user_stream.imgUrl = myReader["UserPic"].ToString();


                    userStream.Add(user_stream);
                }

                myReader.Close();
                con.Close();
            }


            return userStream;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

    }
} 

Answers


Create new user_stream in your while block.

And its' preferable to use using statements

using (var con =new MySqlConnection(WebConfigurationManager.ConnectionStrings["ComeVoxConnectionString"].ToString()))
{

    var userStream = new List<streamData>();

    //string cmdText = "SELECT m.id, s.avatar, s.username, m.title, m.path, m.description, m.date FROM users s, musics m WHERE (musics.userID = users.id)";
    string cmdText =
        "SELECT musics.id as MID, users.avatar as UserPic, users.username as userName, musics.title as MTitle, musics.path as MusicPath, musics.description as Mdesc, musics.date as MDate FROM `users`, `musics` WHERE musics.userID = users.id;";

    con.Open();
    using (var cmd = new MySqlCommand(cmdText, con))
    {

        using (MySqlDataReader myReader = cmd.ExecuteReader())
        {

            if (myReader.HasRows)
            {
                while (myReader.Read())
                {
                    var user_stream = new streamData();
                    user_stream.musicID = Convert.ToInt32(myReader["MID"].ToString());
                    user_stream.Title = myReader["MTitle"].ToString();
                    user_stream.FilePath = myReader["MusicPath"].ToString();
                    user_stream.desc = myReader["Mdesc"].ToString();
                    user_stream.Artists = myReader["userName"].ToString();
                    user_stream.releaseDate = Convert.ToDateTime(myReader["MDate"].ToString());
                    user_stream.imgUrl = myReader["UserPic"].ToString();


                    userStream.Add(user_stream);
                }
            }
        }
    }
}

You're operating on one and the same user_stream instance. Move streamData user_stream = new streamData(); into your while loop to create a new one each iteration.


Need Your Help

Transparent Navigation Bar for Lollipop app?

android xml themes android-5.0-lollipop navigationbar

What do I add to themes.xml to change the navigation bar to be more transparent? I can't find documentation for it anywhere. Thanks!

how to fill the timestamp by input something in the textfield manually?

javascript ajax html5 web

I am not a web developer. So my question maybe a bit silly.