This is a newbie question, I am trying to build a class in C# that is going to set the UserOrgs property for a user (each user can have more than 1)
I have this thus far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// Summary description for clsRepUser
/// </summary>
public class clsUser
{
private string userid;
private List<string> userorgs;
public string UserID
{
get
{
return userid;
}
set
{
userid = value;
}
}
public List<string> UserOrgs
{
get
{
return userorgs;
}
set
{
userorgs = value;
}
}
clsConn cCon = new clsConn();
String connStr = "";
public clsUser()
{
}
public DataSet GetUserOrg(string UserID)
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(cCon.getConn());
SqlCommand cmd = new SqlCommand("sp_getUserOrgs", conn);
// 2. set the command object so it knows
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which
// will be passed to the stored procedure
cmd.Parameters.Add(
new SqlParameter("@UserID", UserID));
try
{
// Open the connection and execute the Command
conn.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = cmd;
sqlDA.Fill(ds);
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return ds;
}
}
How do I now populate the UserOrgs property of that user from the GetUserOrg function? Or am I way off on this?
You are using a stored procedure so it’s hard to give you an exact code, but the idea is to go through all rows of your DataSet, and add the element of that row to your list using .Add() method. Something like this: