Create ArrayList from SQL and Convert ArrayList to String for display in Messagebox RRS feed

  • Question

  • I have the followign code where I build an ArrayList from the results of an SQL qquery and then want to display the resulting ArrayList in a messagebox.  I've attached the code below, but my messagebox doesn't display the array list but rather the text string "System.Collections.ArrayList". 

    I think i'm close and tried the solutiosn for all the questions for "Covert ArrayList to String" but nothing seems to be working.  is my ArrayList not compiling correctly?  it seems to be looping through the values in the SQl table correctly during the "while (dr.Read())" portion of the code so I assumed it was running correctly.

    StringBuilder sqlBase = new StringBuilder();

    StringBuilder sql = new StringBuilder();

    SqlDataReader dr = null;


    cmd = util.SqlConn.CreateCommand();


    AcceptableCurveFamilyCodes = new ArrayList(); 

    string msg = "";

    sql.Length = 0;

    sql.AppendLine("select distinct curve_family_code from tbl_curve_family order by 1");

    cmd.CommandText = sql.ToString();

    dr = cmd.ExecuteReader();

    while (dr.Read())




    //string msg = string.Join(Environment.NewLine, ArrayList);

    //var message = string.Join(",", AcceptableCurveFamilyCodes);

    Tuesday, May 8, 2012 9:04 PM


All replies

  • You're using the wrong type.

    Change the AcceptableCurveFamilyCodes type from ArrayList to List<string>, then use the following instruction to create the string to show in the MessageBox (the second one you have commented out):

    var message = string.Join(", ", AcceptableCurveFamilyCodes);

    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    Tuesday, May 8, 2012 9:19 PM
  • Okay so I changed the type to a List and added in the var msg script, but i get these 2 errors.  Both seem realted to the var msg line since if I comment this out it appears to run fine.

    Is there any way to view the items being added to the List (or ArrayList for that matter) easily?  This whole var msg and MessageBox thing is unnecessarily complicated so if there's another way I'm all ears.

    Error 1 The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments C:\Data\VS2008\AmortClient\AmortClient\Importer.cs 171 31 AmortClient.

    Error 2 Argument '2': cannot convert from 'System.Collections.Generic.List<string>' to 'string[]' C:\Data\VS2008\AmortClient\AmortClient\Importer.cs 171 49 AmortClient

    Wednesday, May 9, 2012 12:25 PM
  • OK, so you just need to cast the AccptableCurveFamilyCodes list to string array:

    var message = string.Join(", ", AcceptableCurveFamilyCodes.ToArray());

    But if your problem is only to see the content of AccptableCurveFamilyCodes, you can easily do that using the Watch window while you're in debug: http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-ndash-the-watch-window.aspx.

    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    • Proposed as answer by Mike Feng Thursday, May 10, 2012 11:47 AM
    • Marked as answer by Mike Feng Thursday, May 17, 2012 11:12 AM
    Wednesday, May 9, 2012 12:33 PM