Cinedine
Cinedine

Reputation: 23

Ado.Net Fill Grid By Logged in User

I need to fill a gridview with records according to the currently logged in user. My stored procedure asks for one parameter, the ID of the user. I want that parameter to be the currently logged in user but i cant figure out how to accomplish this.

My stored Procedure to grab the records:

@cnt_ID int 

AS
BEGIN
SELECT     TOP (100) PERCENT dbo.vtCursusPlanning.cur_CursusID AS CursusID, dbo.vtCursusPlanning.cur_Omschrijving AS Omschrijving, CONVERT(varchar, 
                      dbo.vtData.dat_Datum, 100) AS Datum, CONVERT(varchar, dbo.vtData.dat_Start, 100) AS DStart, CONVERT(varchar, dbo.vtData.dat_Stop, 100) AS DStop, 
                      dbo.vtContactPersonen.cnt_Initialen AS Username
FROM         dbo.vtData INNER JOIN
                      dbo.vtCursusPlanning ON dbo.vtData.cur_FK = dbo.vtCursusPlanning.cur_CursusID INNER JOIN
                      dbo.vtContactPersonen ON dbo.vtCursusPlanning.cnt_FK = dbo.vtContactPersonen.cnt_ID INNER JOIN
                      dbo.vtCursusCursisten ON dbo.vtData.cur_FK = dbo.vtCursusCursisten.cst_fk
WHERE     (dbo.vtContactPersonen.cnt_ID = @cnt_ID) AND (NOT (dbo.vtCursusCursisten.cst_fk IS NULL)) AND (NOT (dbo.vtCursusPlanning.cur_Project IS NULL))
ORDER BY DStart
END

And my attempt to fill the gridview with my C# code.

           protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        GridView1.DataKeyNames = new string[] { "@cnt_ID" };
        string connectionString = ConfigurationManager.ConnectionStrings["KRIS-Planning"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("spOffice2010Evaluaties", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Add("@cnt_ID", SqlDbType.Int);
        comm.Parameters["@cnt_ID"].Value = UserID;

        try
        {
            conn.Open();
            reader = comm.ExecuteReader();
            GridView1.DataSource = reader;
            GridView1.DataBind();
            reader.Close();
        }
        catch (Exception ex)
        {
            dbErrorLabel.Text = Convert.ToString(ex);
        }

        finally
        {
            conn.Close();
        }
    }
}

Upvotes: 1

Views: 94

Answers (1)

Ali
Ali

Reputation: 2592

Try this one instead;

 string UserId = HttpContext.Current.User.Identity.Name;

Upvotes: 1

Related Questions