Save Multiline TextBox text in Database and display in ASP.Net FormView using C# and VB.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

I want to save multiline text in sql database table with line break. The column datatype is nvarchar(Max). Currently when I am saving the data to database on button click, it won't save with the linkbreak. How should I proceed? Thanks!

Code :

<asp:FormView ID="FormView1" runat="server" AutoGenerateRows="False" DataKeyNames="projdefenseid"
    DataSourceID="SqlDataSource1" Width="100%">
    <InsertItemTemplate>
        <table id="tblPartI" width="780px">
            Abstract:<asp:TextBox ID="txtAbstract" runat="server" Text='<%# Bind("Abstract")%>'
                Width="655px" TextMode="MultiLine" Height="300px" />
            <br />
            <p style="text-align: center;">
                <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insert" />
                &nbsp;<asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False"
                    CommandName="Cancel" Text="Cancel" />
            </p>
    </InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    InsertCommand="INSERT INTO [table1] ([AccessCode]) VALUES (@AccessCode)" SelectCommand="SELECT * FROM [table1] WHERE [AccessCode] = @AccessCode">
    <SelectParameters>
        <asp:Parameter Name="AccessCode" Type="String" />
    </SelectParameters>
    <InsertParameters>
        <asp:Parameter Name="AccessCode" Type="String" />
    </InsertParameters>
</asp:SqlDataSource>
You are viewing reply posted by: pandeyism 4 months ago.
Posted 4 months ago

Hi kelsen1989,

Refer below sample.

HTML

<asp:FormView ID="CustomerFormView" DataSourceID="CustomerDetailsSqlDataSource" DataKeyNames="CustomerId"
    GridLines="Both" runat="server">
    <HeaderStyle BackColor="Navy" ForeColor="White" />
    <RowStyle BackColor="White" />
    <EditRowStyle BackColor="LightCyan" />
    <ItemTemplate>
        CustomerId:
        <asp:Label ID="CustomerIdLabel" runat="server" Text='<%# Eval("CustomerId") %>' />
        <br />
        Name:
        <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>' />
        <br />
        Country:
        <asp:Label ID="CountryLabel" runat="server" Text='<%# Bind("Country") %>' />
        <br />
        <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
            Text="Edit" />
        &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
            Text="Delete" />
        &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
            Text="New" />
    </ItemTemplate>
    <EditItemTemplate>
        CustomerId:
        <asp:Label ID="CustomerIdLabel1" runat="server" Text='<%# Eval("CustomerId") %>' />
        <br />
        Name:
        <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' TextMode="MultiLine" />
        <br />
        Country:
        <asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>' TextMode="MultiLine" />
        <br />
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
            Text="Update" />
        &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
            CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
    <InsertItemTemplate>
        Name:
        <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' TextMode="MultiLine" />
        <br />
        Country:
        <asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>' TextMode="MultiLine" />
        <br />
        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
            Text="Insert" />
        &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
            CommandName="Cancel" Text="Cancel" />
    </InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="CustomerDetailsSqlDataSource" SelectCommand="SELECT [CustomerId], [Name], [Country] FROM [Customers]"
    InsertCommand="INSERT INTO [Customers] ([Name], [Country]) VALUES (@Name, @Country)"
    UpdateCommand="UPDATE [Customers] SET [Name] = @Name, [Country] = @Country WHERE [CustomerId] = @CustomerId"
    DeleteCommand="DELETE FROM [Customers] WHERE [CustomerId] = @CustomerId" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
    OnInserted="CustomerDetailsSqlDataSource_OnInserted" runat="server" OnUpdating="CustomerDetailsSqlDataSource_OnUpdating">
    <DeleteParameters>
        <asp:Parameter Name="CustomerId" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="Country" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="Country" Type="String" />
        <asp:Parameter Name="CustomerId" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

Code

C#

protected void CustomerDetailsSqlDataSource_OnInserted(object sender, SqlDataSourceStatusEventArgs e)
{
    string name = (CustomerFormView.FindControl("txtName") as TextBox).Text.Replace(Environment.NewLine, "<br />");
    string country = (CustomerFormView.FindControl("txtCountry") as TextBox).Text.Replace(Environment.NewLine, "<br />");
    e.Command.Parameters["@Name"].Value = name;
    e.Command.Parameters["@Country"].Value = country;
}

protected void CustomerDetailsSqlDataSource_OnUpdating(object sender, SqlDataSourceCommandEventArgs e)
{
    string name = (CustomerFormView.FindControl("txtName") as TextBox).Text.Replace(Environment.NewLine, "<br />");
    string country = (CustomerFormView.FindControl("txtCountry") as TextBox).Text.Replace(Environment.NewLine, "<br />");
    e.Command.Parameters["@Name"].Value = name;
    e.Command.Parameters["@Country"].Value = country;
}

VB.Net

Protected Sub CustomerDetailsSqlDataSource_OnInserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
    Dim name As String = (TryCast(CustomerFormView.FindControl("txtName"), TextBox)).Text.Replace(Environment.NewLine, "<br />")
    Dim country As String = (TryCast(CustomerFormView.FindControl("txtCountry"), TextBox)).Text.Replace(Environment.NewLine, "<br />")
    e.Command.Parameters("@Name").Value = name
    e.Command.Parameters("@Country").Value = country
End Sub

Protected Sub CustomerDetailsSqlDataSource_OnUpdating(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)
    Dim name As String = (TryCast(CustomerFormView.FindControl("txtName"), TextBox)).Text.Replace(Environment.NewLine, "<br />")
    Dim country As String = (TryCast(CustomerFormView.FindControl("txtCountry"), TextBox)).Text.Replace(Environment.NewLine, "<br />")
    e.Command.Parameters("@Name").Value = name
    e.Command.Parameters("@Country").Value = country
End Sub

Screenshot