DataTable as DataSource

Last post 11-29-2006 10:55 AM by ubaid1900. 1 replies.

Sort Posts:

  • DataTable as DataSource

    11-22-2006, 4:44 PM
    • Loading...
    • Huffase
    • Joined on 09-12-2005, 11:03 AM
    • Posts 2

    Hi,

    I have a problem with DataTable. I have one Repeater and one GridView Controls in my web page. User gets some information from Repeater with FindControl. I don't want to store this information in database because the user can change some informations. I send these informations to a datatable. The gridview shows these informations and uses datatable as datasource.

    When I use gridview with auto generated columns there is no problem, but when I try to create my columns I receive an error.

     The error reads: "A field or property with the name 'urunid' was not found on the selected datasource".

    the code is:

     

     

     protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ekle")
            {
                 .
                 .
                 .
                {
                    DataTable dt = new DataTable();
                    dt = yenisepetyarat();
                    DataRow drow = dt.NewRow();
                    drow[0] = urunid;
                    drow[1] = yemekadi;
                    drow[2] = sayi;
                    drow[3]=porsyon;
                    drow[4] = ucret;
                    dt.Rows.Add(drow);
                    Session["sepet"] = dt;
                    DataSet ds = new DataSet();
                    ds.Tables.Add(dt);
                    GridView1.DataSource = ds.Tables[0];
                    GridView1.DataBind();
                }
             }       
        }
    
    
    private DataTable yenisepetyarat()
        {
            DataTable dt = new DataTable();
            DataColumn urunid = new DataColumn();
            urunid.ColumnName = "urunid";
            urunid.Caption = "Ürün İd";
            urunid.DataType = typeof(int);
            dt.Columns.Add(urunid);
    
            DataColumn yemekadi = new DataColumn();
            urunid.ColumnName = "yemekadi";
            yemekadi.DataType = typeof(string);
            dt.Columns.Add(yemekadi);
    
            DataColumn adet = new DataColumn();
            urunid.ColumnName = "adet";
            adet.DataType = typeof(int);
            dt.Columns.Add(adet);
    
            DataColumn porsyon = new DataColumn();
            urunid.ColumnName ="porsyon";
            porsyon.DataType = typeof(string);
            dt.Columns.Add(porsyon);
    
            DataColumn fiyat = new DataColumn();
            urunid.ColumnName = "fiyat";
            fiyat.DataType = typeof(float);
            dt.Columns.Add(fiyat);
            return dt;
        }

     While creating datatable and adding columns to it, I check this process with "quickwatch". The problem is: I create the first column, then I create the second one. However, when the second column is created, it replaces the first column; and next to it, in the place where the second column should be I see a column created itself labelled "column1"  When I continue adding 3rd and the rest of the columns this process repeats itself. Each time I add a new column it replaces the former one, and a new column appears from nowhere next to it.

    dt.Columns[0].ColumnName = second column.

    dt.Columns[1].ColumnName=Column1

    And I get an error while showing this data on gridview.

    How can I solve this problem???

     

     

  • Re: DataTable as DataSource

    11-29-2006, 10:55 AM
    • Loading...
    • ubaid1900
    • Joined on 05-25-2005, 4:26 PM
    • Posts 141

    its a copy paste overlook. replace urunid.ColumnName for the second, third, fourth and fifth columns with corresponding datacolumn references.

    Hope it helps

Page 1 of 1 (2 items)
Microsoft Communities
Page view counter