| |

VerySource

 Forgot password?
 Register
Search
View: 1252|Reply: 11

Ask: how to extract the text content of the listBox control?

[Copy link]

2

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-5 11:10:01
| Show all posts |Read mode
I want to call the text content in the listBox space in the database delete statement, but I don't know how to get the text content in this control, advice!
Such as textBox1.Text, similar to this text message
Reply

Use magic Report

0

Threads

56

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-1-5 14:18:01
| Show all posts
string str = listBox1.Items [i] .ToString ();
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-1-5 14:57:01
| Show all posts
For example, take out the text content of the currently selected item:

if (this.listBox1.SelectedItem! = null)
            {
                // For example, take it out and put it in a TextBox
                this.textBox1.Text = this.listBox1.SelectedItem.ToString ();
            }
Reply

Use magic Report

2

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-5 15:15:01
| Show all posts
Sorry, I change a question, listBox2.Text seems to take out the text content, it may be that my database execution statement is wrong, my code
string strconn;
strconn = "data source = (local); initial catalog = student_pl; Integrated Security = SSPI;";
SqlConnection cn1 = new SqlConnection (strconn);
cn1.Open ();
string s = "delete from score where cname = '" + listBox2.Text + "'";
SqlCommand cmd1 = new SqlCommand ();
cmd1.Connection = cn1;
cmd1.CommandText = s;
cmd1.ExecuteNonQuery ();

After the execution, the record I want to delete is not deleted in the database. What is wrong, please advise
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-1-5 15:21:02
| Show all posts
If you want to get the specified item:

            //E.g. Take out the first item
            this.textBox1 .Text = this.listBox1.Items [0] .ToString ();
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-1-5 15:39:01
| Show all posts
Check if this record exists in the database?
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-1-5 15:51:02
| Show all posts
TO: strconn = "data source = (local); initial catalog = student_pl; Integrated Security = SSPI;";


Or you better try with sa user ..

for example:

strconn = "server = .; database = student_pl; uid = sa; pwd = password";

Because I encountered this problem before when I used windows authentication to log in, you try it with sa user first.
Reply

Use magic Report

2

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-5 16:09:01
| Show all posts
I want to delete this record, my code doesn't know where the problem is
Reply

Use magic Report

0

Threads

56

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-1-5 16:12:01
| Show all posts
There seems to be nothing wrong with the code executed by the database ...
Pointing downstairs ...
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-1-5 16:27:01
| Show all posts
You try as I said, try it with sa user ..
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list