0

我创建了一个空的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

之后,我选择参数的来源为“会话”,其会话字段为“用户名”,此会话在登录时填充。当我对其进行测试并输入用户名时,它可以工作,但是在运行程序时,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();
}
4

2 回答 2

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

于 2012-12-11T08:54:19.373 回答
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();
     }
}
于 2012-12-11T08:22:49.987 回答