如何基于会话填充网格视图

时间:2012-12-11 07:33:29

标签: c# asp.net

我创建了一个空的gridview,我创建了一个sqldatasource,select语句就是这个

SELECT S.[ID], S.[Survey_Name], S.[Start_Date], S.[End_Date], C.[Category_Name], S.[Username], S.[Audience] FROM [Survey] S
Inner Join
Category C On S.Category_ID = C.ID Where Username = @Username

之后我选择参数的源为“Session”,其Session字段为“Username”,此会话在登录时填充。当我测试它,并输入一个可用的用户名,但在运行程序时,gridview始终是空白的。

编辑:我的aspx.page代码。

<asp:SqlDataSource 
    ID="SqlDataSource1"
    Runat="server" 
    SelectCommand="SELECT S.[ID], S.[Survey_Name], S.[Start_Date], 
                 S.[End_Date], C.[Category_Name], S.[Username], S.[Audience] 
                 FROM [Survey] S Inner Join Category C 
                      On S.Category_ID = C.ID Where Username = @Username"
    SelectCommandType="Text"
    ConnectionString="<%$ ConnectionStrings:SurveySystemConnectionString %>">
    <SelectParameters>
       <asp:SessionParameter Name="Username" DbType="String" 
            DefaultValue=""  SessionField="username"/>

    </SelectParameters>
</asp:SqlDataSource>

编辑:我的登录按钮

protected void LoginButton_Click(object sender, EventArgs e)
        {
            lblerror.Visible = false;

            SqlConnection conn = DatabaseConnection.GetSurveySystemConnection();             
            string queryString = "SELECT * FROM [Users] WHERE Username=@username AND Password= @password";
            SqlCommand command = new SqlCommand(queryString, conn);
            command.Parameters.AddWithValue("@username", UserNameTextBox.Text);
            command.Parameters.AddWithValue("@password", PasswordTextBox.Text);

            SqlDataReader reader = null;
            reader = command.ExecuteReader();

            if (reader.Read())
            {
                Session["Username"] = UserNameTextBox.Text;
                Session["Name"] = (string)reader["name"];
                Session["UserType"] = (string)reader["UserType"];
                Response.Redirect("Home.aspx");
            }
Connection.close();
}

2 个答案:

答案 0 :(得分:0)

protected void Page_Load(object sender, EventArgs e) {
     if (Session["username"] != null) {
          string strSQLconnection = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
          SqlConnection conn = new SqlConnection(strSQLconnection);
          conn.Open();
          SqlCommand command = new SqlCommand("select * from table1 where username =" + (string)(Session["username"], sqlConnection);
          SqlDataReader reader = sqlCommand.ExecuteReader();
          GridView1.DataSource = reader;
          GridView1.DataBind();
     }
}

答案 1 :(得分:0)

您的SqlDataSource应如下所示。请检查SessionField="username"是否正确。

<asp:SqlDataSource 
    ID="SqlDataSource1"
    Runat="server" 
    SelectCommand="SELECT S.[ID], S.[Survey_Name], S.[Start_Date], 
                 S.[End_Date], C.[Category_Name], S.[Username], S.[Audience] 
                 FROM [Survey] S Inner Join Category C 
                      On S.Category_ID = C.ID Where Username = @Username"
    SelectCommandType="Text"
    ConnectionString="<%$ ConnectionStrings:YourConnStringName %>">
    <SelectParameters>
       <asp:SessionParameter Name="Username" DbType="String" 
            DefaultValue=""  SessionField="username"/>

    </SelectParameters>
</asp:SqlDataSource>

注意:确保SessionParameterSelectParameter