0

Hi I'm trying to return string to my textboxes by click on a repeater row item here the web method;

   [WebMethod]
    public string  OrderGet(int User)
    {
        using (Models.DermabonEntities db = new Models.DermabonEntities())
        {



            var get = (from i in db.OrderAddress
                       where i.Id == User
                       select i.UserId).ToList();

            var a = get.FirstOrDefault().ToString();

            var res = (from i in db.OrderAddress
                       where i.UserId == a
                       select i.FirstName).FirstOrDefault();

            var res1 = (from i in db.OrderAddress
                        where i.UserId == a
                        select i.LastName).FirstOrDefault();

            object[] array1 = new object[2];

            array1[0] = res;
            array1[1] = res1;

            return array1.ToString();

        }
    }

As you see I return array1 here the ajax code;

function siparisAl(id) {

      var User = id;



      $.ajax({
          dataType: "json",
          type: "POST",
          contentType: "application/json",
          url: "/Admin/WebService/Control.asmx/OrderGet",
          data: JSON.stringify({ 'User': User }),
          success: function (data) {

              $("#ContentPlaceHolder1_Name").val(data.d[0]);

                },
          error: function () {

          }
      });
      return false;
  }

the problem is when I run it writes S to the Name textbox why it could be ? And How can I fix it I have several names in my database instead of 'S' please help

and I've tried console.log(data.d) = it says System.Object[]

2
  • Can you clarify what your problem is (I'm having a little bit of trouble understanding what you're saying is happening)? Commented Jun 10, 2014 at 19:07
  • let me explain I have a repeater of ordered products I list them with repeater and in the repeater I have little descriptions like name surname and I have a ımage button for per row when you click it send order Id to the ajax then webservice. So I control the Id and then I return full description to the dialog box. which is name surname address etc but the problem is when I return array it doesnt give the name and surname it gives 'S' Commented Jun 10, 2014 at 19:08

1 Answer 1

1

If you want to return a string, change the code from:

return array1.ToString();

To:

return res + res1;

In the first snippet your are not returning the concatenated string, but calling the ToString method of the object array. Which is System.Object[].

Edit:

To access res and res1 separately, you could return:

return new { First = res, Second = res1};

And in the success callback of your ajax call:

data.First
data.Second

Use console.log(data) to check the response, you should be able to figure it out.

Edit2:

To return a list of string, change the web method signature to:

public List<string> OrderGet(int User)

And in the ajax call, the data object will be an array.

Suggestion:

Please, you should improve your code quality. Check the C# naming convention and best practices.

Edit3:

If you are using an anonymous object, like in the first edit, you need to return an anonymous object from your web service, so change the signature to:

public dynamic OrderGet(int User)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.