好吧,我已经尝试了所有人的建议,但问题仍然存在
让我简单地告诉你:
表单名称: AuzineForum.aspx
有 1 个 GridView,它使用 select * from QF 显示数据库中的所有字段
它工作正常,索引也工作,gridView 有按钮和 onClick 我打开新表格 AuzineForumAnswer.aspx ..OK
我想从 AuzineForum.aspx 中选择一条记录并显示在 AuzineForumAnswer.aspx 上,它发生在 http://stackoverflow.com 中。 (我们点击主题,然后打开新页面,其中包含我们之前点击的问题和答案)......好的
所以在 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 等)所以,当标签字段包含包含“#”字符的数据时,它会给出“对象引用未设置为对象实例”,如果 qtags 具有正常数据(特殊字符 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,都是字符字段,但问题不是在数据库中,问题出在字符 #
最佳答案
尝试更改您的 sql 语句以包含参数,看看是否可行。
您现在拥有的不仅难以维护和导致错误,而且很容易受到 SQL 注入(inject)攻击。
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"])))
;
关于c# - 特殊字符不显示在带有参数的 SQL 数据库的 gridview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473687/