Handling new line and carriage return with Textbox


Introduction

Handling newline and carriage return with Textbox while working with Databases.

The Scenario

If any data contain new line or carriage return or both, while displaying on input box, the data after these characters will not come. Though we can use TextArea for such type of data, sometime such situation comes in an unavoidable way.

In some case we need to refer to some external database and populate data depending on some predefined criteria for our further processing and submit them to another database. In such case, if we are using text box for the fields, we need to handle the newline and carraiage return characters in different way.

The GUI:

Searching and Populating the Data:

In some case we need to refer to remote database (external) and populate the data from that database depending on some predefined criteria for our further processing.

//Inside the loop of reading the records from DataReader

.

.

 

strEmpID   = . . . ; //get the Emp ID

strName    = . . . ; //get the Name

strAddress = . . . ; //get the Address

 

//Replacing the \n and \r with respective value to display

strAddress = strAddress.Replace("\n","\\\\n");

strAddress = strAddress.Replace("\r","\\\\r");

//strAddress can be use now for displaying

 

//Making the complete data for passing to the js Function for populating

//strData will used for populating the data in the input box

strData    = strEmpID + "|" + strName + "|" + strAddress ;

.

.

//Now build up the java Script

strJScript = strJScript  + "<input type=\"radio\" name=\"EmpID\" value=\"" + strEmpID + "\" onClick="return setValues('" + strData + "');"

 

// setValues(strAdd) : This function will populates the data in the TextBox

.
.

Search then populated by clicking the radio button:

Submitting the data:

Before submitting the data to the dataset, we need to revert back our changes. We will replace '\\n' by (char)10 and '\\r' by (char)13.

.  

strAddress = ReplaceForNewLineChar(strAddress);
strAddress = ReplaceForCarriageReturnChar(strAddress); 
.  
.

The replacement Algorithm:

public string ReplaceForNewLineChar(string strInput)

{

            int tmpCounter = 0;

            string strTmp1 = "";

            string strTmp2 = "";

 

            tmpCounter = strInput.IndexOf("\\n");

                                   

            if (tmpCounter == -1)

            {

                        return strInput;

            }

            else

            {

                        strTmp1 = strInput.Substring(0,tmpCounter);

                        strTmp2 = strInput.Substring(strTmp1.Length + 2 ,strInput.Length

              -(tmpCounter+2) );

                        strTmp1 = strTmp1 + (char)10 + strTmp2;

                        return ReplaceForNewLineChar(strTmp1);

            }         

                                   

}

                       

public string ReplaceForCarriageReturnChar(string strInput)

{

            int tmpCounter = 0;

            string strTmp1 = "";

            string strTmp2 = "";

 

            tmpCounter = strInput.IndexOf("\\r");

                                   

            if (tmpCounter == -1)

            {

                        return strInput;

            }

            else

            {

                        strTmp1 = strInput.Substring(0,tmpCounter);

                        strTmp2 = strInput.Substring(strTmp1.Length + 2 ,strInput.Length

              -(tmpCounter+2) );

                        strTmp1 = strTmp1 + (char)13 + strTmp2;

                        return ReplaceForCarriageReturnChar(strTmp1);

            }
}

Do RETURN only for NEW things!

Up Next
    Ebook Download
    View all
    Learn
    View all