Reputation: 103
Hey everyone I have the following code
Console.WriteLine("y: {0}", liveData.Tables["Players"].Rows.Count); //Shows full table
DataRow[] foundRows;
foundRows = liveData.Tables["Players"].Select("FName like 'Marc'");
foreach (DataRow dr in foundRows)
{
Console.WriteLine("Index is " + dr.Table.Rows.IndexOf(dr));
}
Now when i return my index of the row at variable dr that shows correctly
What i want to do is where it states Fname and Marc i want to pass those as variables and not manually input the string, is this possible?
Example, sometimes i may need to search Lname and whatever that is or a different first name?
Thank you for looking a bit stuck but again probably a simple answer :)
Upvotes: 2
Views: 72
Reputation: 3058
You could create a function
public DataRow[] MySelect(string column, string name)
{
return liveData.Tables["Players"].Select(string.Format("{0} LIKE '{1}'", column, name));
}
And then use it like this
foundRows = MySelect("LName", "John");
Upvotes: 0
Reputation: 697
If I am not mistaken, you wish to have the query select a name from FName that resembles an input from an user (in this case 'Marc').
If thats the case, your best bet is to have something ask for the input beforehand and assign that value into a variable from a textbox for example.
String Queryname = TextBox1.Text
Console.WriteLine("y: {0}", liveData.Tables["Players"].Rows.Count); //Shows full table
DataRow[] foundRows;
foundRows = liveData.Tables["Players"].Select("FName like " + Queryname);
foreach (DataRow dr in foundRows)
{
Console.WriteLine("Index is " + dr.Table.Rows.IndexOf(dr));
}
Forgive me if I did not get your question. Let me know if that works and if not, try clarifying with an example.
Upvotes: 1
Reputation: 2058
Can't you just build the string with variables?: .Select(param+" like '"+value+"'")
Upvotes: 0
Reputation: 870
string SelectQuery=//create query based on user input
foundRows = liveData.Tables["Players"].Select(SelectQuery);
For example if user wants to search based on Last Name, then Lname+ like + search term.You need to map the search attribute with column names.
Upvotes: 0