private void ShowMainItem() { SqlConnection conn = new SqlConnection(dbconn.Configuration()); SqlCommand cmd = null; SqlDataReader rdr = null; string sqlsel = "select headline, category, picture, description from selection, item where fk_mainitem = item.id"; try { conn.Open(); cmd = new SqlCommand(sqlsel, conn); rdr = cmd.ExecuteReader(); RepeaterMainItem.DataSource = rdr; RepeaterMainItem.DataBind(); } catch (Exception ex) { //LabelMessage.Text = ex.Message; } finally { conn.Close(); } }
public void mainItem() { // Anvender et try-catch-finally statement, // der kan opfange fejl undervejs i processen. try { // Åbner forbindelsen til DB. conn.Open(); // Laver en ny instans af SqlCommand klassen, // der tager imod to parametrer. cmd = new SqlCommand(sqlMainItem, conn); // Vi forventer mere end én række bliver returneret, // og anvender en ExecuteReader metode. rdr = cmd.ExecuteReader(); RepeaterMainItem.DataSource = rdr; // Fortæller Repeater, hvor den skal få sin data fra (datakilde). Tilegner Repeater-kontrol med DataReaders resultater. RepeaterMainItem.DataBind(); // Binder datakildens resultater til Repeater - Data vil blive vist i Repeater. } catch (Exception ex) { //LabelMessage.Text = ex.Message; } finally { // Lukker forbindelsen til DB. conn.Close(); } }
protected void Page_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(@"data source = .\sqlexpress; integrated security = true; database = CMS;"); SqlCommand cmd = null; SqlDataReader rdr = null; string sqlsel1 = "SELECT mainItem, category, headline, picture, text FROM Selection, Items WHERE Selection.mainItem = Items.itemID "; string sqlsel2 = "SELECT secondItem, category, headline, picture, text FROM Selection, Items WHERE Selection.secondItem = Items.itemID "; string sqlsel3 = "SELECT thirdItem, category, headline, picture, text FROM Selection, Items WHERE Selection.thirdItem = Items.itemID "; string sqlsel4 = "SELECT Selection.jokeId, Jokes.caption, Jokes.img FROM Selection, Jokes WHERE Selection.jokeId = Jokes.jokeID"; try { conn.Open(); cmd = new SqlCommand(sqlsel1, conn); rdr = cmd.ExecuteReader(); RepeaterMainItem.DataSource = rdr; RepeaterMainItem.DataBind(); } catch (Exception ex) { LabelFooter.Text = ex.Message; } finally { conn.Close(); } try { conn.Open(); cmd = new SqlCommand(sqlsel2, conn); rdr = cmd.ExecuteReader(); RepeaterSecond.DataSource = rdr; RepeaterSecond.DataBind(); } catch (Exception ex) { LabelFooter.Text = ex.Message; } finally { conn.Close(); } try { conn.Open(); cmd = new SqlCommand(sqlsel3, conn); rdr = cmd.ExecuteReader(); RepeaterThird.DataSource = rdr; RepeaterThird.DataBind(); } catch (Exception ex) { LabelFooter.Text = ex.Message; } finally { conn.Close(); } try { conn.Open(); cmd = new SqlCommand(sqlsel4, conn); rdr = cmd.ExecuteReader(); RepeaterJoke.DataSource = rdr; RepeaterJoke.DataBind(); } catch (Exception ex) { LabelFooter.Text = ex.Message; } finally { conn.Close(); } }