locked
only one row using a wcf ria service RRS feed

  • Question

  • this are my screens:

    and this is my result screen:

    and here is my code:

    1) SupplierDataService:

    namespace Central.Data.Services
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.ServiceModel.DomainServices.Hosting;
        using System.ServiceModel.DomainServices.Server;
        using System.Data.SqlClient;
        using System.Configuration;
        using MySql.Data.MySqlClient;
    
    
        [Description("Enter the connection string to the Shipper Central DB")]
        public class SupplierDataService : DomainService
        {
    
            private readonly List<SupplierRecord> _supplierRecordList;
            public SupplierDataService ()
            {
            _supplierRecordList = new List<SupplierRecord>();
            }
            string _connectionString;
            public override void Initialize (System.ServiceModel.DomainServices.Server.DomainServiceContext context)
            {       
    
                _connectionString = "Server=localhost;Uid=root;pwd=123;Database=pruebarelacion";
       
                base.Initialize(context);
            }
            [Query(IsDefault = true)]
            public IQueryable<SupplierRecord> GetSupplierData()
            {
            _supplierRecordList.Clear();
    
            MySqlConnection cnn = new MySqlConnection(_connectionString);
    
    
    
            //MySqlCommand cmd = new MySqlCommand("SELECT * FROM tabla1", cnn);
            MySqlCommand cmd = new MySqlCommand("select * from tabla1", cnn);
      
    
            try
            {
                cnn.Open();
                   
                MySqlDataReader dr = cmd.ExecuteReader();
                SupplierRecord supplier;
            {
                while (dr.Read())
                {
                    supplier = new SupplierRecord();               
                    supplier.campo1 += dr["campo1"].ToString();
                 
                    _supplierRecordList.Add(supplier);
                    
                }
            }
    
                }
            finally
            {
             cnn.Close();
            }
            return _supplierRecordList.AsQueryable();
            
            }
    
    
        }
    }
    
    

    2) SupplierRecord

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ComponentModel.DataAnnotations;
    
    namespace Central.Data.Services
    {
        public class SupplierRecord
        {
    
            [Key, Editable(false)]
            public int idTabla1 { get; set; }
            [Required(ErrorMessage = "Campooo 1"),
            StringLength(60)]
            public string campo1 { get; set; }
           // public string campo2 { get; set; }
    
        }
    }

    as you can see in my result screen it only show me one row but in my database i have 10 rows.

    in this example im using mysql but is the same if i use sqlserver

    please help me


    Sunday, February 17, 2013 8:45 PM

Answers

  • You misunderstood what I said. You have to set idTabla1 on the SupplierRecord instance, for example:

    while (dr.Read())
    {
        supplier = new SupplierRecord();           
        supplier.idTabla1 = Convert.ToInt32(dr["idTabla1"]);
        supplier.campo1 += dr["campo1"].ToString();
    
        _supplierRecordList.Add(supplier);
    }


    Justin Anderson, LightSwitch Development Team


    Monday, February 18, 2013 9:47 PM
    Moderator

All replies

  • You should be specifying a unique key value for the idTabla1 property on each instance of SupplierRecord. Otherwise, all of the instances have the same key value, which would make all of the instances equivalent. Hence, why you are getting only one instance returned.

    Also, the value for idTabla1 should not be arbitrary. If the application executes a query that returns an instance of the SupplierRecord entity and another invocation or another query returns the same (equivalent) instance, then the idTable1 property should be the same value.


    Justin Anderson, LightSwitch Development Team

    Monday, February 18, 2013 1:48 AM
    Moderator
  • You should be specifying a unique key value for the idTabla1 property on each instance of SupplierRecord. Otherwise, all of the instances have the same key value, which would make all of the instances equivalent. Hence, why you are getting only one instance returned.

    Also, the value for idTabla1 should not be arbitrary. If the application executes a query that returns an instance of the SupplierRecord entity and another invocation or another query returns the same (equivalent) instance, then the idTable1 property should be the same value.


    Justin Anderson, LightSwitch Development Team

    i have changed this: supplier.campo1 = dr["campo1"].ToString();

    for this: supplier.campo1 = dr["idTabla1"].ToString();

    and still have only one row.

    what else do i have to change

    Monday, February 18, 2013 8:20 PM
  • You misunderstood what I said. You have to set idTabla1 on the SupplierRecord instance, for example:

    while (dr.Read())
    {
        supplier = new SupplierRecord();           
        supplier.idTabla1 = Convert.ToInt32(dr["idTabla1"]);
        supplier.campo1 += dr["campo1"].ToString();
    
        _supplierRecordList.Add(supplier);
    }


    Justin Anderson, LightSwitch Development Team


    Monday, February 18, 2013 9:47 PM
    Moderator
  • You misunderstood what I said. You have to set idTabla1 on the SupplierRecord instance, for example:

    while (dr.Read())
    {
        supplier = new SupplierRecord();           
        supplier.idTabla1 = Convert.ToInt32(dr["idTabla1"]);
        supplier.campo1 += dr["campo1"].ToString();
    
        _supplierRecordList.Add(supplier);
    }


    Justin Anderson, LightSwitch Development Team


    sorry but not working:

        public class SupplierRecord
        {

            [Key, Editable(false)]
            public int idTabla1 { get; set; }
            [Required(ErrorMessage = "Campooo 1"),
            StringLength(60)]
            public string campo1 { get; set; }
           // public string campo2 { get; set; }

        }

    and

    supplier.idTabla1 = Convert.ToInt32(dr["idTabla1"]);

    and not working


    now it shows nothing
    Monday, February 18, 2013 10:04 PM
  • You misunderstood what I said. You have to set idTabla1 on the SupplierRecord instance, for example:

    while (dr.Read())
    {
        supplier = new SupplierRecord();           
        supplier.idTabla1 = Convert.ToInt32(dr["idTabla1"]);
        supplier.campo1 += dr["campo1"].ToString();
    
        _supplierRecordList.Add(supplier);
    }


    Justin Anderson, LightSwitch Development Team


    oh my god, mi mistake.

    i forgot to write:

     _supplierRecordList.Add(supplier);

    now is working

    thanks alot

    Tuesday, February 19, 2013 12:25 AM
  • is there a way to update data using this ria service??
    Tuesday, February 19, 2013 1:10 AM