Solved

Special ASCII characters in ASP.NET DropDownList Control

Posted on 2010-09-05
9
1,551 Views
Last Modified: 2013-11-08
Dear experts,

I have the following piece of code on my ASP.NET web form:

<asp:DropDownList ID="CommandLineTerminatorList" runat="server" SelectedValue='<%# Bind("CommandLineTerminator") %>' ValidationGroup="NewValidators">
	<asp:ListItem Value="<%=vbCr%>">&lt;CR&gt;</asp:ListItem>
	<asp:ListItem Value="<%=vbCrLf%>">&lt;CR&gt;&lt;LF&gt;</asp:ListItem>
</asp:DropDownList>

Open in new window


I would like the Values to be saved in the database as the actual ASCII characters i.e. vbCr should be saved as the Carriage Return Character (ASCII 13) and vbCrLf should be saved as a Carriage Return + Line Feed (ASCII 13 and ASCII 10) Characters. At the moment, the values are being stored as strings "<%=vbCr%>" and "<%=vbCrLf%>"
0
Comment
Question by:bmatumbura
  • 5
  • 3
9 Comments
 
LVL 11

Author Comment

by:bmatumbura
ID: 33609304
By the way, I prefer a solution that does this from markup (and not from the code-behind file). I already have a work-around that uses the code-behind file.
0
 
LVL 3

Expert Comment

by:pnedic
ID: 33609508
Just I think:
1. have you tried instead of Value="<%=vbCr%>"> to put Value="Chr(13)" and Value="Chr(10)" for Line Feed.
I'm not quite sure what you mean with ASCII characters for Carrige return and Line feed. There are no "visual" presentation of those to things. You can see only a little square box instead of them. So you can save them on some other way.
2. For example in database you can save some other data for CR and LF (instead of pure ascii values): for example for CR you can store just "CR" and for LF save "LF". Then when you need to present "back" mentioned data in some your form or page, you can make some conversion code like:
If someVar = "CR" Then showValue = vbCr.
for CRLF just replace db string CRLF with vbCrLf
Regards Peter !
0
 
LVL 11

Author Comment

by:bmatumbura
ID: 33609868
@pnedic,

TRUE, there are no visual presentations of these characters, but my code displays them as (None), <CR> and <CR><LF> in the list. What I need is to set the Value property (and not the Text property) of the DropDownList control to these values. The challenge I have is I must store the values in the db as the actual Carriage Return and Line Feed characters. I don't have control (i.e source code) of the application that uses these fields in the db hence the requirement.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Expert Comment

by:pnedic
ID: 33610027
So, actually you need to store in DB cr value when you select e.g <CR> in combo ?

If so, then you have to find out how to present those "values" in your database (i.e. how those data is written in DB) !

 I know that if you want to output from Oracle the cr, you should write sql such as
SELECT Field1 || CHR(13) || Field2 from someTable Where ....
If you execute that in some db manager (like TOAD) you will see strange chartacter instead of CR. But if you pick up that result and present it on some form, you will get CR.

I don't know which DB you use, but CHR(13) should be universal for all databases (not sure).
In this case you should set the value for your combos, what ever you want - just not that what you use, and programticaly insert data into the DB.
0
 
LVL 11

Author Comment

by:bmatumbura
ID: 33610140
Yes, that's what I need to store. I am using SQL 2008. All I need is how present the values in the DropDownList's .Value property.
0
 
LVL 3

Expert Comment

by:pnedic
ID: 33610911
Well, some peple confirms that for carriage return should be used CHAR(13) and for Line feed CHAR(10).
See this link:http://blog.sqlauthority.com/2007/08/22/sql-server-t-sql-script-to-insert-carriage-return-and-new-line-feed-in-code/

But I have done some simple insert into my test table (based on mentioned link), but when done select from that table I haven't got CR and LF.
But this depends of interface that should present data. I believe that in ASP.NET page you will get new lines.
Anyhow, try this. Also I found on some link that is used NCHAR(13) instead of CHAR(13).
0
 
LVL 11

Accepted Solution

by:
bmatumbura earned 0 total points
ID: 34051110
Sorry, but this solution doesn't work. I managed to resolve this issue on my own by using Entity Framework in the code-behind file. I intercepted the Inserting/Updating events of the entity datasource and modified the value of the CommandLineTerminatorList field to store VBCR, VBCRLF or Nothing.
0
 
LVL 11

Author Closing Comment

by:bmatumbura
ID: 34082680
Managed to resolve the issue on my own
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question