簡體   English   中英

帶參數的SQL數據庫的gridview中不顯示特殊字符

[英]Special Character does not displays in gridview from SQL database with Parameter

好吧,我嘗試了所有建議的人,但是問題仍然存在

讓我簡單地告訴你:

表姓名:AuzineForum.aspx

具有1個GridView,它使用QF中的select *顯示數據庫中的所有字段

它的工作良好,索引也正常工作,gridView具有按鈕和onClick,我打開新的表格``AuzineForumAnswer.aspx..OK

我想從AuzineForum.aspx中選擇一條記錄並顯示在AuzineForumAnswer.aspx上,以及它發生在http://stackoverflow.com上的情況 (我們單擊線程,然后打開新頁面,其中包含關於該問題的答案我們點擊了上一個)...確定

所以在AuzineForum.aspx的Button上,代碼是

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等)的數據類型為“#”時就會發生問題當標簽字段包含數據``#''時,則給出``對象引用未設置為對象實例'',並且如果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和Question是兩個字段,用戶可以根據需要在其中存儲數據,問題是text和qtags都是char字段,但是問題不在數據庫中問題在這里與字符#

嘗試更改您的sql語句以包含參數,然后查看是否可行。

您現在擁有的不僅難以維護並且會導致錯誤,而且還很容易遭受SQL注入攻擊。

sqlcomm.CommandText = "Select * From QF where Question=@Question And qtags=@Qtags And UserFullName=@UserName And AID1=@AID1 And AID2=@AID2 And AID3=@AID3";

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"])))

;

據我了解,即使您在條件中使用#,查詢也很好。

我懷疑了一會兒,然后我嘗試了這個查詢

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

這些查詢仍然可以順利運行並返回記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM