Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:18:41+00:00 2026-05-28T08:18:41+00:00

I am getting data back from my C# WebMethod. I’m not sure how to

  • 0

I am getting data back from my C# WebMethod. I’m not sure how to bind the Key and Value from my WebMethod to my dropdown:

 <select name="rest" id="rest" maxlength="50" style="width: 200px;"></select>

C#

 [WebMethod]
public static Dictionary<string, string> LoadRestByCityState(string city, string state)
{
        DataSet ds = new DataSet();
        Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Data"]);
        DbCommand dbCommand = db.GetStoredProcCommand("sel_RestByCityState_p");
        db.AddInParameter(dbCommand, "@pListCity", DbType.String, city);
        db.AddInParameter(dbCommand, "@pListState", DbType.String, state);
        ds = db.ExecuteDataSet(dbCommand);

        Dictionary<string, string> rest = new Dictionary<string,string>();

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            rest.Add(row[0].ToString(), row[1].ToString());
        }
            return rest;

}

jQuery

  function LoadRest() {
        __state = $("#State :selected").val();
        __state = '"' + __state + '"'
        __city = $("#City :selected").val();
        __city = '"' + __city + '"'
        $.ajax({
            type: "POST",
            url: "Default.aspx/LoadRestByCityState",
            data: '{"city":' + __city + ',"state":'+ __state +'}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success:
                function (data) {
                    $.each(data[0], function(key....not sure about this stuff
                    alert('parks loaded');
                },
            fail: function () {
                alert("Error.");
            }
        });
        return false;
    }     

Data
This is being passed to WebMethod

{
"state": "AL",
"city": "Auburn"
}

When debugging my WebMethod, I hover over ‘rest’ and it shows the Key and Value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:18:41+00:00Added an answer on May 28, 2026 at 8:18 am

    Don’t use a dictionary as this doesn’t serialize as a collection. Use a list:

    public static List<KeyValuePair<string, string>> LoadRestByCityState(string city, string state)
    {
        ...
    }
    

    And then:

    success: function (data) {
        var rest = $('#rest');
        rest.empty();
        $.each(data.d, function(index, item) {
            rest.append(
                $('<option/>', {
                    value: item.Key,
                    text: item.Value
                })
            );
        });
    }
    

    also make sure to properly encode your values before sending them. I would recommend you using the JSON.stringify method because if the city has a quote in its name your request will break:

    data: JSON.stringify({city: __city, state: __state }),
    

    The JSON.stringify method is built into modern browsers but if you need to support legacy browsers you could include the json2.js script to your page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting xml in a object back from a soap service. <data><name>Test</name><name>Test</name><name>Test</name></data> I
If I'm getting my data from Mysql like so: $result = $dbConnector->Query(SELECT * FROM
I'm getting JSON back in this format from my C# webmethod: {d:[ADAMS CITY,BOULDER,ANTON,ARBOLES]} I
I am getting json data as a response back from the server. I want
Hi i want to add a datarow that im getting back from a data
I'm trying to figure out a clean way of getting data back from my
I am currently getting the following data back from the server: { d: [
We are getting an error in a VB6 application that sends data back and
how can i getting data from listbox in second form named listform while i
I'm importing from a CSV and getting data roughly in the format { 'Field1'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.