Click here to Skip to main content
14,870,660 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get a API and need to bind few drop down in asp.net c# with async Task. I tried a lot, i m getting response but my drop down menu not binding.

I am not getting any type of error while debugging.

What I have tried:

How ever i convert the response to datatable and trying to bind the drop down with that data table, but i m not able to bind.

Here is the code i m using

C#
public async Task Getlist()
{
    var stringContent = new StringContent("{ \"appcode\": \"mice@443\" }", 
    System.Text.Encoding.UTF8, "application/json");

    using (var client = new HttpClient())
    {
        
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        //var department = new Params() { appcode = "mice@443" };
        HttpResponseMessage response = await client.PostAsync("http://IP_Address_of Server/itsupport/api/http.php/getSubDept_UnitMap_id", stringContent);
        var result = await response.Content.ReadAsStringAsync();
        if (response.IsSuccessStatusCode)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Rootobject blogObject = js.Deserialize<rootobject>(result);

            var department = blogObject.result.getDepartment;
            DataTable dtDepartment = new DataTable();
            dtDepartment.Columns.Add("id", typeof(string));
            dtDepartment.Columns.Add("name", typeof(string));

            dtDepartment.Rows.Add(department.id, department.name);



            //foreach (DataRow row in dtDepartment.Rows)
            //{
            //    ListItem lstitem = new ListItem();
            //    lstitem.Text = row["name"].ToString();
            //    lstitem.Value = row["id"].ToString();
            //    ddl_dept.Items.Add(lstitem);

            //}

            ddl_dept.DataSource = dtDepartment;
            ddl_dept.DataTextField = "name";
            ddl_dept.DataValueField = "id";
            ddl_dept.DataBind();

            // Get the URI of the created resource.  
            // Uri returnUrl = response.Headers.Location;
            //  Console.WriteLine(returnUrl);

        }
    }
}

protected void ddl_Service_SelectedIndexChanged(object sender, EventArgs e)
    {
        new Task(() =>
        {
            Getlist();
        }).Start();
    }

public class Rootobject
{
    public int status { get; set; }
    public string message { get; set; }
    public Result result { get; set; }
}

public class Result
{
    public Getsubdepartment getSubDepartment { get; set; }
    public Getdepartment getDepartment { get; set; }       
    public bool disable { get; set; }


}

public class Getsubdepartment
{
    public int id { get; set; }
    public string name { get; set; }
}

public class Getdepartment
{
    public int id { get; set; }
    public string name { get; set; }
}
Posted
Updated 1-Apr-21 0:34am
v2

1 solution

Done this with Using..


protected async void ddl_Service_SelectedIndexChangedAsync(object sender, EventArgs e)
{
//new Task(() =>
//{
// Getlist();
//}).Start();

// await Task.Run(() => Getlist());

await Getlist();


}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900