0

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);
         
            }
  • There is no constructor for SqlAdapter that takes a single string. I'm pretty sure that you cannot throw json data at a SqlAdapter like this. Search for other solutions. – Palle Due Jan 28 '21 at 07:42
  • I know but how can i solve that – fazal abbas Jan 28 '21 at 08:59
  • You cannot use a SqlAdapter. I don't know what your webservice is returning, but if it's JSON have a look at this: https://stackoverflow.com/questions/11981282/convert-json-to-datatable. Personally I would skip the DataTable altogether and just deserialize the data and add it to the checkboxlist. There is no need to go through a DataTable. – Palle Due Jan 28 '21 at 09:45
  • The problem has nothing todo with WebAPI or CheckedListBox. To keep your post focused on the problem, please correct the tags and also cleanup the code and just keep the relevant code. – Reza Aghaei Jan 28 '21 at 10:07

1 Answers1

0
     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; }