0

Trying to populate select with sql results using WebMethods.

I can get and pass the data, but I've noticed the format is [["",""],["",""]] instead of [{"",""},{"",""}].

When I use $.each, I get single characters as the select's options, each character from the output, including the JSON delimiters.

VB

Dim MArray()() As String = New String(sqlDataset.Tables(0).Rows.Count)() {}
Dim i As Integer = 0

For Each rs As DataRow In sqlDataset.Tables(0).Rows
    MArray(i) = New String() {rs("suppliername").ToString(), rs("supplierid").ToString()}
    i = i + 1
Next

Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Dim sJSON As String = js.Serialize(MArray)
Return sJSON

Jquery

$(document).ready(function () {

$.ajax({
    type: "POST",
    url: "SupplierAdmin.aspx/PopulateSupplierSelectDropDownList",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        $("#supplierSelect").empty();
        $.each(msg.d, function(name, value) {
                $('#supplierSelect').append($('<option>').text(name).attr('value', value));
            });
    }
});
});
3
  • In the alert, do you get [object Object], a comma separated list of values, or the json string. It should be either [object Object] or a comma separated list of values. $.type(msg) should be "array" or "object" Commented Sep 12, 2012 at 17:32
  • Did you mean to type $.each(this...) or do you mean $.each(msg.d)? Commented Sep 12, 2012 at 17:34
  • sry about the alert. i was only using that for testing. deleted it. what does 'this' in the place of 'msg.d' do? I'm trying to populate the select with the data from the msg sent back from asp.net webmethod. Commented Sep 12, 2012 at 17:40

2 Answers 2

1

Instead of the .each loop, try:

for(var i=0; i < msg.d.length; i++) {
  $('#supplierSelect').append($('<option>').text(msg.d[i].name).attr('value', msg.d[i].value));
}
Sign up to request clarification or add additional context in comments.

Comments

0

The same thing can also be done using $.each function

$.each(msg.d , function(i){
  $('#supplierSelect').append($('<option>').text(msg.d[i].name).attr('value', msg.d[i].value));
}

Comments

Your Answer

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