Click here to Skip to main content
15,886,772 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am a beginner to wcf,my question is ,i simply want to make a service which reads data from my sql table and returns it to the client ,and on the client side i invoke the service which returns the table and i display it on the client web form,i have provided a part of the code for the wcf service so that you may understand my question clearly!


if possible please show me the code without using generics

below is the code i wrote,
namespace datafromdbservice
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        dataret GetData(int id);       

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.

    [DataContract]
    public class dataret
    {
       [DataMember]
        public string name
        {
            get { return name; }
            set { name = value; }
        }

        [DataMember]
        public int personid
        {
            get { return personid; }
            set { personid = value; }
        }
        [DataMember]
        public DateTime hiredate { get; set; }
        [DataMember]
        public DateTime enrolldate { get; set; }
        [DataMember]
        public string discriminator { get; set; }
    }
}
//service1.svc.cs

 public class Service1 : IService1
    {
        SqlCommand cmd;
        SqlConnection con;
        SqlDataReader dr;
        dataret ds = new dataret();
        public dataret GetData(int id)
        {
            con=new SqlConnection("Data Source=dell-pc;Initial Catalog=test1;Persist Security Info=True;User ID=Sa;Password=passwordone;MultipleActiveResultSets=True");
            cmd=new SqlCommand("select * from person where id=1",con);
            con.Open();
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ds.name = dr.GetValue(0).ToString();
                ds.personid = Convert.ToInt32(dr.GetValue(1));
                ds.hiredate = Convert.ToDateTime(dr.GetValue(2));
                ds.enrolldate = Convert.ToDateTime(dr.GetValue(3));
                ds.discriminator = dr.GetValue(4).ToString();        
            }
            return ds;
Posted
Updated 28-Mar-14 9:07am
v2
Comments
[no name] 28-Mar-14 14:53pm    
What generics are you talking about? Why would you do this anyway? Your GetData method takes an int parameter and you do not even use it. Your query will only return records where the id is 1. If there is more than one record, you will only get the last one. The very first time you run this, you will get a stackoverflow exception from your name property. When you fix that, the second time you run this, you will get a stackoverflow exception on your personid property. You need to review basic programming.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900