2012-12-11 48 views
0

我創建了一個空的GridView的會話來填充一個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字段爲「用戶名」,此會話在登錄時填充。當我測試它,並輸入它的用戶名,但運行該程序時,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 [email protected] 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(); 
} 
+0

確保會議不爲空 – IBRA

+0

我做到了,它不是。 – BasharKH

+0

你如何定義和填充你的GridView? – Andrei

回答

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

即時消息ID使用以下格式=「08F4134」,當我運行它時,它們告訴我「f4134」附近的語法不正確......是造成這個? – BasharKH

+0

@BasharKH確保數據庫中的數據類型是nVarchar,並且對於網格ID =「string」Text ='<%#Bind(「string」)%>'..我現在無法檢查代碼語法,但你可以從谷歌獲得正確的語法,並嘗試搜索關於gridview templatefield – IBRA

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

+0

即使我這樣做,它仍然顯示爲空白,我的會話肯定有用戶名,因爲我在頁面頂部放置了一個標籤以顯示會話內容。 – BasharKH

+0

您是否在管理工作室獲得相同查詢的記錄? – Kaf

+0

是的,它給我很好的記錄。 – BasharKH