Click here to Skip to main content
16,017,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay so what i have is a list box and abunch of text boxes, i have the list box filling with usernames from a mysql db

what i want to do is When u select a name on the listbox, have all there data show up in the text boxes (username, pass, bod, email,)
all that, but i have no clue how to do this, here is what i have so far

C#
myConnection.GetConnection();
            DataSet sds = new DataSet();
            MySqlCommand sql = new MySqlCommand("SELECT * FROM users", myConnection.GetConnection());
            MySqlDataAdapter mydap = new MySqlDataAdapter(sql);
            mydap.Fill(sds, "users");
            this.UserList.DataSource = sds.Tables["users"];
            this.UserList.DisplayMember = "name";
            this.UserList.ValueMember = "id";


And then on listbox selection change i have this


C#
private void UserList_SelectedIndexChanged(object sender, EventArgs e)
{
    Usernametxtbox.Text = this.UserList.DisplayMember;
}



but of course that doesn't work

So what im asking is how would i do this because i have no clue and everything ive tried, does not work, so im asking for some help on how to do this
Posted
Comments
Henry Minute 20-Oct-10 9:50am    
Yes. Although I suspect that using the ID would be more efficient.
Tunacha 20-Oct-10 11:06am    
Well this is what i have so far
<pre lang="cs">
DataSet sds = new DataSet();
MySqlCommand sql = new MySqlCommand("SELECT * FROM users", myConnection.GetConnection());
MySqlDataAdapter mydap = new MySqlDataAdapter(sql);
mydap.Fill(sds, "users");
this.UserList.DataSource = sds.Tables["users"];
this.UserList.DisplayMember = "name";
this.UserList.ValueMember = "id";


}

private void UserList_SelectedIndexChanged(object sender, EventArgs e)
{
myConnection.GetConnection();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE name = '"+this.UserList.SelectedValue+"'");
MySqlDataReader reader = cmd.ExecuteReader();
Usernametxtbox.Text = reader.GetString("name");
ranktxtbox.Text = reader.GetString("groups");
}
<pre>


But i still cant figure out what im doing wrong,

1 solution

What you need to do is to use the selected username/ID to query the database again to retrieve the data for that user.

A very rough query would look something like:

SQL
SELECT field1, field2, etc FROM users WHERE ID = @ID


Use a parameterized query (search on that to find out how) to pass the correct ID for each query.

You can then use the results of the query to populate your controls.
 
Share this answer
 
Comments
Tunacha 20-Oct-10 9:49am    
So basicaly what your saying is i would have to make it query the Selected name then use the retrieved data to fill the text boxes?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900