Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dataset Manipulation

Status
Not open for further replies.

Elegabalus

Programmer
Jan 13, 2005
71
CA
Probably a simple question: I've got a dataset of data I'm getting from a database.

I want to be able to make some changes to the results based on certain criteria (i.e., pseudocode: if employeeID in (1,2,3,4,5) then employeename = employeename + " - manager";)

Problem is that I'm not sure how to do this to a dataset.

Code:
public DataSet GetEmps()
{
clsEmps all_emps = new clsEmps();
DataSet dsEmps = all_emps.dsAllEmps();

if (dsEmps.Tables.Count > 0)
{
	foreach (DataRow row in dsEmps.Tables[0].Rows)
	{
		if (row["EmpID"].ToString() == "1" || row["EmpID"].ToString() == "2")
		{
			_strEmp_Name = row["Emp_Name"].ToString() + " - manager";
		}
		else
		{
			_strEmp_Name = row["Emp_Name"].ToString();
		}
	}
}

else
{
	_strEmp_Name = "";
}

return dsEmps;
}

This doesn't work...it only returns the dataset straight form the database.

The output should be:

Bill - manager
Joe - manager
Jeff
Bob

However, it is:

Bill
Joe
Jeff
Bob

Any help is appreciated.
 
You are assing your value to a variable not to the dataset column. If you are using a stored procedure to populate your data set, I would do the code in there instead.
 
In other words you can do this
Code:
if (dsEmps.Tables.Count > 0)
			{
				foreach (DataRow row in dsEmps.Tables[0].Rows)
				{
					if (row["EmpID"].ToString() == "1" || row["EmpID"].ToString() == "2")
					{
						 
						row["EmpID"] = row["Emp_Name"].ToString() + " - manager";
					}
					else
					{
						// not needed 
					}
				}
			}
or you can create second DataTable, fill there from the first data table+ changes and then use the second DataTab
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top