0

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.

Rohan Rao
  • 2,505
  • 3
  • 19
  • 39
  • Does this answer your question? [How to get a DataRow out the current row of a DataReader?](https://stackoverflow.com/questions/18511700/how-to-get-a-datarow-out-the-current-row-of-a-datareader) – Jawad Jan 08 '20 at 04:52
  • @Jawad, Thanks that somewhat helped. I used dt.Rows in foreach loop. However, in debug mode I am getting the above parent class instead of actual string. – Rohan Rao Jan 08 '20 at 04:58
  • 1
    It doesn't seem like that code that you've put in the question shows us where the string `System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]]` is coming from. It appears that you're effectively doing a `.ToString()` on the `list` instance. But I can't see that in the code. You need to provide a [mcve]. – Enigmativity Jan 08 '20 at 05:48
  • @Enigmativity please see my updated question. – Rohan Rao Jan 08 '20 at 08:24
  • @RohanRao - You still haven't provided a [mcve] of your code. As one example, the end of the `GetChats` method is missing. – Enigmativity Jan 09 '20 at 01:13

2 Answers2

0

I don't get your question properly but you can try below code or share some more info

int idn=0;
foreach (DataRow row in dt.Rows)
{
    string queMsg = row["Description"].ToString();
    list.Add(new KeyValuePair<int, string>(idn, queMsg));
    idn=idn+1;
}
Shridhar
  • 2,258
  • 2
  • 12
  • 13
0

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. :)

Rufus L
  • 36,127
  • 5
  • 30
  • 43