Question How to obtain the first line of a multiline text field saved into database?

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
I have a multi-line textbox whose text is saved into database. Now I need to obtain only the first line of the text to be shown in a datagridview. How can I do it?
 
Are you looking to get the first line directly from the TextBox or from the database? If it's the former, the Lines property of the TextBox returns the lines of text as a String array, so you can simply get the first element. If it's the latter then you can use String.Split to split the text on line breaks to get a String array.

Note that, in a TextBox, line breaks consist of a carriage return and a line feed. Use Environment.NewLine to represent the line break when calling String.Split, which means that you MUST use an overload that supports Strings rather than Chars to split on.
 
If it's the latter then you can use String.Split to split the text on line breaks to get a String array.

It's the latter case.

If I'm not wrong then you meant to obtain the entire value from the database and then split it in .Net and show in the datagridview accordingly. Is that the way of it?
 
Ok I understand. But in my case the datagridview is bound to either a view or a result set returned by a select query. So the splitting method in .Net will be a little cumbersome. I wanted something to do the same thing on the database side, get the final result set and bind the grid with it.
 
Here's a solution I found. Assuming tblOrders as the table and Comments as the field in question, you can use the following syntax to do it:

VB.NET:
Select SUBSTRING(Comments,1,CHARINDEX(CHAR(13),Comments)) As Comments From tblOrders

Please post if you found any other way. :welcoming:
 

Latest posts

Back
Top