Thread: conversion problem

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    66

    conversion problem

    Hye guys, below is my code, i dont know why i cant get the actual result into txtTest.text, in debug mode, i did see the result that i want in list[1], without convert to string there will be an error saying that i need to do conversion, after do the conversion, the result is different. please help asap. thx

    Code:
      {
    
    
                    string myConnection = "datasource=localhost;database=erp;port=3306;username=root;password=123456";
                    MySqlConnection myConn = new MySqlConnection(myConnection);
    
    
                    try
                    {
                        myConn.Open();
                       
                    MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    
    
                        List<string>[] list = new List<string>[12];
                        list[0] = new List<string>();
                        list[1] = new List<string>();
                        list[2] = new List<string>();
                        list[3] = new List<string>();
                        list[4] = new List<string>();
                        list[5] = new List<string>();
                     
    
    
                         MySqlCommand cmd = new MySqlCommand("SELECT * FROM erp_customer WHERE customer_name = ('" + username_txt.Text + "')", myConn);
                        cmd.ExecuteNonQuery();
    
    
                        //add
                         while (dataReader.Read())
                    {
    
                        list[0].Add(dataReader["customer_id"] + "");
                        list[1].Add(dataReader["customer_name"] + "");
                        list[2].Add(dataReader["customer_telno"] + "");
                        list[3].Add(dataReader["customer_address"] + "");
                        list[4].Add(dataReader["customer_company"] + "");
                        list[5].Add(dataReader["customer_title"] + "");
                       
                      }
    
    
                         txtTest.text = convert.toString(List[1]);     //<-------Here's the problem
    
    
                    //close Data Reader
                    dataReader.Close();
                        //end
                    }
    
    
                    catch (MySqlException ex)
                    {
    
    
                        MessageBox.Show("Can't connect to database\n" + ex.ToString());
                    }
    
    
                    finally
                    {
                        //MessageBox.Show("added success", "success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        //myConn.Close();
                    }
                }
            }
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    This isn't the code you are running, it won't even compile. Given that, you are telling it to convert a List<T> into a string. Remember, the 'list' variable is an array of List<T>, so 'list[1]' is the 2nd List<T> in the array. Did you want a specific element from this list?

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You have created more than one list there. The compiler does not know how to convert an entire list into a string. Remove the array from the list declaration and use 1 list. Then access the list using indexes.

    Code:
    List<string> list = new List<string>();
                     
                         MySqlCommand cmd = new MySqlCommand("SELECT * FROM erp_customer WHERE customer_name = ('" + username_txt.Text + "')", myConn);
                        cmd.ExecuteNonQuery();
    
    
                        //add
                         while (dataReader.Read())
                    {
    
                        list.Add(dataReader["customer_id"] + "");
                        list.Add(dataReader["customer_name"] + "");
                        list.Add(dataReader["customer_telno"] + "");
                        list.Add(dataReader["customer_address"] + "");
                        list.Add(dataReader["customer_company"] + "");
                        list.Add(dataReader["customer_title"] + "");
                       
                      }
                      
                      txtTest.text = list[1]
    No need for an array of lists here. Also since it is a list of strings there is no need to convert the element in the list to a string.

    If you really need an array of lists then you will have to wrap the List class and override the ToString() method and provide your specific implementation which will turn an entire list into a string.

    I should also mention you might want to bind txtTest.Text to a property in your class which fires INotifyPropertyChanged. To alter the text in the textbox you alter the property. No need to access the control directly.
    Last edited by VirtualAce; 08-24-2013 at 03:59 AM.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    66
    Quote Originally Posted by Momerath View Post
    This isn't the code you are running, it won't even compile. Given that, you are telling it to convert a List<T> into a string. Remember, the 'list' variable is an array of List<T>, so 'list[1]' is the 2nd List<T> in the array. Did you want a specific element from this list?
    yes. how?

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Read my post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer conversion problem
    By CDude in forum C Programming
    Replies: 4
    Last Post: 08-22-2012, 08:48 AM
  2. Yen to Dollars conversion problem
    By JJohnson in forum C++ Programming
    Replies: 12
    Last Post: 05-14-2011, 04:00 PM
  3. Microsoft -> ISO conversion problem
    By murdercityriot in forum C++ Programming
    Replies: 18
    Last Post: 08-07-2008, 09:03 AM
  4. TYpe Conversion Problem
    By q105 in forum C Programming
    Replies: 3
    Last Post: 03-10-2006, 02:05 AM
  5. Base conversion problem!
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-08-2001, 07:00 PM

Tags for this Thread