I have table which has composite keys in order to retrieve data from two different tables. I have created stored procedures to do that and it works fine:
Stored procedure:
ALTER PROC dbo.spp_adm_user_user_group_sel
AS
BEGIN
SET NOCOUNT ON
SELECT
g.name AS Group_Name, u.fullname, u.designation,
u.email, u.mobile
FROM
TBL_ADM_USER_GROUP g, TBL_ADM_USER u
WHERE
g.id = u.group_id
AND (g.deleted IS NULL OR g.deleted <> 1)
END
The result is like this:
Group_name fullname designation email mobile
Alex fffffffff Engineer [email protected] 3333333333
Jon hhhhhhhhh programmer [email protected] 020202028347
As you can see, the stored procedure does not have any parameters. How to read this output and return it as list using C#?
Code:
public List<string> GetData()
{
using (SqlConnection con = new SqlConnection(Global.Config.ConnStr))
{
string group;
DataTable dt = new DataTable();
List<string> details = new List<string>();
SqlCommand cmd = new SqlCommand("spp_adm_user_user_group_sel", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
details.Add(group);
}
}
return details;
}
Group_name fullname designation email mobile
then make a List<Class> and populate the List<Class> field remembering to Add as well as creating a new instance of that class that you will add to your List<T>Join
the values with,
look up string.Join() method.. or create an instance of a List<T> and while you are in the loop get at the datatable's ItemArray start using the debugger and you will see you are almost there..