Using C# and SQL I need to write a telephone number to an Access database. The value gets written to the database but the leading zero is lost in the database itself. How can I keep the leading zero?
Thanks in advance.
Use a Text field instead of a numeric one. Numeric fields do not retain formatting information (such as leading zeroes).
Related
I am using data from csv in data driven coded ui.
In one of the columns(say "File Code") there are strings like 001,
abc123 etc.
I need to use the data of this column to search for results. But coded ui is not entering the leading 0's in the TextBox,instead,its just skipping the 0's and entering the latter part of the data.
How can i make the test enter the 0's as well.
Coded UI reads CSV data in much the same way as Excel. It tries to infer the type of fields correctly. So fields that look like numbers, date and times are so interpreted.
One solution is to force a different interpretation. Possibilities include enclosing the field in double quotes or embedding a space.
Example:
Name,Number,Telephone
Alice,"001",01 234 567890
Brian,"002",01 234 678901
When we use C# with the Universe database the multi values are coming from the Universe Database as comma separated values to the programming site. Normally in Pick Basic language programming they come up as a ^252 or ^253 separated values. Therefore we can split the multi value easily with value separators because people don’t use the ^252 or ^253 in normal data entries.
But in C# when we select multi values from the Universe database they comes up with comma separated. If the multi value data actually contains a comma then we can’t use the comma value(,) as a value separator. Because this will split the multi value data in the wrong position.
For example if the multi value data is :
01 Although , we will do , Tom goes there , I will come down
The multi value for the above record are separated by a comma in the .net programming. But the first value(in bold) actually contains a comma after the “Although”.
We are facing problem to use the C# Split function to separate the data and get the individual values. Could you tell us how can we can overcome this in C# orVB.net programming with Universe database and get the individual values/sub values? .
Thank you.
In general field delimiters are required precisely for the problem you are describing. If you use " you will then also need to decide what to do when your data inside a field also holds a " in it.
When you have found a good field delimiter (one with small memory foot print and that your data is not likely to contain). You can create a regular expression to grab the data from each field.
Like others have said, some code snippets or samples will mean that answers are more accurate and helpful.
Is there anyway you can bring the data back into a specific Type, for example MS DataTable or your own structure? List, where Row is a Type you create to store all possible fields in your specific data model?
I am having an issue with importing a CSV file. The problem arises when an address field has multiple comma seperated values e.g. home no, street no, town etc.
I tried to use http://forums.asp.net/t/1705264.aspx/1 this article but, the problem did not solved because of a single field containing multiple comma separated values.
Any idea or solution? because I didnt found any help
Thanks
Don't split the string yourself. Parsing CSV files is not trivial, and using str.Split(',') will give you a lot of headaches. Try using a more robust library like CsvHelper
- https://github.com/JoshClose/CsvHelper
If that doesn't work then the format of the file is probably incorrect. Check to make sure the text fields are properly quoted.
Do you control the format of the CSV file? You could see about changing it to qualify the values by surrounding them with double quotes (""). Another option is to switch to a different delimiter like tabs.
Barring that, if the address is always in the same format, you could read in the file, and then inspect each record and manually concatenate columns 2, 3, and 4.
Are the fields surrounded by quotation marks? If so, split on "," rather than just ,.
Is the address field at the beginning or end of a record? If so, you can ignore the first x commas (if at the beginning) or split only the correct number of fields (if at the end).
Ideally, if you have control of the source file's creation, you would change the delimiter of either the address sub-fields, or the record fields.
My query looks like:
string str = string.Format("Insert into [MyDB].[dbo].[tlb1] ([file_path],[CONTENT1],[CONTENT2]) values ('{0}','{1}','{2}');", fullpath, _val[0], _val[1]);
Now when I insert data into database if array _val[] contains data in english language it insert correctly but when array contains data in Russian Language in database this show like ???????????????????????
Is there a way to insert data in Russian Language from an array.
According to this (Archived) Microsoft Support Issue:
You must precede all Unicode strings with a prefix N when you deal with Unicode string constants in SQL Server
First of all, you should use prepared statements and let the database driver insert the placeholders correctly (i.e. SqlCommand with parameters). Then the issue should go away (as well as any potential SQL injection problems).
As a quick fix in your case: Prefix the string literals you're inserting with N:
... values (N'{0}',N'{1}',N'{2}')
This causes the literals to be Unicode literals, not arbitrary-legacy-codepage ones and thus preventing the conversion from Unicode to the legacy codepage (which results in question marks for characters that cannot be represented).
It seems that the datatype of columns [Content1] and [Content2] is nchar. You should convert the columns to nvarchar which is used to store unicode data.
First of all you must see Database codepage at server. May be non-Unicode CP in database, but data from your app comes in Unicode format.
I have to build a C# program that makes CSV files and puts long numbers (as string in my program). The problem is, when I open this CSV file in Excel the numbers appear like this:
1234E+ or 1234560000000 (the end of the number is 0)
How I retain the formatting of the numbers? If I open the file as a text file, the numbers are formatted correctly.
Thanks in advance.
As others have mentioned, you can force the data to be a string. The best way for that was ="1234567890123". The = makes the cell a formula, and the quotation marks make the enclosed value an Excel string literal. This will display all the digits, even beyond Excel's numeric precision limit, but the cell (generally) won't be able to be used directly in numeric calculations.
If you need the data to remain numeric, the best way is probably to create a native Excel file (.xls or .xlsx). Various approaches for that can be found in the solutions to this related Stack Overflow question.
If you don't mind having thousands separators, there is one other trick you can use, which is to make your C# program insert the thousands separators and surround the value in quotes: "1,234,567,890,123". Do not include a leading = (as that will force it to be a string). Note that in this case, the quotation marks are for protecting the commas in the CSV, not for specifying an Excel string literal.
Format those long numbers as strings by putting a ' (apostrophe) in front or making a formula out of it: ="1234567890123"
You can't. Excel stores numbers with fifteen digits of precision. If you don't mind not having the ability to perform calculations on the numbers from within Excel, you can store them as Text, and all of the digits will display.
When I generate data to imported into Excel, I do not generate a CSV file if I want control over how the data are displayed. Instead, I write out an Excel file where the properties of the cells are set appropriately. I do not know if there is a library out there that would do that for you in C# without requiring Excel to be installed on the machine generating the files, but it is something to look into.
My two cents:
I think it's important to realize there is a difference between "Data" and "Formatting". In this example you are kind of trying to store both in a data-only file. This will, as you can tell from other answers, change the nature of the data. (In other words cause it to be converted to a string. A CSV file is a data only file. You can do some tricks here and there to merge formatting in with data, but to my way of thinking this essentially corrupts the data by merging it with non-data values: ie: "Formatting".
If you really need to be able to store formatting information I suggest that, if you have time to develop it out, you switch to a file type capable of storing formatting info separately from the data. It sounds like this problem would be a good candidate for a XML Spreadsheet solution. In this way you can not only specify your data, but also it's type and any formatting you choose to use.