locked
multiple paramters passing in async RRS feed

  • Question

  • Hii all,

    Im having a problem . i have in database a table which has 3 columns :) i want to insert values in them using WCF . How should i go about it .. im unable to send values using async method ..

     

     

     
    Monday, June 6, 2011 6:42 AM

Answers

  • 1. create data contract ex: your table is Customer

    [DataContract]
        public class Customer
        {
            [DataMember]
            public string FirstName { get; set; }
            [DataMember]
            public string MiddleName { get; set; }
            [DataMember]
            public string LastName { get; set; }
    }

    Service Contract

    public ICustomerService

    {

    [OperationContract]

    int InsertCustomer(Customer customer);

    }

    public class CustomerService:ICustomerService

    {

    public int InsertCustomer(Customer customer)
    {
      //TODO: Insert customer
     //customer.FirstName
    }

    }

    In client

    Client.InsertCustomerCompleted+=((s,e)=>{ //Insert successfull});

    Customer customer= new Customer(){FirstName="test", LastName="test2"};

    Client.InsertCustomerAsync(customer);

    Check this like

    http://www.codeproject.com/KB/WCF/wcf_bohansen.aspx

    -Ganga

     

    Monday, June 6, 2011 9:10 AM

All replies

  • Hi,

    Can you share your code..

    you can check below method to call async method with parms..

    client.InsertUserCompleted += ((object sender, InsertUserCompletedEventArgs e) =>
                {
                    if (e.Error == null && e.Result != null)
                    {

                      //TODO: Insert success                                      

                    }
                               });
                client.InsertUserAsync(userName, password);

    -Ganga

    Monday, June 6, 2011 6:49 AM
  • Below is the code ..im trying to catch 3 values and insert it in my table ..how shud i go about it

    Monday, June 6, 2011 6:52 AM
  • mainpage.cs


            private void button1_Click(object sender, RoutedEventArgs e)
            {
                string[] a = new string[10];
                a[0] = textBox1.Text;
                a[1] = textBox2.Text;
                a[2] = textBox3.Text;
                proxy.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(proxy_DoWorkCompleted);
               
                proxy.DoWorkAsync(a);
               
               
            }

            void proxy_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
            {
                //throw new NotImplementedException();
                textBox11.Text = e.Result.ToString();
            }
            }

     


    .svc.cs file

     

    namespace SilverlightApplication2.Web
    {
        [ServiceContract(Namespace = "")]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class CustomerService
        {
          
            [OperationContract]
            public int DoWork(string[] a)
            {
              
               string a0= a[0].ToString();
               string a1 = a[1].ToString();
               string a2 = a[2].ToString();

                SqlConnection conn;
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ToString());
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into Table_3 values('"+a[0]+"','"+a[1]+"','"+a[2]+"')";
                cmd.Connection = conn;
                int ret = 0 ;

                try
                {
                    conn.Open();
                    ret = cmd.ExecuteNonQuery();

                }
                catch (Exception exp)
                {
                }
                finally
                {
                    conn.Close();
                }
                if (ret > 0)
                {
                    return ret;
                }
                else
                {
                    return 0;
                }
            }

            // Add more operations here and mark them with [OperationContract]
        }
    }

    Monday, June 6, 2011 6:57 AM
  • Hi,

    How u r adding Service reference? by default it will accept Observablecollection

    if you want to pass array..

    while adding service reference in dialog click Addvance and change collection type to System.Array

    I hope that will resolve the issue if not let me know..

    Note: Ideally if you want to pass three paramaters you need to pass as differnt parameters or if you have more parameters to pass  create a datacontract and use.

    -Ganga

    Monday, June 6, 2011 7:42 AM
  • Hii ganga ,

     

    can you show me with an example

    Monday, June 6, 2011 8:10 AM
  • Hi Aditya,

    What do you want, Adding a service reference example? or creating a service with multiple parameters?

    -Ganga

    Monday, June 6, 2011 8:39 AM
  • Creating a service with multiple parameters :)

    Thanks in advance .....

    Monday, June 6, 2011 8:47 AM
  • 1. create data contract ex: your table is Customer

    [DataContract]
        public class Customer
        {
            [DataMember]
            public string FirstName { get; set; }
            [DataMember]
            public string MiddleName { get; set; }
            [DataMember]
            public string LastName { get; set; }
    }

    Service Contract

    public ICustomerService

    {

    [OperationContract]

    int InsertCustomer(Customer customer);

    }

    public class CustomerService:ICustomerService

    {

    public int InsertCustomer(Customer customer)
    {
      //TODO: Insert customer
     //customer.FirstName
    }

    }

    In client

    Client.InsertCustomerCompleted+=((s,e)=>{ //Insert successfull});

    Customer customer= new Customer(){FirstName="test", LastName="test2"};

    Client.InsertCustomerAsync(customer);

    Check this like

    http://www.codeproject.com/KB/WCF/wcf_bohansen.aspx

    -Ganga

     

    Monday, June 6, 2011 9:10 AM