Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can I click on one of the items in the box list, such as the ID. name . Show the mobile number in the database in the Tx Box<

What I have tried:

private void listBox1_Click(object sender, EventArgs e)
      {
          int id = Convert.ToInt32(listBox1.Text);
         string query = "select * from rgstude where name = '" + listBox1.Text + "' ";
        SqlCommand cmd = new SqlCommand(query,con);
             txtname.Text = Name;
      }
Posted
Updated 31-May-19 23:36pm
Comments
Richard MacCutchan 1-Jun-19 3:50am    
What happens when you try it?
You created a SQL command but you do not execute it, or check that it returns a valid result.

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And ... in order to read information from the DB, you will need two things:
1) An Open connection to the DB. Don't share connections, create them and open them when you need them. They are scarce resources and it can cause problems later if you try to recycle them. Ideally, a using block around the connection and command is recommended.
2) Creating an SqlCommand does not mean that SQL will execute it! You need to call ExecuteReader, ExecuteScalar, ExecuteReader, or one of the DataAdapter methods to actually interface with the DB system.
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Id, description FROM myTable where MyName=@Name", con))
        {
        cmd.Parameters.AddWithValue("@Name",tbUserName.Text);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["Id"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", id, desc);
                }
            }
        }
    }
 
Share this answer
 
I want to display an ID in the text box by clicking on it
 
Share this answer
 
Comments
Richard MacCutchan 1-Jun-19 5:29am    
This is not a solution, and nor is it a valid question. Please dit your question if you have additional details.
munib ahmadi 1-Jun-19 6:22am    
yes
That's what you said right
But I kicked the keyrie database
I showed the name in the list box
Now, by clicking on the name, I want to display the other information in the text box 
 
Share this answer
 
Comments
Richard MacCutchan 1-Jun-19 6:36am    
Please read my comment above, and stop posting vague information as "solutions".

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