sql-servervb.nettruedbgrid

Remove the crlf in the SQL-database?


I'm doing an exercise with the Northwind database from Microsoft. I try to fill the values from a table into a Truedbgrid. That still worked, but afterwards I noticed that 3 columns have a crlf. I tried to solve it with a REPLACE, but it didn't work.

I changed the query(in dataset) from

SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts

to

SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, REPLACE(Address,CHAR(13)+CHAR(10),' '), City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts

An another way I don't known. I search on Internet, if theres a way to say to the truedbgrid, remove all crlf. But I doesn't found something. In the table is that crlf invisible. I can't remove that. Are there any ideas?

My VB.NET Classcode:

Private Sub tdbgContactsParamentrieren()
    Try
        With tdbgContacts
            .AllowAddNew = False
            .AllowDelete = False
            .AllowColMove = False
            .AllowFilter = True
            .AllowSort = True
            .MultiSelect = MultiSelectEnum.None
            .AllowUpdate = True
            .AllowUpdateOnBlur = True
            .FilterBar = True

            .RowHeight = 22

            .TabAction = TabActionEnum.ColumnNavigation

            .MarqueeStyle = MarqueeEnum.HighlightCell

            .Columns(0).Caption = "ID"
            .Columns(1).Caption = "Type"
            .Columns(2).Caption = "Company"
            .Columns(3).Caption = "Name"
            .Columns(4).Caption = "Title"
            .Columns(5).Caption = "Address"
            .Columns(6).Caption = "City"
            .Columns(7).Caption = "Region"
            .Columns(8).Caption = "Postal Code"
            .Columns(9).Caption = "Country"
            .Columns(10).Caption = "Phone"
            .Columns(11).Caption = "Extension"
            .Columns(12).Caption = "Fax"
            .Columns(13).Caption = "Home Page"
            .Columns(14).Caption = "Photo Path"

            With .Splits(0)
                .AlternatingRowStyle = True
                .EvenRowStyle.BackColor = Color.LightYellow
                .EvenRowStyle.VerticalAlignment = AlignVertEnum.Center
                .OddRowStyle.VerticalAlignment = AlignVertEnum.Center

                .ColumnCaptionHeight = 30

                .DisplayColumns(0).Width = 42 'ID
                .DisplayColumns(1).Width = 52 'Type
                .DisplayColumns(2).Width = 218 'Company
                .DisplayColumns(3).Width = 168 'Name
                .DisplayColumns(4).Width = 168 'Title
                .DisplayColumns(5).Width = 150 'Country
                .DisplayColumns(6).Width = 257 'Address
                .DisplayColumns(8).Width = 56 'PostalCode
                .DisplayColumns(9).Width = 168 'City
                .DisplayColumns(10).Width = 90 'Phone
                .DisplayColumns(11).Width = 56 'Extension
                .DisplayColumns(12).Width = 93 'Fax
                .DisplayColumns(13).Width = 200 'HomePage

                For index As Integer = 0 To .DisplayColumns.Count - 1
                    .DisplayColumns(index).HeadingStyle.HorizontalAlignment = AlignHorzEnum.Center
                    .DisplayColumns(index).Locked = True
                Next
            End With
        End With
    Catch ex As Exception
    End Try
End Sub

EDIT: enter image description here

Thats, before I make a change.

When I do it so:

SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, REPLACE(Address,CHAR(13)+CHAR(10),' '), City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts

or

SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, REPLACE(REPLACE(Address, CHAR(13), ' '), CHAR(10), ' '), City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts

or

SELECT ContactID, ContactType, CompanyName, ContactName, ContactTitle, REPLACE(REPLACE(cast(Address as nvarchar(max)), CHAR(13), ' '), CHAR(10), ' '), City, Region, PostalCode, Country, Phone, Extension, Fax, HomePage, PhotoPath, Photo FROM dbo.Contacts

The result is: enter image description here

EDIT 2:

I need the table "Contacts".

This is my Database structure: enter image description here


Solution

  • You haven't shown us exactly how you're binding the grid to your data source, but based on the observed behaviour it seems likely that it relies in some way on the names of the fields returned by the SELECT query.

    Therefore, as mentioned in the comments, try giving the output of your REPLACE function an alias so that it has a recognisable name:

    REPLACE(REPLACE(cast(Address as nvarchar(max)), CHAR(13), ' '), CHAR(10), ' ') As Address,
    

    Right now, since that column has no alias, I would expect that it isn't binding to the "Address" column in the grid.


    P.S. a as a separate issue, this:

    Try
    ...
    Catch ex As Exception
    End Try
    

    is an anti-pattern. It's fine to catch exceptions, but if you do that you need to log the exception details somewhere (e.g. in the Windows Event Log, or to a file). Otherwise if something does go wrong in your code you'll have no idea what it was and therefore no way of trying to fix it. You are throwing away important information which is intended help you as a developer to solve problems.