Insert (Save) accents text using C# and VB.Net in ASP.Net

ilanocf
 
on Oct 27, 2021 10:43 PM
547 Views

I'm using Visual Studio 2010 and I'm using the AjaxControlToolKit Editor.

I already use it in several forms and I haven't had any problems until today. I created a form in which the Editor is inside UpdatePanel and when I save the text in the database, all the characters that have accents become a question mark. So, in texts that have ç, ã, õ, á, ó, ... all are replaced by question marks (card, concession, extraction, ...).

In other forms where the Editor is not inside an UpdatePanel this does not happen.

In Web.config there is already the line below.

<globalization culture="en-US" uiCulture="en-US" requestEncoding="iso-8859-1" responseEncoding="iso-8859-1" fileEncoding="iso-8859-1"/>

In the scriptmanager that is on MasterPage, the EneblaScriptGlobalization property is True.

What more can I do to resolve this issue?

Grateful,

ilano

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 28, 2021 06:22 AM

Hi Ilanocf,

Please refer below Sample.

HTML

<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
    <ContentTemplate>
        <asp:TextBox ID="Editor1" runat="server" Width="300" Height="200" />
        <br />
        <asp:Button Text="Save" runat="server" OnClick="OnSave" />
    </ContentTemplate>
</asp:UpdatePanel>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Code

C#

public bool RemoveLinhasVazias(string pTexto)
{
    string[] pDelimiters = new string[] { "\n", "\r", "\n\r" };
    string[] pLines = pTexto.Split(pDelimiters, StringSplitOptions.RemoveEmptyEntries);
    string pResult = string.Join(Environment.NewLine, pLines);
    pResult = StripHTML(pResult);
    if (string.IsNullOrEmpty(pResult.Trim()))
    {
        return true;
    }
    else
    {
        return false;
    }
}
public static string StripHTML(string input)
{
    return System.Text.RegularExpressions.Regex.Replace(input, "<.*?>", String.Empty);
}
protected void OnSave(object sender, EventArgs e)
{
    string pTexto;
    pTexto = Editor1.Text.Trim();
    if (RemoveLinhasVazias(pTexto))
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Empty');", true);
    }
    else if (!RemoveLinhasVazias(pTexto))
    {
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO HTMLContent (Content) VALUES (@content)", con))
            {
                cmd.Parameters.Add("@content", SqlDbType.NVarChar).Value = Editor1.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}

VB.Net

Public Function RemoveLinhasVazias(ByVal pTexto As String) As Boolean
    Dim pDelimiters As String() = New String() {vbLf, vbCr, vbLf & vbCr}
    Dim pLines As String() = pTexto.Split(pDelimiters, StringSplitOptions.RemoveEmptyEntries)
    Dim pResult As String = String.Join(Environment.NewLine, pLines)
    pResult = StripHTML(pResult)
    If String.IsNullOrEmpty(pResult.Trim()) Then
        Return True
    Else
        Return False
    End If
End Function

Public Shared Function StripHTML(ByVal input As String) As String
    Return System.Text.RegularExpressions.Regex.Replace(input, "<.*?>", String.Empty)
End Function

Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
    Dim pTexto As String
    pTexto = Editor1.Text.Trim()
    If RemoveLinhasVazias(pTexto) Then
        ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "", "alert('Empty');", True)
    ElseIf Not RemoveLinhasVazias(pTexto) Then
        Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(conString)
            Using cmd As SqlCommand = New SqlCommand("INSERT INTO HTMLContent (Content) VALUES (@content)", con)
                cmd.Parameters.Add("@content", SqlDbType.NVarChar).Value = Editor1.Text
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    End If
End Sub

Screenshot