2013-10-20 29 views
2

嗯,我已經嘗試了所有你的人建議,但問題仍然相同特殊字符不顯示在GridView控件從SQL數據庫參數

讓我告訴你在簡短:

表姓名:AuzineForum。 ASPX

擁有1個GridView控件使用SELECT * FROM QF

其工作良好,指數也在努力,GridView控件有按鈕,顯示數據庫中的所有領域和onClick我打開新的表單AuzineForumAnswer.aspx ..OK

我想從AuzineForum.aspx中選擇一個記錄,並顯示在AuzineForumAnswer.aspx以及它發生在http://stackoverflow.com這裏(我們點擊線程然後新頁面打開其中有問題,並在其上點擊我們前面的答案)... OK

等AuzineForum.aspx的按鈕的代碼是

Button lb = (Button)sender; 
      GridViewRow row = (GridViewRow)lb.NamingContainer; 
      if (row != null) 
      { 
       int index = row.RowIndex; //gets the row index selected 


       Label AID1 = (Label)ForumQuesView.Rows[index].FindControl("AID1"); 
       Label AID2 = (Label)ForumQuesView.Rows[index].FindControl("AID2"); 
       Label AID3 = (Label)ForumQuesView.Rows[index].FindControl("AID3"); 
       HyperLink Question = (HyperLink)ForumQuesView.Rows[index].FindControl("Question"); 
       Label Questiontags = (Label)ForumQuesView.Rows[index].FindControl("Questiontags"); 
       Label Askedby = (Label)ForumQuesView.Rows[index].FindControl("Askedby"); 


       Response.Redirect(String.Format("AuzineForumAnswer.aspx?Question=" + Question.Text + "&Questiontags=" + Questiontags.Text + "&Askedby=" + Askedby.Text + "&AID1=" + AID1.Text + "&AID2=" + AID2.Text + "&AID3=" + AID3.Text, Server.UrlEncode(Question.Text), Server.UrlEncode(Questiontags.Text), Server.UrlEncode(Askedby.Text), Server.UrlEncode(AID1.Text), Server.UrlEncode(AID2.Text), Server.UrlEncode(AID3.Text))); 

我有經過了太多的參數監守精度......

現在,當我運行它,然後單擊按鈕,它的開放AuzineForumAnswer.AuzineForumAnswerand顯示,記錄非常好,但出現問題時,qtags領域有「#」型喜歡這裏的標籤數據(C#,GridView的,等等),因此,當標記字段具有數據INCLUDIN「#」 chracter然後它給出「對象refrence不設置到對象的實例」,並且如果有qtags像正常數據(specialcharacter gridview的SQL C)然後將其打開AuzineForumAnswer。ASPX並沒有錯誤的數據顯示

AuzineForumAnswer.aspx後面的代碼如下

protected void GetAllData() 
     { 
      string connection = System.Configuration.ConfigurationManager.ConnectionStrings["AuzineConnection"].ConnectionString; 


      using (SqlConnection sqlconn = new SqlConnection(connection)) 
      { 
       using (SqlCommand sqlcomm = sqlconn.CreateCommand()) 
       { 
        sqlcomm.CommandText = "Select * From QF where Question='" + Server.UrlDecode(Request.QueryString["Question"].ToString()) + "' And qtags='" + Server.UrlDecode(Request.QueryString["Questiontags"].ToString()) + "' And UserFullName='" + Server.UrlDecode(Request.QueryString["Askedby"].ToString()) + "' And AID1='" + Server.UrlDecode(Request.QueryString["AID1"].ToString()) + "' And AID2='" + Server.UrlDecode(Request.QueryString["AID2"].ToString()) + "' And AID3='" + Server.UrlDecode(Request.QueryString["AID3"].ToString()) + "'"; 

        SqlDataAdapter sda = new SqlDataAdapter(sqlcomm); 
        DataTable dt = new DataTable(); 
        sda.Fill(dt); 

        try 
        { 
         sqlconn.Open(); 

         ForumQuesView.DataSource = dt; 
         ForumQuesView.DataBind(); 

         ForumQuesView.AllowPaging = true; 

        } 
        catch (Exception ex) 
        { 
         Status.Text = ex.Message.ToString(); 
        } 
       } 
      } 
     } 

現在我也搞不懂什麼問題在這裏,因爲只有qtags和的問題有兩個網絡連接場中,用戶可以儲存數據,因爲他們想要的,問題是文字和qtags而且都煤焦領域,但問題並不在數據庫中的問題是在這裏以字符#

+0

您是否嘗試過調試和看到的是創造了什麼SQL語句?發佈該sql語句。 – unlimit

+0

調試,獲取查詢並在sql server中運行它。我什麼也沒有出錯,那麼你的任何對象的初始化必須有問題 - 「對象引用沒有設置爲對象的實例」 – polin

+0

親愛的波林,我也同意你的意見,但我所說的是......我在qtags列中有很多行,通過單擊任何行都沒有出錯,但具有「#」字符的行會給出錯誤,並且當我直接轉到SQL Server以更改行內部時,它會給出錯誤「Binary data將被截斷「現在這個錯誤來時,限制超過,但一切都很好,我不明白我在哪裏犯的錯誤 –

回答

0

我所知查詢罰款,即使你在條件中使用#。

我一會兒懷疑,那麼我想這個查詢

Select * From QF where Question='question 1' 
And qtags='tag #1'; 

這些查詢仍然運行平穩,並返回該記錄。

+0

不工作親愛的....請幫我解決 –

1

試着改變你的SQL語句,包括參數,看看是否能工程。

你現在擁有的不僅是難以維持和導致錯誤,但它容易出現SQL注入攻擊很容易。

sqlcomm.CommandText = "Select * From QF where [email protected] And [email protected] And [email protected] And [email protected] And [email protected] And [email protected]"; 

sqlcomm.Parameters.Add(new SqlParameter("@Question", Server.UrlDecode(Request.QueryString["Question"]))); 
sqlcomm.Parameters.Add(new SqlParameter("@Qtags", Server.UrlDecode(Request.QueryString["Questiontags"]))); 
sqlcomm.Parameters.Add(new SqlParameter("@UserName", Server.UrlDecode(Request.QueryString["Askedby"]))); 
sqlcomm.Parameters.Add(new SqlParameter("@AID1", Server.UrlDecode(Request.QueryString["AID1"]))); 
sqlcomm.Parameters.Add(new SqlParameter("@AID2", Server.UrlDecode(Request.QueryString["AID2"]))); 
sqlcomm.Parameters.Add(new SqlParameter("@AID3", Server.UrlDecode(Request.QueryString["AID3"]))) 

;

+0

參數化查詢'(@Question nvarchar(300),@ Qtags nvarchar(14),@ UserName nvarchar('期望參數@UserName,它沒有提供。 –