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.

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. Commented Nov 18, 2013 at 9:36
  • 2
    I think you're looking for the repeater, here's an article to get you started. Commented Nov 18, 2013 at 9: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 ? Commented Nov 18, 2013 at 10:02
  • I wanna display it in a asp:label :) Commented Nov 18, 2013 at 10:44

1 Answer 1

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();

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.