Ritzel
Ritzel

Reputation: 33

How to check if string is in column value of returned DataRow array in C#?

I know this is very basic, but I can't seem to get it right. I have this datatable that I will fill with processed information from my database.

After searching if the EmpRequestID has already been added in the Datatable, I want to be able to get the value of the column named "RequestedEmp" of the returned row and check if already contains the initials that my variable is currently hosting (it is in a loop). If it does not, append the initials in the variable in the existing initials in the row.

DataRow[] MyEmpReq_Row = MyEmpRequests_DataTable.Select("EmpRequestID='" + EmpRequestID + "'");
int SameReqID = MyEmpReq_Row.Length;

if (MyEmpReq_Row > 0) //REQ ID IN DT, MULTIPLE EMP 1 REQUEST
{
    //INSERT HERE 
}
else //ID NOT IN DT YET
{
    MyEmpRequests_DataTable.Rows.Add(EmpRequestID, ActionBy, Requested_Initials, DateSubmitted, RequestStatus);
}

I want to be able to do something like this

string RetrievedInitials = MyEmpReq_Row["RequestedEmp"].ToString();

if (RetrievedInitials LIKE '%" + Requested_Initials + "'") // but if statements doesnt have LIKE

or this and then know if the column contains the value or not.

MyEmpReq_Row.Select("RequestedEmp LIKE '%" + Requested_Initials + "'");

Upvotes: 0

Views: 370

Answers (2)

Matt Smucker
Matt Smucker

Reputation: 5234

if (RetrievedInitials.Contains(Requested_Initials))

Upvotes: 1

remarkrm
remarkrm

Reputation: 159

Take a look at the string class: http://msdn.microsoft.com/en-us/library/system.string.aspx

As already mentioned by some other posters, the Contains() method may be of use. However, you should also look at EndsWith() (which is more in line with your use of the wildcard in your LIKE query) and StartsWith().

Example:

if (RetrievedInitials.EndsWith(Requested_Initials))

Upvotes: 0

Related Questions