Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

How to add load data in to checkedboxlist using web api C# winforms

data not loading in checkedboxlist in winforms when using web api C# . Error : cannot convert string to system.Sqlclient.sqldata.sqlcommand

            **what i have tried.**
            string ComId = Login.usercomid;
            string UName = Login.recuser;

            HttpClient clientongtask = new HttpClient();
            clientongtask.BaseAddress = new Uri("https://localhost:44342/");
            HttpResponseMessage responseongtask = clientongtask.GetAsync("api/Values/GetOngTasklist/" 
             + UName + "/" + ComId).Result;
            string emp = responseongtask.Content.ReadAsStringAsync().Result;

            SqlDataAdapter sda = new SqlDataAdapter(emp);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
               

                GetTaskID getTaskID = new GetTaskID { Task = dt.Rows[i]["Task"].ToString(), Id = 
                Convert.ToInt32(dt.Rows[i]["Id"]), Enabled = false };

                checkedListBoxongoing.Items.Add(getTaskID, getTaskID.Enabled);
         
            }
question from:https://stackoverflow.com/questions/65932768/how-to-add-load-data-in-to-checkedboxlist-using-web-api-c-sharp-winforms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
     string emp = responseongtask.Content.ReadAsStringAsync().Result;

(emp) Returned will be a json object you need to Deserialize it to mode.

   using Newtonsoft.Json;

  var data= JsonConvert.DeserializeObject<List<T>>(emp);

/// T here id you class/model(GetTaskID) you can now do this

       foreach(var d in data)
       {
           GetTaskID getTaskID = new GetTaskID
             {
               Task = d.Task,
               Id = d.Id,
               Enabled = false
             };
            checkedListBoxongoing.Items.Add(getTaskID, getTaskID.Enabled);
       }

// To Deserialize below might be required in your model

    [JsonProperty("Id")]
     public int Id { get; set; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...