Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i tried this code but it doesn't work the label show as same as his old text name label25 ,what is the problem?

What I have tried:

C#
string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:/ALL/Database311.mdb";
OleDbConnection cn = new OleDbConnection(connectString);

cn.Open();
string selectString = "SELECT name FROM tabel22 WHERE name='" + label25.Text+ "'";
OleDbCommand cmd = new OleDbCommand(selectString, cn);
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
    label25.Text = reader.GetString(reader.GetOrdinal("name"));
}


cn.Close();
Posted
Updated 21-Oct-19 7:13am
v3
Comments
Member 14630006 21-Oct-19 12:33pm    
how to show the second name in tabel22

Here's what your sql does:

Give me 'Name' where 'Name' is "Doug" and you're surprised that it's Doug?

Take another look at the sql. step through it and check the values.
 
Share this answer
 
Comments
Member 14630006 21-Oct-19 12:24pm    
thanks alot i tried this and it worked
string selectString = "SELECT name FROM tabel22";
Glad you have resolved your logic error...

Now onto more important things- This code has a SQL Injection Vulnerability
string = "SELECT name FROM tabel22 WHERE name='" + label25.Text+ "'"

The proper way to take user input and place within an SQL Command is to utilize parameters; which will properly escape the user entry.
C#
string selectString = "SELECT name FROM tabel22 WHERE name= ?";
OleDbCommand cmd = new OleDbCommand(selectString, cn);
cmd.Parameters.AddWithValue("@name", label25.Text);
OleDbDataReader reader = cmd.ExecuteReader();
 
Share this answer
 
Comments
Member 14630006 23-Oct-19 10:27am    
oh thanks a lot but where user gonna enter his input?
MadMyche 23-Oct-19 13:32pm    
Nothing changed with entry, still label25. It goes into the query here:
cmd.Parameters.AddWithValue("@name", label25.Text);
Member 14630006 24-Oct-19 7:47am    
it not shown anything label25 stil label25
The code is correct, you search for the same name and it will be shown again :)
 
Share this answer
 

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