通过登录用户Ado.Net填充网格

时间:2014-06-22 15:27:33

标签: c# asp.net gridview

我需要根据当前登录的用户使用记录填充gridview。我的存储过程要求一个参数,即用户的ID。我希望该参数是当前登录的用户,但我无法弄清楚如何实现这一目标。

我的存储过程来获取记录:

@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

我试图用我的C#代码填充gridview。

           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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个;

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