2

I am currently learning how to use database connections and how to read and write data. I am using Mysql data/connector 6.1 for visual studio. This supports access to a localhost mysql server.

I want to display personal information of the person logged in for which i use this code:

string query = "SELECT * FROM gebruiker WHERE id='" + id + "'";

List<string>[] list = new List<string>[5];
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>();


if (this.openConnection() == true)
{

    MySqlCommand cmd = new MySqlCommand(query, connection);
    //Een nieuw datareader object maken en dan query uitvoeren
    MySqlDataReader dataReader = cmd.ExecuteReader();


    while (dataReader.Read())
    {
        list[0].Add(dataReader["id"].ToString());
        list[1].Add(dataReader["voornaam"].ToString());
        list[2].Add(dataReader["achternaam"].ToString());
        list[3].Add(dataReader["geboortedatum"].ToString());
        list[4].Add(dataReader["Rol_id"].ToString());
    }

    dataReader.Close();

    this.closeConnection();

    return list;
}

I use Lists because I am following this tutorial: tutorial

Then when i return the list i want to get that data and add it to labels on my winform. this is the code i use:

DBconnect connectDB = new DBconnect();
List<string>[] persGegevens = connectDB.persoonlijkeGegevens(userId);
for (int i = 0; i <= persGegevens.Count(); i++)
{
    switch (i)
    {
        case 0:
            break;
        case 1:
            lblVoornaamVrbl.Text = persGegevens[i].ToString();
            break;
        case 2:
            lblAchternaamVrbl.Text = persGegevens[i].ToString();
            break;
        case 3:
            lblGbrtedatumVrbl.Text = persGegevens[i].ToString();
            break;
        case 4:
            lblFuntieVrbl.Text = persGegevens[i].ToString();
            break;
        default:
            break;
    }
}

But unfortunatly the code does not convert the data in List toString but the element will get converted toString. This is what i get: system.collections.generic.list`1[system.string] I also tried to do this with a foreach but that did not work because i can't specify which element has to be placed in what label.

Do you guys know what is the problem here? and is this the right way to retrieve data? or should i use something else to store my data?

5
  • Use a DataTable to make your code easier to work with stackoverflow.com/questions/11993211/… Commented May 26, 2016 at 12:54
  • 1
    @MurrayFoxcroft Never has using a DataTable for something like this made anything easier, in my experience Commented May 26, 2016 at 12:56
  • I would recommending looking into using Entity Framework and Linq it may make things a little easier for you to understand. Commented May 26, 2016 at 12:56
  • You should be using MySqlCommand parameters and not passing in id directly to the string. Take a look here for more info Commented May 26, 2016 at 12:59
  • Thanks for the suggestion guys. Commented May 26, 2016 at 13:53

3 Answers 3

2

I think nik0lias suggestion is formerly correct but maybe it can runs as desired if changed to :

    case 1:
        lblVoornaamVrbl.Text = persGegevens[i][ 0 ].ToString();
        break;
    case 2:
        lblAchternaamVrbl.Text = persGegevens[i][ 0 ].ToString();
        break;
    case 3:
        lblGbrtedatumVrbl.Text = persGegevens[i][ 0 ].ToString();
        break;
    case 4:
        lblFuntieVrbl.Text = persGegevens[i][ 0 ].ToString();

If I am right, consider if a List array List<>[] is needed or you can simplify your code with List this way:

    string query = "SELECT * FROM gebruiker WHERE id='" + id + "'";

    List<string> listGebruiker = new List<string>();


    if (this.openConnection() == true)
    {
        MySqlCommand cmd = new MySqlCommand(query, connection);
        //Een nieuw datareader object maken en dan query uitvoeren
        MySqlDataReader dataReader = cmd.ExecuteReader();


        while (dataReader.Read())
        {
            listGebruiker.Add(dataReader["id"].ToString());
            listGebruiker.Add(dataReader["voornaam"].ToString());
            listGebruiker.Add(dataReader["achternaam"].ToString());
            listGebruiker.Add(dataReader["geboortedatum"].ToString());
            listGebruiker.Add(dataReader["Rol_id"].ToString());
        }

        dataReader.Close();

        this.closeConnection();

        return list;
    }

and

    DBconnect connectDB = new DBconnect();
    List<string> persGegevens = connectDB.persoonlijkeGegevens(userId);

    lblVoornaamVrbl.Text = persGegevens[1];
    lblAchternaamVrbl.Text = persGegevens[2];
    lblGbrtedatumVrbl.Text = persGegevens[3];
    lblFuntieVrbl.Text = persGegevens[4];

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

It might indeed be a better solution to not use a list<> array but to use list. I did not think about that because i was following a tutorial. Thanks.
2

You are trying to do a ToString() on a list, hence the output. You need to specify where in the array or list you want to display and do a ToString() on. The below will work or you could use another variable to loop through each user to get their details.

DBconnect connectDB = new DBconnect();
    List<string>[] persGegevens = connectDB.persoonlijkeGegevens(userId);
    for (int i = 0; i <= persGegevens.Count(); i++)
    {
        switch (i)
        {
            case 0:
                break;
            case 1:
                lblVoornaamVrbl.Text = persGegevens[i][0].ToString();
                break;
            case 2:
                lblAchternaamVrbl.Text = persGegevens[i][1].ToString();
                break;
            case 3:
                lblGbrtedatumVrbl.Text = persGegevens[i][2].ToString();
                break;
            case 4:
                lblFuntieVrbl.Text = persGegevens[i][3].ToString();
                break;
            default:
                break;
        }
    }

1 Comment

This indeed works, I use PersGegevens[i][0] because every list on list<string>[] only has 1 element. Thanks for the help.
1

persGegevens[i] is of type List<string>, so persGegevens[i].ToString() wont give anything meaningful. You need something like:

persGegevens[i][j];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.