GentlemenFinn
GentlemenFinn

Reputation: 135

Using Sql data after binding to repeater

I wish to use some data from the database after binding my query to a repeater. But i'm not sure how i am supposted to do this. Here is my code:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM kontakt", conn);

conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
Repeater_Beskeder.DataSource = reader;
Repeater_Beskeder.DataBind();

    foreach (RepeaterItem row in Repeater_Beskeder.Items)
    {
        if (reader.Read())
        {
            Panel Vis_Panel = (Panel)row.FindControl("Panel_Vis_Besked");
            if (Request.QueryString["id"].ToString() == reader["id"])
            {
                Vis_Panel.Visible = true;
            }
        }
    }
conn.Close();

My reader wont work as it's allready been binded to my repeater, so i'm quite lost. I hope some of you have another solution to this problem.

Upvotes: 1

Views: 243

Answers (1)

Shafqat Masood
Shafqat Masood

Reputation: 2570

ItemDataBound Event Occurs after an item in the Repeater control is data-bound but before it is rendered on the page.

 void Repeater_Beskeder_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

      // This event is raised for the header, the footer, separators, and items.

      // Execute the following logic for Items and Alternating Items.
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
         Panel Vis_Panel = (Panel)row.FindControl("Panel_Vis_Besked");
        if (Request.QueryString["id"].ToString() == reader["id"])
        {
            Vis_Panel.Visible = true;
        }
      }
   }    

Upvotes: 2

Related Questions