2013-10-20 31 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,它没有提供。 –