2017-07-30 64 views
0

我目前正在使用gridview將論壇項目附加到數據庫。使用SelectedIndexChanged,它將重定向到另一頁面以顯示標籤中的詳細信息。但是,我無法顯示&沒有任何特定的錯誤。無法從數據庫檢索數據並將其存儲到值

這是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class FAQViewPost : System.Web.UI.Page 
{ 
    string _connStr = ConfigurationManager.ConnectionStrings["WingsDrinksDbContext"].ConnectionString; 
    FAQ faq = null; 

    string CustQuestionCategory = null; 
    string CustQuestion = null; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     string FAQID = Request.QueryString["FAQID"].ToString(); 
     Load(FAQID); 
    } 

    protected void Load(string FAQID) 
    { 
     DataTable dt = new DataTable(); 
     string queryStr = "SELECT * FROM [FAQ] WHERE FAQID = @FAQID "; 

     SqlConnection conn = new SqlConnection(_connStr); 
     SqlCommand cmd = new SqlCommand(); 

     string[] arr = { queryStr }; 
     string allQueries = string.Join(";", arr); 

     cmd.CommandText = allQueries; 
     cmd.Connection = conn; 
     cmd.Parameters.AddWithValue("@FAQID", FAQID); 

     SqlDataAdapter sqlDa = new SqlDataAdapter(cmd); 

     sqlDa.Fill(dt); 

     if (dt.Rows.Count > 0) 
     { 
      lbl_category.Text = dt.Rows[0]["CustQuestionCategory"].ToString(); 
      lbl_question.Text = dt.Rows[0]["CustQuestion"].ToString(); 
     } 

     conn.Close(); 
     conn.Dispose(); 
    } 
} 
+1

您是否調試過代碼?您在FAQID中獲得價值嗎?你能分享selectedIndexChanged事件的代碼嗎?是網格視圖的事件嗎?你在數據表中獲取行嗎? –

回答

0

我不知道你爲什麼把SQL到一個數組。你只有一個聲明。嘗試使用只是:

DataTable dt = new DataTable(); 
string queryStr = "SELECT * FROM [FAQ] WHERE FAQID = @FAQID "; 
SqlConnection conn = new SqlConnection(_connStr); 
SqlCommand cmd = new SqlCommand(); 
cmd.CommandText = queryStr; 
cmd.Connection = conn; 
cmd.Parameters.AddWithValue("@FAQID", FAQID); 
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd); 

sqlDa.Fill(dt); 

我認爲你已經檢查了常見問題解答表有一個你正在發送的ID記錄?如果你使用數字ID,生活會更安全一些。

相關問題