0

I'm using a WCF-webservice and Linq-To-SQL to insert a new record.

However, in the webservice i'm setting two fields, CreatedBy and Created, but even if i use ref parameter(as suggested on MSDN) the changes are not reflected at the client. So when i debug the service the properties are set and the record is inserted correctly, but at the calling method the object's properties are still unset.

This will cause problems later, for example if i'll try to delete it i'll get following exception at db.SubmitChanges() since these columns are non-null:

System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Here's the insert method at the client(winforms application):

private void Add_Status()
{
    using (var db = new ERP_ServiceClient())
    {
        var status = new Erp_ServiceReference.Status();
        status.Name = this.txtStatusNameAdd.Text;
        db.InsertStatus(status, this.IdUser);
        statusBindingSource.Add(status);
    }

    this.txtStatusNameAdd.Text = "";
    this.txtStatusNameAdd.Select();
}

This is the webservice method:

public void InsertStatus(ref Status status, int userID)
{
    using (var db = new ERPDataContext())
    {
        status.CreatedBy = userID;
        status.Created = DateTime.Now;
        db.Status.InsertOnSubmit(status);
        db.SubmitChanges();
    }
}

What am i doing wrong? My wcf,winforms and linq-to-sql skills are rusty(that's why i've chosen them).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    My guess is that even through status is marked as ref, the web service is not passing back that value. Have you tried just returning that value? – Rob Mar 26 '13 at 17:33
  • i think thats not possible..you need to retrive data back .. – Pranay Rana Mar 26 '13 at 17:35
  • check this : http://stackoverflow.com/questions/2882178/designing-wcf-interface-no-out-or-ref-parameters – Pranay Rana Mar 26 '13 at 17:40
  • you can also check this answer : http://stackoverflow.com/questions/1956039/are-by-ref-arguments-in-wcf-bad-or-good – Pranay Rana Mar 26 '13 at 17:57
  • Thanks for the links. However, I've already seen the proposed duplicate. The accepted answer led me to the link which suggests void+ref. – Tim Schmelter Mar 26 '13 at 18:13

1 Answers1

1

You can't pass by reference in a web service. You can return the value.

public Status InsertStatus(Status status, int userID)
{
    using (var db = new ERPDataContext())
    {
        status.CreatedBy = userID;
        status.Created = DateTime.Now;
        db.Status.InsertOnSubmit(status);
        db.SubmitChanges();
        return status;
    }
}

private void Add_Status()
{
    using (var db = new ERP_ServiceClient())
    {
        var status = new Erp_ServiceReference.Status();
        status.Name = this.txtStatusNameAdd.Text;
        status = db.InsertStatus(status, this.IdUser);
        statusBindingSource.Add(status);
    }

    this.txtStatusNameAdd.Text = "";
    this.txtStatusNameAdd.Select();
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Rob
  • 5,578
  • 3
  • 23
  • 28
  • Your deleted answer was close. Actually i've forgotten to refresh the service reference, hence i didn't get a compiler error that the argument must be `ref`. +1 for the efforts, thanks. – Tim Schmelter Mar 27 '13 at 08:48