Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 21

Discussione: Aiuto per ciclo

  1. #1
    Utente di HTML.it L'avatar di Sonikag
    Registrato dal
    Mar 2004
    Messaggi
    2,080

    Aiuto per ciclo

    Ho preso da questo 3d il codice per creare un repeater annidato e l'ho tradotto in c. Ho avuto qualche complicazione ma tutto ok... Il problema è che adesso il codice mi visualizza solo il primo gruppo e il primo record del primo gruppo osservando il codice vedo che va a pescare solo il idgruppo=1 quindi immagino servirà un ciclo, ma come???

    Nel page load richiamo la mia funzione carica che è questa:
    codice:
    public void carica()
    {
    ds = new DataSet(); 
    
    OleDbDataAdapter cmd1 = new OleDbDataAdapter("select * from ban where idban=1", conn); 
    
    OleDbDataAdapter cmd2 = new OleDbDataAdapter("select * from datiban where iddet=1", conn); 
    
    conn.Open();
    
    cmd1.Fill(ds, "ban");
    
    cmd2.Fill(ds, "nome");
    
    ds.Relations.Add("myrelation", ds.Tables["ban"].Columns["idban"], ds.Tables["nome"].Columns["iddet"]);
    
    parentRepeater.DataSource = ds.Tables["ban"];
    
    Page.DataBind();
    
    conn.Close();
    
    }
    Avrei "semplicemente" bisogno di capire come incrementare l'id di queste due cmd
    OleDbDataAdapter cmd1 = new OleDbDataAdapter("select * from ban where idban=1", conn);

    OleDbDataAdapter cmd2 = new OleDbDataAdapter("select * from datiban where iddet=1", conn);

    Sonia

  2. #2

    Re: Aiuto per ciclo

    Se i dati non sono molti potresti evitare di mettere una clausola WHERE nella query sql e poi una volta che i dati sono in memoria filtrarli.
    Saluti a tutti
    Riccardo

  3. #3
    Utente di HTML.it L'avatar di Sonikag
    Registrato dal
    Mar 2004
    Messaggi
    2,080
    Ho tolto il where e mi viene restituito questo errore:

    This constraint cannot be enabled as not all values have corresponding parent values.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.

    Source Error:


    Line 46: cmd2.Fill(ds, "nome");
    Line 47:
    Line 48: ds.Relations.Add("myrelation", ds.Tables["ban"].Columns["idban"], ds.Tables["nome"].Columns["iddet"]);
    Line 49:
    Line 50: parentRepeater.DataSource = ds.Tables["ban"];

  4. #4
    L'errore e' abbastanza chiaro, non puoi inserire una relazione tra dati che non hanno relazione (o non tutti i record della tabella figlia hanno un corrispettivo id nella tabella padre). Non ho capito pero' che senso abbia aggiungere quella datarelation a meno che da qualche parte nell'applicazione tu non la usi effettivamente.
    Saluti a tutti
    Riccardo

  5. #5
    Utente di HTML.it L'avatar di Sonikag
    Registrato dal
    Mar 2004
    Messaggi
    2,080
    la uso in fase di visualizzazione:

    codice:
    <asp:repeater id="parentRepeater" runat="server">
    								<itemtemplate>
    									
    										<%# DataBinder.Eval(Container.DataItem, "ban") %>
    									
    									
    
    									<asp:repeater id="childRepeater" datasource='<%# DataBinder.Eval(Container.DataItem, "myrelation") %>' runat="server">
    										<itemtemplate>
    											<%# DataBinder.Eval(Container.DataItem, "nome") %>
    											
    
    										</itemtemplate>
    									</asp:repeater>
    								</itemtemplate>
    							</asp:repeater>
    Il risultato che vorrei avere è una cosa simile:

    Gruppo1:
    - maria
    - mario
    - giuseppe
    Gruppo2:
    - Lorenzo
    - Gianni
    Gruppo3:
    - Sara

  6. #6
    La pagina aspx è:
    codice:
    <form id="Form1" method="post" runat="server">
    	
    	<asp:DataList id="DataList1" 	runat="server">
    		<ItemTemplate>
    			<%# DataBinder.Eval(Container.DataItem, "Categoryname") %>
    			<asp:DataList id="DataList2" runat="server">
    			<ItemTemplate>
    				<%# DataBinder.Eval(Container.DataItem, "Productname") %>
    				</ItemTemplate>
    			</asp:DataList>
    		</ItemTemplate>
    	</asp:DataList>
    </form>
    Mentre il codebehind:

    codice:
    Imports System.Data
    Imports System.Data.SqlClient
    
    Public Class WebForm5
        Inherits System.Web.UI.Page
        Dim dtProdotti As New DataTable("Products")
    
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
        End Sub
        
        Protected WithEvents DataList1 As System.Web.UI.WebControls.DataList
        Protected WithEvents DataList2 As System.Web.UI.WebControls.DataList
    
        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object
    
        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
    
    #End Region
    
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            If Not Page.IsPostBack Then
                GetProdotti()
                bindDataList()
            End If
        End Sub
    
        Private Sub bindDataList()
            Dim cnn As New SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost")
            Dim cmd As New SqlCommand("SELECT * FROM Categories", cnn)
            Dim da As New SqlDataAdapter(cmd)
            Dim dt As New DataTable("Categories")
            cnn.Open()
            da.Fill(dt)
            cnn.Close()
            Me.DataList1.DataSource = dt
            Me.DataList1.DataBind()
        End Sub
    
        Private Sub GetProdotti()
            Dim cnn As New SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost")
            Dim cmd As New SqlCommand("SELECT * FROM Products", cnn)
            Dim da As New SqlDataAdapter(cmd)
            'Dim dt As New DataTable("Products")
            cnn.Open()
            da.Fill(dtProdotti)
            cnn.Close()
        End Sub
    
        Private Sub bindDataList2(ByVal i As String, ByRef dl As DataList)
            Dim dv As DataView
            dv = dtProdotti.DefaultView
            dv.RowFilter = "CategoryID = " & i
            dl.DataSource = dv
            dl.DataBind()
        End Sub
    
        Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
            Dim d As DataRowView
            d = e.Item.DataItem
            Dim d2 As DataList
            d2 = e.Item.FindControl("DataList2")
            bindDataList2(d(0), d2)
        End Sub
    End Class

    Fammi sapere
    Ciao
    Kalman
    Kalman

  7. #7
    Utente di HTML.it L'avatar di Sonikag
    Registrato dal
    Mar 2004
    Messaggi
    2,080
    Tnx! Non è che hai la traduzione in C#? Ho qualche problema di conversione!

  8. #8
    Mi fai fare lavoro doppio!!!

    La pagina aspx è identica,mentre il codebehind

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace TestVariCSharp
    {
    /// <summary>
    /// Summary description for WebForm7.
    /// </summary>
    public class WebForm7 : System.Web.UI.Page
    {

    protected System.Web.UI.WebControls.DataList DataList1;
    DataTable dtProdotti = new DataTable("Products");

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.DataList1.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler (this.DataList1_ItemDataBound);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void Page_Load(object sender, System.EventArgs e){
    if (!Page.IsPostBack){
    GetProdotti();
    bindDataList();
    }
    }

    private void bindDataList(){
    SqlConnection cnn = new SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost");
    SqlCommand cmd = new SqlCommand("SELECT * FROM Categories",cnn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable("Categories");

    cnn.Open();
    da.Fill(dt);
    cnn.Close();
    this.DataList1.DataSource = dt;
    this.DataList1.DataBind();
    }

    private void GetProdotti(){
    SqlConnection cnn = new SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost");
    SqlCommand cmd = new SqlCommand("SELECT * FROM Products",cnn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    cnn.Open();
    da.Fill(dtProdotti);
    cnn.Close();
    }

    private void bindDataList2(string i,ref DataList dl ){
    DataView dv = new DataView();
    dv = dtProdotti.DefaultView;
    dv.RowFilter = "CategoryID = " + i;
    dl.DataSource = dv;
    dl.DataBind();
    }

    private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e){
    DataRowView d = null;
    d = (DataRowView)e.Item.DataItem;
    DataList d2 = new DataList();
    d2 = (DataList)e.Item.FindControl("DataList2");
    bindDataList2(d[0].ToString(),ref d2);
    }
    }
    }

    Fammi sapere!
    Ciao
    Kalman
    Kalman

  9. #9
    Utente di HTML.it L'avatar di Sonikag
    Registrato dal
    Mar 2004
    Messaggi
    2,080
    Il codice è perfetto... in fase di compilazione tutto ok, ma in fase di visualizzazione ho questo errore:
    Parser Error Message: The base class includes the field 'DataList1', but its type (System.Web.UI.WebControls.DataList) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl).

    Source Error:


    Line 70: </tr>
    Line 71: </table>
    Line 72: <aspataList id="DataList1" runat="server">
    Line 73: <ItemTemplate>
    Line 74: <%# DataBinder.Eval(Container.DataItem, "gruppo") %>

  10. #10
    Non mi sembra molto corretto!!


    <aspataList id="DataList1" runat="server">


    Ciao
    Kalman
    Kalman

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.