CSV Validation In C# - Making Sure Each Row Has The Same Number Of Commas

Mar 10, 2011

I wish to implement a fairly simple CSV checker in my C#/ASP.NET application - my project automatically generates CSV's from GridView's for users, but I want to be able to quickly run through each line and see if they have the same amount of commas, and throw an exception if any differences occur. So far I have this, which does work but there are some issues I'll describe soon:

int? CommaCount = null;

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
String Str = null;
//This loops through all the headerrow cells and writes them to the stringbuilder
for (int k = 0; k <= (grd.Columns.Count - 1); k++)
{
sw.Write(grd.HeaderRow.Cells[k].Text + ",");
}
sw.WriteLine(",");
//This loops through all the main rows and writes them to the stringbuilder
for (int i = 0; i <= grd.Rows.Count - 1; i++)
{
StringBuilder RowString = new StringBuilder();
for (int j = 0; j <= grd.Columns.Count - 1; j++)
{
//We'll need to strip meaningless junk such as <br /> and
Str = grd.Rows[i].Cells[j].Text.ToString().Replace("<br />", "");
if (Str == " ")
{
Str = "";
}
Str = """ + Str + """ + ",";
RowString.Append(Str);
sw.Write(Str);
}
sw.WriteLine();
//The below code block ensures that each row contains the same number of commas, which is crucial
int RowCommaCount = CheckChar(RowString.ToString(), ',');
if (CommaCount == null)
{
CommaCount = RowCommaCount;
}
else
{
if (CommaCount!= RowCommaCount)
{
throw new Exception("CSV generated is corrupt - line " + i + " has " + RowCommaCount + " commas when it should have " + CommaCount);
}
}
}
sw.Close();

And my CheckChar method:

protected static int CheckChar(string Input, char CharToCheck)
{
int Counter = 0;
foreach (char StringChar in Input)
{
if (StringChar == CharToCheck)
{
Counter++;
}
}
return Counter;
}

Now my problem is, if a cell in the grid contains a comma, my check char method will still count these as delimiters so will return an error. As you can see in the code, I wrap all the values in " characters to 'escape' them. How simple would it be to ignore commas in values in my method? I assume I'll need to rewrite the method quite a lot.

View 4 Replies


Similar Messages:

Forms Data Controls :: Format A Number With Commas?

Apr 9, 2010

I have a number that is generated from a SQL query that is populated into a tablecell as follows:

[Code]....

I get the correct value in my tablecell, except the value is 15345432. I would like to format this to show 15,345,432How can I go about doing this?

View 3 Replies

Making The Container Of A Validation Summary Visible When The Validation Summary Becomes Visible?

Nov 29, 2010

I have the following markup. The errorPanel was first only used to show server side exception messages, and works fine like that. Now I'd like to incorporate my validation summary into that same errorPanel.

[code]....

My problem now is that the required validation happens client side, and I want to keep that, so I have no server side opportunity to make errorPanel visible, in order to make the validation summary visible.

I see I have two options: Do validation server side, and use my code there to make the panel visible, or hook into the client side code somehow and catch an event there when the summary should be made visible, and then also make the errorPanel visible. How could I go about the latter?

View 1 Replies

Validation With Javascript On Age And Mobile Number?

Jan 5, 2010

I want to do validation in asp.net with javascript on age and mobile number. Can you suggest how I could do this?

View 3 Replies

Mvc Validation Must Be A Number Custom Error?

Dec 23, 2010

I am new to asp.net and I have a problem. When the users insert in a editor for a decimal field something other than numbers, they get an error "Field name" is not a number. But I don't want them to receive this message I want them to receive another message. I have no problem with this with required and range validators. Is there any way for me to do this?

I am not refering necessarily to changing the culture just displaying another message.

View 6 Replies

Phone Number Validation Using JavaScript With .NET?

Feb 25, 2011

How do I validate phone numbers using JavaScript in ASP.NET?

View 2 Replies

Web Forms :: Validation Expression For Decimal Number?

Jan 10, 2011

What is the validation expression for validating numbers with 2 decimal places (Only Zeros).

Each number must have two zeros as decimal places

valid numbers

0.00
23.00
34.00
234.00

Invalid numbers

1.01
1.0
23.06
0.09

View 8 Replies

Web Forms :: Regular Expression For Fax Number Validation

May 7, 2015

I need regular expression for fax like

+(country code)-(area code)-(fax number)

for example: +1-212-9876543

and with error message,

if miss '-' then show 'missing '-'', if missing '+' then show 'missing '+''.

View 1 Replies

Datatable Field Validation - How To Validate Port Number

Jul 23, 2010

i have imported data from excel to datatable but i need to validate port no column which should

allow only numeric value and also i need to check the single quotes and need to replace to double quotes.

View 2 Replies

Web Forms :: Form Validation Of Mobile And Telephone Number?

Sep 16, 2010

I have a form for the user to add a Contact's details. Within this form I have fields to enter telephone, mobile, fax numbers etc.

Each have a separate textbox for country code, area code and number. Basically, what I want to do is have validation on these fields so that all or none of the textboxes are filled.

So if the user just adds a number in they are required to enter also the country code and the area code. Or if they don't enter any of the details then the validation will not kick in.

View 4 Replies

Web Forms :: Validate Number For Regular Express Validation?

Mar 28, 2011

how can i validate a textbox with 5 number only are allow to fill in and first number must number 3. For example like 3XXXX,3YYYY (X and Y are number).

View 2 Replies

MVC :: Apply The Validation,filtered For Phone Number Textbox?

Mar 10, 2010

how to apply the validation,filtered for phone number textbox.

View 4 Replies

Web Forms :: Regular Expression For Number Range Validation

May 7, 2015

I need regular expression validation for textBox  

(integer and min value 200)

If input text 200, 201,202  OK

Else error input!

View 1 Replies

Web Forms :: Validation To Allow Only Fixed Number Of Characters In TextBox?

May 7, 2015

in my textbox i need to enter only three charcter like CEL in caps letter else i need to  show error.  

if(txtsid.text==3) means it throw error

View 1 Replies

Trimming Commas From The End Of A Stringbuilder?

Jan 22, 2010

I've a string builder, and I need to convert it to a string, and then trim any commas if there are any there (I need to get rid of them because I'm building an sql query).

Here's what I'm doing:

myStringBuilder.ToString().TrimEnd(',');

Why isn't this working??? It's not trimming any commas at the end!

I'm using c# :)

View 9 Replies

AJAX :: FilteredTextBoxExtender Want To Allow Commas?

Jan 21, 2011

i am using the filtered text box extender.I want to allow commas like "," but not possible with the code below.how do i change this to make commas acceptable?

View 3 Replies

Forms Data Controls :: Validation For Number Of Words In A Text Box

Mar 8, 2010

I am having some text boxes in my webform. I need to set numbers of words for each textbox. if i put more than defined no of words it should show msgs or popup msgs whatever it may be.

View 2 Replies

Web Forms :: Range Validation To Allow Value Between Negative And Positive Number In TextBox

May 7, 2015

I want to put validation in a Textbox, that the numbers entered inside textbox should be between 1 to -1 only i.e., (-1, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7 ....... 1 )I tried below code but "RangeValidator" is not working as per requirement:

HTML:

<asp:RangeValidator ID="RangeValidator1" runat="server" MaximumValue="1" MinimumValue="-1" Text="value between 1 to -1" ErrorMessage="value between 1 to -1" ControlToValidate="TxtY" ValidationGroup="usrvalid"></asp:RangeValidator>
<asp:TextBox ID="TxtY" Text='<%#Eval("Y") %>' runat="server" Width="30%"></asp:TextBox>
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender5" TargetControlID="TxtY" FilterType="Custom" ValidChars="-01234567890." runat="server"></cc1:FilteredTextBoxExtender>

How to achieve it.

View 1 Replies

How To Read A CSV With Embedded Commas In Fields

Oct 18, 2010

I am trying to read a CSV file which has commas embedded within certain fields.

Eg.

"Joe Bloggs", "123 Bloggs Street, Bloggsville, MA, USA", "123.34", "XYZ"
"John Doe", "12 JD Avenue, MA, USA", "53.2", "QRS"

As you can see I have the same amount of fields with a differing amount of comma characters in each. I was using the comma character (',') to split my string into different fields but this gave me varied results.

I have no control over the format of the file so restrucuring it is not an option.

Is there a way where I can split the string by using the comma and maintaining the address field as a single field.

View 2 Replies

Possible To Get The CompareValidator To Accept Numbers With Commas?

Aug 31, 2010

I had been doing a type check for Double for an input field on a web page but now I need to allow commas. Can this be done using a CompareValidator or do I need to use a regex validator?

View 1 Replies

Facebox Adding Commas To Input?

Jan 8, 2010

I'm using a facebox to display a form inside a lightbox, nothing too exciting (just a couple of datepickers, some textboxes and a checkbox). However, I'm having issues with the postbacks, whenever I post back from the facebox it adds a ',' to the start of the input (so "rabbit" becomes ",rabbit") Now, I saw that there was the same issue with the modalpopup extender from the ajaxcontroltoolkit, so I assume it's a common issue.

Can anyone either explain why this is happening, or tell me how to fix it? provide a decent way of fixing this?

See attached answer for a correct solution (I fixed this eventually but didn't want to ruin the bounty question so left the answer until afterwards).

View 3 Replies

Commas Not Recognized In File Exists?

Jul 21, 2012

I have a .net aspx project done with VS 2008 using the .Net 2.xxxx framework. This ASPX application is deployed on and 4 different 2003 Server boxes. 3 are running in total beautifully. In the 4th box is my issue. I do a FILE.EXISTS conditional test looking for case file pdf's. These case numbers are structured as such 2012-C-0421,001,0008.PDF

Please note the comma's used in the path. 3 of the 4 servers in court houses recognize the path to the pdf WITH the commas in them perfectly and the FILE.EXISTS works 100%. On the fourth box (same config we THINK) the exact same FILE.EXISTS does not return a hit even though we see the document sitting in the folder via explorer. IF we remove the commas and make it 2012-C-0421001008.pdf all is fine and document is indeed discovered with the EXISTS.

WHY on this one server does the original ducument path not function. We can not ask the court houses to change the way they store their documents so I need to make this work like the other 3. Again... same version..same .net framework... same OS (2003 server)

View 4 Replies

Web Forms :: Export To CSV File - Missing All Commas?

Jun 8, 2010

I use following code to export data to csv file:

Response.AddHeader("content-disposition", "attachment;filename=UnicodeChar.csv");
Response.ContentType = "application/octet-stream";
StreamWriter sw = new StreamWriter(Response.OutputStream, Encoding.UTF8);[code]...

It works fine. Then I download the csv file to local and open it. At last, I save it. I open the csv file with notepad, I found it misses all commas.

View 15 Replies

AJAX :: Modal Popup Inserting Unnecessary Commas?

Sep 20, 2010

I have a textbox inside modal popup. when i write some data into the textbox and hit ok it is appending unnecessary commas. How to remove them.

View 5 Replies

Name Or Number Search In Textbox While Entering The First Letter Or Number Charactor Or Number?

Feb 2, 2010

I am using the asp.net and framework 2.0 with Ajax enable web.i have a text box and when user will enter "a" or number in this will search the name started from "a" character or number if he or she enter the number basically we can say live-search.

Employee search: Textbox.In this text box she/he will enter the first character of name or first number of employeeno and according to the a character name will search in list.

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved