0

I wonder how I could write out my rows, and not the first one only.

Here is my c#

string SessionID = Session["ID"].ToString();

using (SqlConnection connection = new SqlConnection(@"Data Source=JENSKVIST\SQLEXPRESS;Initial Catalog=BlogNetwork;Integrated Security=True"))
{
    connection.Open();

    string query = "SELECT * FROM Entries ORDER BY Entry_ID DESC";
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                string DataHeadline = reader["Entry_Headline"].ToString();
                string DataUserID = reader["Entry_UserID"].ToString();
                string DataMedia = reader["Entry_Media"].ToString();
                string DataContent = reader["Entry_Content"].ToString();
                string DataID = reader["Entry_ID"].ToString();

                User_ID.Value = DataID;
                Entry_Headline.Text = DataHeadline;
                Entry_Media.ImageUrl = DataMedia;
                Entry_Content.Text = DataContent;
            }
        }

    }

    string UserQuery = "SELECT * FROM Users WHERE User_ID=@DataID";
    using (SqlCommand UserCommand = new SqlCommand(UserQuery, connection))
    {

        UserCommand.Parameters.AddWithValue("@DataID", User_ID.Value);

        using (SqlDataReader UserReader = UserCommand.ExecuteReader())
        {
            while (UserReader.Read())
            {
                string DataAvatar = UserReader["User_Avatar"].ToString();
                string DataName = UserReader["User_Firstname"].ToString() + " " + UserReader["User_Lastname"].ToString();

                Entry_Avatar.ImageUrl = DataAvatar;
                Entry_Name.Text = DataName;

            }
        }
    }
}

and here are my asp elements

<asp:HiddenField runat="server" ID="User_ID" />

<asp:Image runat="server" ID="Entry_Avatar" />

<asp:Label runat="server" ID="Entry_Name" /> posted entry <asp:Label runat="server" ID="Entry_Headline" />

<asp:Image runat="server" ID="Entry_Media" Width="400" />

<asp:Label runat="server" ID="Entry_Content" />... <asp:HyperLink runat="server" ID="Entry_Link">Read more</asp:HyperLink>

I don't know how to do it, I also searched online, but no solution found. If you guys wanna see my sql table, feel free to ask. I hope you guys can help me figure this out. Thank you.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kvist
  • 29
  • 4
  • 3
    You are reading all of your data, but you are assigning it over and over again to the same controls. It's like wanting to display a whole table using only one row - it can be done if you really want to, but the solution won't be pretty and it won't make much sense in the first place. – S_F Nov 18 '13 at 09:36
  • 2
    I think you're looking for the repeater, [here](http://www.codeproject.com/Articles/8659/Mastering-ASP-NET-DataBinding)'s an article to get you started. – JMK Nov 18 '13 at 09:38
  • @Kvist As **S_F** already said you are assigning new values to same variable , So first let us know what you want to do, I mean what output you need in what form ? – Suraj Singh Nov 18 '13 at 10:02
  • I wanna display it in a asp:label :) – Kvist Nov 18 '13 at 10:44

1 Answers1

0

Below is a sample application i have created.

entitySet is a collection.

           using (_connection)
            {
                reader = command.ExecuteReader();

                fieldCount = reader.FieldCount;
                properties = new SortedList<string, PropertyInfo>();

                foreach (PropertyInfo pi in typeof(T).GetProperties())
                {
                    properties.Add(pi.Name.ToUpper(), pi);
                }

                while (reader.Read())
                {
                    T item = Activator.CreateInstance<T>();

                    for (int i = 0; i < fieldCount; i++)
                    {
                        if (reader[i] != DBNull.Value)
                            properties[reader.GetName(i).ToUpper()].SetValue(item, reader[i], null);
                    }

                    entitySet.Add(item);
                }
            }
            reader.Close();
Vishnu
  • 21
  • 5