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