首页 \ 问答 \ asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)

asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)

我有一个html格式的文本字段,我需要选择一行数据,然后将其显示在aspx页面中作为html。

该文本包含html标签,符号像&; 等等......当然还有很多双引号。 例如....

<li>products</li>Check out our full line of these products, bla bla bla....<br><a href="http://www.thiswebsite3223.com/thispage.aspx" target="_blank"><font color="blue">View</font></a><td bgcolor="#000000" align="center"><b><font color="#FFFFFF">Diameter</font><li>30&deg; Helix<li></td>

...等等

我试图用在代码隐藏中检索它的方法,给我错误...

System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理其他信息:“=”附近的语法错误。

    public void GetHtml()
    {
        string gethtml = Request.QueryString["product"];
        string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string query = "SELECT htmlpull FROM ejn_html WHERE newseries LIKE = @gethtml";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.Add("@gethtml", SqlDbType.VarChar, 70).Value = gethtml;
        SqlConnection con = new SqlConnection(conString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        DataTable dt2 = new DataTable();
        sda.Fill(dt2);

        foreach (DataRow dtRow in dt2.Rows)
        {
            pulledhtml = dtRow[0].ToString();
        }

    } 

在包含... sda.Fill(dt2);

在我的aspx页面中,我尝试使用...调用变量...

<%= pulledhtml %>

当我尝试检索包含正常字母数字字符的任何其他字符串变量时,此工作正常。 这是一个非常大的数据库,我继承了,我不允许,也不想编辑这些字段中的html。

我如何检索这个HTML代码并显示它没有例外错误?


I have a field of text in html format, in a row of data that I need to select and then display it in a aspx page as html.

The text contains html tags, symbols like & ; etc... and of course lots of double quotes. For example....

<li>products</li>Check out our full line of these products, bla bla bla....<br><a href="http://www.thiswebsite3223.com/thispage.aspx" target="_blank"><font color="blue">View</font></a><td bgcolor="#000000" align="center"><b><font color="#FFFFFF">Diameter</font><li>30&deg; Helix<li></td>

...etc

The method that I am trying to use to retrieve it with in the codebehind, gives me the error...

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near '='.

    public void GetHtml()
    {
        string gethtml = Request.QueryString["product"];
        string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string query = "SELECT htmlpull FROM ejn_html WHERE newseries LIKE = @gethtml";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.Add("@gethtml", SqlDbType.VarChar, 70).Value = gethtml;
        SqlConnection con = new SqlConnection(conString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        DataTable dt2 = new DataTable();
        sda.Fill(dt2);

        foreach (DataRow dtRow in dt2.Rows)
        {
            pulledhtml = dtRow[0].ToString();
        }

    } 

The exception is triggered on the line that contains... sda.Fill(dt2);

In my aspx page I try to call the variable using...

<%= pulledhtml %>

This works fine when I try to retrieve any other string variables that contain normal alphanumeric characters. This is a very large database that I inherited and I am not allowed nor would I want to edit the html in those fields.

How can I retrieve this html code and display it without exception errors?


原文:https://stackoverflow.com/questions/45428258
更新时间:2019-07-13 09:05

最满意答案

你的sql文本不正确

SELECT htmlpull FROM ejn_html WHERE newseries LIKE = @gethtml

是无效的。 你不能一起使用LIKE和=。

将你的sql更改为

SELECT htmlpull FROM ejn_html WHERE newseries = @gethtml

准确匹配或

SELECT htmlpull FROM ejn_html WHERE newseries LIKE @gethtml

在那里用通配符定义参数值

cmd.Parameters.Add("@gethtml", SqlDbType.VarChar, 70).Value = "%" + gethtml + "%";

用于gethtml的值包含在列值中的匹配项。


Your sql text is incorrect

SELECT htmlpull FROM ejn_html WHERE newseries LIKE = @gethtml

is invalid. you can't use LIKE and = together.

Change you sql to either

SELECT htmlpull FROM ejn_html WHERE newseries = @gethtml

for an exact match or

SELECT htmlpull FROM ejn_html WHERE newseries LIKE @gethtml

where you define the parameter value with wildcards

cmd.Parameters.Add("@gethtml", SqlDbType.VarChar, 70).Value = "%" + gethtml + "%";

for a match where the value of gethtml is contained in the column value.

2017-08-01

相关问答

更多

使用asp.net mvc3,如何以不同的格式显示数据如何存储在数据库中?(Using asp.net mvc3, how do I display data in a different format to how it's stored in the database?)

由于这是数据库中的一列,我建议将这个奇数时间字段的逻辑抽象到它自己的类中。 class MyTimeField { public int Hours { get; set; } public int Minutes { get; set; } public MyTimeField(String timeString) { var stringParts = timeString.Split(":"); if(stringParts ...

ASP.NET MVC C#:将来自多个表/查询的数据引入视图(ASP.NET MVC C#: Bringing in data from multiple tables/queries into a view)

您正在寻找的概念被称为ViewModel 。 本质上,这是一个自定义类,它包含将在视图中使用的所有数据。 所以它负责合并来自不同表格的所有数据并将其作为属性公开。 如果您使用的是数据访问层,这通常与将几个实体组合在一起一样简单。 如果您使用原始SQL来执行此操作,那么您将在访问属性时执行查询。 然后你可以让视图继承ViewModel,如下所示: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" ...

ASP.Net发布超时(ASP.Net Post timeout)

我在一个网页上遇到同样的错误。 它是非常有线的,因为如果我使用代理服务器,错误就会发生,如果我从计算机直接拨打电话,我就有时间了,永远不要继续。 此问题出现在2011年12月之前的网站是http://www.auctionsniper.com/如果我使用移动版本,则同一网站可以正常工作。 经过与上述网站的许多检查后,我发现问题出在非常大的视图状态! 我如何找到它 :保存页面的html部分,并将其从HTML中移除并开始发布,直到我发现该帖子在我裁剪视图状态帖子时继续。 解决方案是禁用许多不需要的控件 ...

ASP.NET(OWIN)身份:如何从Web API控制器获取UserID?(ASP.NET (OWIN) Identity: How to get UserID from a Web API controller?)

您应该能够通过标识1.0 RTW包中的相同扩展方法在MVC控制器和Web api控制器上获取用户标识。 以下是身份包的扩展: namespace Microsoft.AspNet.Identity { public static class IdentityExtensions { public static string FindFirstValue(this ClaimsIdentity identity, string claimType); ...

ASP.NET MVC附件列表(来自二进制数据)(ASP.NET MVC List of attachments (from binary data))

您必须先将二进制数据保存为Image Format到您的目录项目中。 使用FileResult HTML: <div> @Html.ActionLink("Download", "Download", "Home"); </div> HomeController的: public FileResult Download() { String path = HostingEnvironment.ApplicationPhysicalPath + "Image\\C ...

ASP.NET DataBoundField DataFormatString,如何正确添加html?(ASP.NET DataBoundField DataFormatString, how can I add html correctly?)

尝试将字段的HtmlEncode属性设置为“false”。 它默认启用,以防止执行任何恶意客户端脚本代码。 Try setting the HtmlEncode property of the field to "false". It's enabled by default to prevent any malicious client-script code from being executed.

从Web Service(asmx)页面引用asp.net页面上的字段(Referencing a field on an asp.net page from a Web Service (asmx) page)

不记得你是否可以做到这一点,但尝试添加另一个参数到你的方法。 public string[] ISGetCompletionList(string prefixText, string payerDate) 然后在ajax调用 data: "{ 'prefixText': '" + request.term + "', 'payerDate': '" + formDate + "' }", etc. 要么, 在ajax调用中将信息添加到prefixText中,类似于webmethod组合数据的 ...

相关文章

更多

最新问答

更多
  • Unity着色器错误;(Unity shader error; presumably in if statement)
  • 如何在Liferay portlet中设置Cookie?(How to set a Cookie in Liferay portlet?)
  • C#只读访问List <>或其他集合,或ToArray()魔术(C# readonly access to List<> or other collection, or ToArray() magic)
  • 如何使用Google Web工具包创建登录应用程序?(How to create a login application using Google web tool kit? [closed])
  • 如何使我的函数返回结果作为全局变量?(How to make my function return results as a global variable?)
  • 为什么我的网站需要“启用32位应用程序”?(Why does my website need “Enable 32-bit applications”?)
  • 红宝石数组具有相同的值(ruby array of hash with same value)
  • Android(在Scala中):StackOverflowError取决于何时启动线程?(Android (in Scala): StackOverflowError depends on when to start a thread?)
  • 适用于iOS应用的通用链接(Universal Links for iOS apps)
  • 创建并打开文件linux编程(create and open file linux programming)
  • Datepicker,第2个日期是从第1个日期开始的X天(Datepicker, 2nd date is X days from 1st date)
  • 背景大小过渡不起作用(Transition on background-size doesn't work)
  • 在React / Redux中的On Click函数中传递Prop(Passing a Prop in an On Click Function in React/Redux)
  • 关键字在代码中做了什么,是否有没有此关键字的替代方法?(what does the keyword this does in the code and are there any alternate methods without this keyword? [duplicate])
  • 反向设计FoxPro / dBsae数据库以创建EER模型(Reverse Engineer a FoxPro/dBsae database to create the EER model)
  • 在R中的变量中组合具有相同值的行(Combine rows that have same value in a variable in R [duplicate])
  • SQL Server:非空唯一主键(SQL Server: Non-null unique vs. Primary Key)
  • 将扩展ASCII字符代码转换为ISO-8859-1(Convert extended ASCII character codes to ISO-8859-1)
  • 如何在.NET中读取m4a文件中的标签?(How to read tags out of m4a files in .NET?)
  • 呼叫链接的性能提升?(Performance gain on call chaining?)
  • 风景名胜区规划设计文本里面都包含哪些内容?推荐一家旅游规划设计公司?谢谢!
  • 如何在String中将字符串转换为枚举?(How do I convert a string to enum in TypeScript?)
  • SQLite SELECT出现异常如何解决它?(SQLite SELECT gives exception How to fix it?)
  • NSLog不会从ViewController类输出,而是来自AppDelegate类的WILL。(NSLog will NOT output from the ViewController class, but WILL from the AppDelegate class. iOS:Objective C)
  • 更改UIPicker突出显示的标签宽度(change UIPicker highlighted tab width)
  • 空心倒五角形(Hollow inverted pentagon)
  • 如何设置仅使用OpenID的Plone站点(How to setup Plone sites working only with OpenID)
  • Paperclip缺少Amazon S3的协议(https)(Paperclip is missing the Protocol (https) with Amazon S3)
  • 从宏中部署netbeans中的ANT文件(Deploy ANT file in netbeans from macro)
  • XMLReader是未知的(XMLReader is unknown)