Search code examples
c#sql-serverbotframeworkkeyvaluepair

getting System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]] instead of string


I am working on a MS Bot Framework project where I am retrieving the value from a key-value pair from database in C#. Previously I had this:

var list = new List<KeyValuePair<int, string>>()
{
     new KeyValuePair<int, string>(obj,_Obj.Questions)
};

Dictionary<int, string> d = new Dictionary<int, string>
{
  { 1, "Welcome, How are you?" },
  { 2, "Tell me something about yourself."},
  { 3, "How much experience do you have?"},
};

My goal was to bring the Values such as "Welcome, How are you?", "Tell me something about yourself", etc from database. In order to achieve that I did this:

Edit:

Questions.cs

public static string GetChats(int Id)
{


   using (SqlCommand cmd = new SqlCommand("usp_FetchData", con))
   {
   var list = new List<KeyValuePair<int, string>>();

   DataTable dt = new DataTable();
   cmd.CommandType = System.Data.CommandType.StoredProcedure;

   cmd.Parameters.AddWithValue("@id", Id);

   SqlDataAdapter da = new SqlDataAdapter(cmd);
   da.Fill(dt);

   con.Open();

   SqlDataReader reader = cmd.ExecuteReader();

   if (reader.HasRows)
   {
      foreach (DataRow row in dt.Rows)
      {
        string queMsg = row["Description"]?.ToString();
        list.Add(new KeyValuePair<int, string>(Id, queMsg));
      }
    }

 // class property

 public string WelcomeStmt = GetChats(1).ToString();

And the value from above function is getting in this method:

MyDialog.cs // this is the dialog for the Bot

private static async Task<DialogTurnResult> **NameStepAsync**(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{

  return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text(questions.AskName) }, cancellationToken);
}

I am passing the Id value as 1 in GetChats(Id) method. So based on that, I should get the corresponding Value.

In the NameStepAsync method, I am receiving an unusual parent class I guess instead of actual string that I am expecting.:

System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]].

Does anybody know why this is happening?

Thanks.


Solution

  • The output you're describing is the default behavior of the Object.ToString method, which is to return the fully qualified name of the type of the object. Since List<T> does not override ToString, you are seeing the output you've described.

    It can be reproduced by:

    Console.WriteLine(list.ToString());   // Or just: Console.WriteLine(list);
    

    enter image description here

    If the intent is to output the Value property of each KeyValuePair in the list, we need to first select that property from each item in the list. For example:

    var list = new List<KeyValuePair<int, string>>()
    {
        new KeyValuePair<int, string> (1, "Welcome, How are you?" ),
        new KeyValuePair<int, string> (2, "Tell me something about yourself."),
        new KeyValuePair<int, string> (3, "How much experience do you have?"),
    };
    
    list.ForEach(kvp => Console.WriteLine(kvp.Value));
    

    enter image description here

    I realize this answer ignores a great portion of the code you provided, but it doesn't appear to me that the code you've shown is what's causing the output you described. Hopefully this helps some. :)