Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery split comma delimited from ashx handler

Tags:

jquery

ashx

I am having trouble splitting a comma delimited string into an array. In my ashx handler page, my string looks like this:

context.Response.Write(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9} ", BusProfileID, BusinessName, BusinessPhone, BusinessEmail, BusinessAddress, BusinessCity, BusinessState, BusinessZip, BusinessWebsite, BusinessCategory));

When I try to do an array the results are not shown.

   <script>
        $(document).ready(function () {
            $('#ContentPlaceHolder1_businessSelect').change(function () {
                $.ajax({
                    contentType: "text/html; charset=utf-8",
                    data: "ID=" + $('#ContentPlaceHolder1_businessSelect').val(),
                    url: "getBusValue.ashx",
                    dataType: "text",
                    success: function (data) {
                    var vardata = JSON.stringify(data)
                    var arr = vardata.split(',')
                    $("#ContentPlaceHolder1_BusProfileID").val(arr[0]);
                    $("#ContentPlaceHolder1_BusinessName").val(arr[1];
                    $("#ContentPlaceHolder1_BusinessPhone").val(arr[2]);
                    $("#ContentPlaceHolder1_BusinessEmail").val(arr[3]);
                    $("#ContentPlaceHolder1_BusinessAddress").val(arr[4]);
                    $("#ContentPlaceHolder1_BusinessCity").val(arr[5]);
                  $("#ContentPlaceHolder1_BusinessState").val(arr[6]).prop('selected',true);
                    $("#ContentPlaceHolder1_BusinessZip").val(arr[7]);
                    $("#ContentPlaceHolder1_BusinessWebsite").val(arr[8]);
                   $("#ContentPlaceHolder1_BusinessCategory").val(arr[9]).prop('selected', true);
                    }
                });
            });
        });
    </script>

Here is my ashx page:

   public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/html";
        string ID = context.Request.QueryString["ID"];
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("SELECT BusProfileID, BusinessName, BusinessPhone, BusinessEmail, BusinessAddress, BusinessCity, BusinessState, BusinessZip, BusinessWebsite, BusinessCategory FROM [BusProfile] WHERE BusinessName = @BusinessName", conn);
        comm.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar);
        comm.Parameters["@BusinessName"].Value = ID;
        try
        {
            conn.Open();
            reader = comm.ExecuteReader();
            if (reader.Read())
            {
                string BusProfileID = reader["BusProfileID"].ToString();
                string BusinessName = reader["BusinessName"].ToString();
                string BusinessPhone = reader["BusinessPhone"].ToString();
                string BusinessEmail = reader["BusinessEmail"].ToString();
                string BusinessAddress = reader["BusinessAddress"].ToString();
                string BusinessCity = reader["BusinessCity"].ToString();
                string BusinessState = reader["BusinessState"].ToString();
                string BusinessZip = reader["BusinessZip"].ToString();
                string BusinessWebsite = reader["BusinessWebsite"].ToString();
                string BusinessCategory = reader["BusinessCategory"].ToString();

                context.Response.Write(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9} ", BusProfileID, BusinessName, BusinessPhone, BusinessEmail, BusinessAddress, BusinessCity, BusinessState, BusinessZip, BusinessWebsite, BusinessCategory));
            }
            reader.Close();
        }

        finally
        {
            conn.Close();
        }
    }

This is how the data looks like in the textboxes upon success:

8,My Business Inc,(702) 555-1212,[email protected],555 anywhere street,Los Angeles,California,44502,google.com,Hotel & Travel
like image 648
Dan Nick Avatar asked Dec 02 '25 10:12

Dan Nick


1 Answers

Here is a demonstration of the code.

var string = "8,my business inc,(702) 555-1212,[email protected],555 anywhere street,Los Angeles,california,44502,google.com,hotel and ttravel";

var stringArray = string.split(',');

console.log(stringArray);

The result is here:

["8", "my business inc", "(702) 555-1212", "[email protected]", "555 anywhere street", "Los Angeles", "california", "44502", "google.com", "hotel and ttravel"]

console.log(xm[5]);

output: Los Angeles

if the data that you get on success is a string, then possibly here is how the flow goes.....

I don't think there is any error in the code that you've written.

if data is not a string, then

do var string = "" + data; to convert it to a string.

like image 196
Prashanth Benny Avatar answered Dec 05 '25 01:12

Prashanth Benny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!