Reputation: 1428
I want to be able to evaluate a dataset with a string. In an SQL statement I will I am pulling the data with a dataset, and if the dataset matches the string, I want to do something. I can't figure out the syntax to do that.
Here is my code:
var sql = new SQL_Statements();
var stringSql = "select type from po where po_num=" + stringPO_NUM;
const string stringLumber = "Lumber";
var sql_ds = sql.SelectFromDB(stringSql);
if (sql_ds.Tables[0].Rows.ToString() == stringLumber)
{
//Lets do something.
}
The dataset is pulling the word Lumber from the table. Can some please tell me how I can do this?
Upvotes: 2
Views: 419
Reputation: 365
Try this. This will iterate through each row to see if it matches your string, then do something if that row does. So you may need a little bit more code to finish it off.
foreach (DataTable table in sql_ds.Tables)
{
foreach (DataRow row in table.Rows)
{
if (row.Field["ROW_NAME"].ToString() == stringLumber)
{
//Lets do something.
}
}
}
Upvotes: 0
Reputation: 3456
The problem right now is you are calling .ToString on the row. That will likely return the data type of the row which does you no good.
You need to choose a particular row, then a particular columns value inside the row. If there is only one row returned you can try:
sql_ds.Tables[0].Rows[0][0].ToString() == stringLumber
This will get the first row in your collection, then the first column. You mentioned there could be spaces in the results, so you can add a .Trim() if needed to remove whitespace at the beggining or end of your string.
Upvotes: 0
Reputation: 12815
You are calling ToString
on the DataRowCollection
rather than the cell value being returned.
As your query is only returning one cell in the row you can access the first cell by indexing the DataRow
directly:
if (sql_ds.Tables[0].Rows[0][0].ToString() == stringLumber)
In Rows[0][0]
the first 0
is the row index and the second is the column index.
Upvotes: 1