Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
name is a list of items having first name of students . I need to update student column that starts with elements in name .

list<string> name = {"william","tom", " harry",......}

student column values are "williamt smith", "tomhardy" , harry philip"...like so .

how to update column that starts with some values ..

What I have tried:

foreach (var i in name)
{
cmd.CommandText = "update students SET Progress = 'Good' where Student like '" + i% + "' ";
cmd.ExecuteNonQuery();
}
Posted
Updated 28-Jun-21 2:59am
Comments
Richard MacCutchan 28-Jun-21 8:03am    
What happens if you have more than one person with the same forename?
Member 15212425 28-Jun-21 8:15am    
yes that one will also get updated . when i'm doing Student like '" + i% + "' ";
itz not working . how to code % on that ??

1 solution

The wildcard character % has to be part of the command string. i.e.
C#
cmd.CommandText = "update students SET Progress = 'Good' where Student like '" + i + "%' ";

However, you should be using a Parameterised query here
C#
cmd.CommandText = "update students SET Progress = 'Good' where Student like @name";
string searchName = string.Format("{0}%",i);
cmd.Parameters.Add(new SqlParameter("@name", searchName));
cmd.ExecuteNonQuery();

Also note the comment from Richard - using forenames like this is not a good idea
 
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