file contents
-
Hi huys I have small issue, look at my code below:
HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
string strFilename = Path.GetFileName(myFile.FileName);
string[] lines = File.ReadAllLines(strFilename);
foreach (string s in lines)
{
string[] fragments = s.Split(',');errors.Text += fragments\[0\] + " "; errors.Visible = true; }
The contents of my file look like: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 what happens is that when i pick
errors.Text += fragments[0];
it returns "Kiss Person" which are the first index[0]s in both lines. I just want to retrieve "Kiss" how can I do this?:confused: Please help me in case am making a mistake somewhere. Morg
-
Hi huys I have small issue, look at my code below:
HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
string strFilename = Path.GetFileName(myFile.FileName);
string[] lines = File.ReadAllLines(strFilename);
foreach (string s in lines)
{
string[] fragments = s.Split(',');errors.Text += fragments\[0\] + " "; errors.Visible = true; }
The contents of my file look like: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 what happens is that when i pick
errors.Text += fragments[0];
it returns "Kiss Person" which are the first index[0]s in both lines. I just want to retrieve "Kiss" how can I do this?:confused: Please help me in case am making a mistake somewhere. Morg
It is concatenating both parts in the loop (underlined)
foreach (string s in lines) { string\[\] fragments = s.Split(','); errors.Text += fragments\[0\] + " "; errors.Visible = true; }
I'm not sure what you want it to do from there. You either need to create a list of errors and add in turn, or return the error inside the loop if you want to stop processing.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
It is concatenating both parts in the loop (underlined)
foreach (string s in lines) { string\[\] fragments = s.Split(','); errors.Text += fragments\[0\] + " "; errors.Visible = true; }
I'm not sure what you want it to do from there. You either need to create a list of errors and add in turn, or return the error inside the loop if you want to stop processing.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
Keith Barrow wrote:
errors.Text += fragments[0] + " ";
oh yes I know that, but first when i put:
foreach (string s in lines)
{
string[] fragments = s.Split(',');
errors.Text = fragments[0];
errors.Visible = true;
}then it will give me the first index[0] in the last line(Person) not (Kiss). File contents: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 and if I do this:
foreach (string s in lines)
{
string[] fragments = s.Split(',');
errors.Text += fragments[0];
errors.Visible = true;
}then it will give me both "Person" and "Kiss". But I only want to retrieve "Kiss" the first index[0] in the first line. Do you think am screwing up somewhere?
-
Hi huys I have small issue, look at my code below:
HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
string strFilename = Path.GetFileName(myFile.FileName);
string[] lines = File.ReadAllLines(strFilename);
foreach (string s in lines)
{
string[] fragments = s.Split(',');errors.Text += fragments\[0\] + " "; errors.Visible = true; }
The contents of my file look like: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 what happens is that when i pick
errors.Text += fragments[0];
it returns "Kiss Person" which are the first index[0]s in both lines. I just want to retrieve "Kiss" how can I do this?:confused: Please help me in case am making a mistake somewhere. Morg
You use indexof and substring function to retrieve only "Kiss". foreach (string s in lines) { int i = s.indexof(','); string sub = s.substring(0,i); }
-
Keith Barrow wrote:
errors.Text += fragments[0] + " ";
oh yes I know that, but first when i put:
foreach (string s in lines)
{
string[] fragments = s.Split(',');
errors.Text = fragments[0];
errors.Visible = true;
}then it will give me the first index[0] in the last line(Person) not (Kiss). File contents: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 and if I do this:
foreach (string s in lines)
{
string[] fragments = s.Split(',');
errors.Text += fragments[0];
errors.Visible = true;
}then it will give me both "Person" and "Kiss". But I only want to retrieve "Kiss" the first index[0] in the first line. Do you think am screwing up somewhere?
Like I said, it's your loop. If you only ever want to process the first line, this is the job:
string[] fragments = lines[0].Split(',');
errors.Text += fragments[0];
errors.Visible = trueI think I'm missing the point, as you must have put the loop in for a reason.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
Hi huys I have small issue, look at my code below:
HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
string strFilename = Path.GetFileName(myFile.FileName);
string[] lines = File.ReadAllLines(strFilename);
foreach (string s in lines)
{
string[] fragments = s.Split(',');errors.Text += fragments\[0\] + " "; errors.Visible = true; }
The contents of my file look like: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 what happens is that when i pick
errors.Text += fragments[0];
it returns "Kiss Person" which are the first index[0]s in both lines. I just want to retrieve "Kiss" how can I do this?:confused: Please help me in case am making a mistake somewhere. Morg
Instead of using a
foreach
construct to loop over all the lines use only the first line (if I understood your question correctly):string[] lines = File.ReadAllLines(strFilename);
string[] fragments = lines[0].Split(',');
errors.Text += fragments[0];
errors.Visible = true; -
Like I said, it's your loop. If you only ever want to process the first line, this is the job:
string[] fragments = lines[0].Split(',');
errors.Text += fragments[0];
errors.Visible = trueI think I'm missing the point, as you must have put the loop in for a reason.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
You are a star man, exactly what I want to do... Thanks again:thumbsup::thumbsup:
-
Instead of using a
foreach
construct to loop over all the lines use only the first line (if I understood your question correctly):string[] lines = File.ReadAllLines(strFilename);
string[] fragments = lines[0].Split(',');
errors.Text += fragments[0];
errors.Visible = true;Yes man you did understand my question, this works exactly the way I want it to. Thanks alot man:thumbsup::thumbsup:
-
You use indexof and substring function to retrieve only "Kiss". foreach (string s in lines) { int i = s.indexof(','); string sub = s.substring(0,i); }
Because that is far more succinct than he currently has. Also, when he needs to process other elements your code should much easier to do than in than his own. Oh, hang on everything, except this sentence) is a whopping lie.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
You are a star man, exactly what I want to do... Thanks again:thumbsup::thumbsup:
No probs, somtimes the hardest ones to spot are the easiest.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
Because that is far more succinct than he currently has. Also, when he needs to process other elements your code should much easier to do than in than his own. Oh, hang on everything, except this sentence) is a whopping lie.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
i just give him answer. I think i forgot to remove foreach loop....
-
i just give him answer. I think i forgot to remove foreach loop....
Actually, it looks more like the thing you needed to change you didn't and vice versa.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
Actually, it looks more like the thing you needed to change you didn't and vice versa.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
hmmmmmm k i ll. Any way wat succit.. ;P
-
No probs, somtimes the hardest ones to spot are the easiest.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
Cool, hey now another problem is that what if I don't know the number of lines a file has, how can I deal with this line:
string[] fragments = lines[0].Split(',');
especially if I want to read all the lines of that file and I don't know how many lines the file has.:confused: Thanks for being helpful man
-
hmmmmmm k i ll. Any way wat succit.. ;P
Uni-Vote all you like, my rep'll take it :rolleyes:
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
Cool, hey now another problem is that what if I don't know the number of lines a file has, how can I deal with this line:
string[] fragments = lines[0].Split(',');
especially if I want to read all the lines of that file and I don't know how many lines the file has.:confused: Thanks for being helpful man
The code I posted only reads the first line, and will throw an error if the file is empty To process the whole file you need the loop. You need to decide how you are going to handle it. The way I see it you have a few realistic options:
- Create a list of errors and add to it in the loop.
- Only register the first error and continue processing.
- Break out of the loop if an error is found.
Which of these apply?
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
The code I posted only reads the first line, and will throw an error if the file is empty To process the whole file you need the loop. You need to decide how you are going to handle it. The way I see it you have a few realistic options:
- Create a list of errors and add to it in the loop.
- Only register the first error and continue processing.
- Break out of the loop if an error is found.
Which of these apply?
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
Cool bro I have figured it out check below:
manager = new data_manager();
HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
string strFilename = Path.GetFileName(myFile.FileName);
if (strFilename != "")
{
string[] lines = File.ReadAllLines(strFilename);
for (int b = 0; b < lines.Length; b++)
{
string[] fragments = lines[b].Split(',');
int count = lines.Length;
errors.Text = fragments[0] + count.ToString();
errors.Visible = true;
string first = fragments[0]; string last = fragments[1]; string email = fragments[2];
string phone = fragments[3]; string birth = fragments[4];
string addrecord = manager.AddContact(first, last, email, phone, birth, Request.QueryString["userid"]);if (addrecord == "done") { error.Text = "Contact added check in the table below!"; error.Visible = true; FillContacts(Request.QueryString\["userid"\]);//update datagrid after deleting a contact } else { error.Text = addrecord; error.Visible = true; } } } else { errors.Text = "No file with content was selected"; errors.Visible = true; }
and this works quiet well....super! :laugh:
-
hmmmmmm k i ll. Any way wat succit.. ;P
Nope. I've run that through every possible combination of Google Translate and it keeps coming back with the same response: You're a dribbling idiot.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
You use indexof and substring function to retrieve only "Kiss". foreach (string s in lines) { int i = s.indexof(','); string sub = s.substring(0,i); }
Bad news for you mate. Under the new reputation system, your points total goes down everytime you post something stupid. It's only the daily cap limit that's stopping your reputation being minus several hundred thousand.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.