views:

232

answers:

1

Please tell me if there's an easy way to convert a DataTable to a HashTable or a SQLDataReader to a HashTable. I have to parse it through javascriptserializer. the code i m using, having some problem. please take a look and guide me.

try
{
    using (SqlConnection conn = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            dt.Load(dr);
        }
    }

    Hashtable sendData = new Hashtable();

    foreach (DataRow drIn in dt.Rows)
    {

        sendData.Add(drIn["orderNumber"].ToString(), drIn["customerName"].ToString());

    }

    sendData.Add("orderNum", order);
    JavaScriptSerializer jss = new JavaScriptSerializer();
    string output = jss.Serialize(sendData);
    return output;
}
catch (Exception ex)
{
    return ex.Message + "-" + ex.StackTrace;
}

It's giving a correct result when queried from one table in the database but from another table it's having a problem. Is there any other way to do this?

A: 

problem occur on browser "error: Expected ']'

but this problem occurs due to server side code because its working fine with other table of database. I have also matched column names with database table thousand of times. anyways below is my client side code:

 $.ajaxSetup({
    cache: false
    //timeout: 1000000
});

function concatObject(obj) {
    strArray = []; //new Array
    for (prop in obj) {
        strArray.push(prop + ": " + obj[prop]);
    }
    return strArray.join("<br />");
}


function orderMethod() {
    $.ajax({
        type: "POST",
        url: "orderSearch.aspx/SendMessage",
        data: "{order: '" + $('#order').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            resultData = eval("(" + result.d + ")");
            $("#rawResponse").html(concatObject(resultData));
        },
        error: function(result) {
            alert("jQuery Error:" + result.statusText);
        }
    });
}
amby