whats the best way to do this?

S

Steve

I have to create 2 strings and then parse one string out to save the data
into the database.

My first string looks like this:

John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected],

I then need to parse out that string to seperate them by the commas so I see
this
John|Smith|[email protected]
Greg|Henry|[email protected]
Kelly|Smith|[email protected]


I then need to parse out that string so I get
John
Greg
Kelly

Smith
Henry
Smith

so I can save each value to the database, what is the best way to do this?
The stored procedure I'm calling is already parsing strings by the comma, so
would it be easier to just parse out the large string in code and pass the
smaller string with the pipe (|) and make a change to the proc to parse the
values by the (|) or should I do all of this in my C# code? If the C# way,
how would that be done? I'm able to get this string format
(John|Smith|[email protected]) but when I try to parse it out, I' get errors
(Index was outside the bounds of the array)
 
S

Steve

I am currently doing that but when I split the first string by the comma(,)
its fine, its the second string John|Smith|[email protected] thats giving me
a fit.

when i do this:

String[] names= ViewState["custnames"].ToString().Split('|');
foreach (String n in names)
{
String[] u = n.ToString().Split('|');
Response.Write(u[0]);
Response.Write(u[1]);
}

I'm getting this error:
Index was outside the bounds of the array, but if I do this
String[] names= ViewState["custnames"].ToString().Split('|');
foreach (String n in names)
{
String[] u = n.ToString().Split('|');
Response.Write(u[0]);
}

I can see the first name in the string with no problem. So am I missing
something or doing something wrong in the sytnax?
 
S

Steve

Mark,
I've tried that, I've tried everything I can think of and it only shows
[0] and anything beyond that give me the out of bounds error message.


Mark Rae said:
so I can save each value to the database, what is the best way to do
this?

strRaw =
"John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected],";

foreach (string strRecord in strRaw.Split(','))
{
string[] astrElements = strRecord.Split('|');

strFirstName = astrElements[0];
strLastName = astrElements[1];
strEmailAddress = astrElements[2];
}
 
M

Mark Rae [MVP]

I've tried that, I've tried everything I can think of and it only shows
[0] and anything beyond that give me the out of bounds error message.

Not sure what's happening, then - I've just tried it, and it works fine for
me.

Can you post your whole code...
 
S

Steve

sure:

code:
main string:
John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected],

String[] users= ViewState["Stuff"].ToString().Split(',');

which returns this:
John|Smith|[email protected],

then I loop through that string;
foreach (string s in users)
{
string[] s= strRecord.Split('|');

strFirstName = s[0];
strLastName = s[1];
strEmail = s[2];
}
Response.Write(strFirstName + " " + strLastName + " " +
strEmail);

and I get this error:
Index was outside the bounds of the array.
strLastName = astrElements[1];

Mark Rae said:
I've tried that, I've tried everything I can think of and it only shows
[0] and anything beyond that give me the out of bounds error message.

Not sure what's happening, then - I've just tried it, and it works fine
for me.

Can you post your whole code...
 
A

Alex Meleta

Hi Steve,

Probably the exception occurs in that piece of code:
S> Response.Write(u[1]);

You should check the boundaries.
Anyway, you can try to analyse that input string is correct, for doing this
change your code to something like code below:

foreach (String n in names)
{
foreach(String justForTest in n.ToString().Split('|'))
{ Response.WriteLine(justForTest); }
Response.WriteLine("-");
}

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



S> I am currently doing that but when I split the first string by the
S> comma(,) its fine, its the second string John|Smith|[email protected]
S> thats giving me a fit.
S>
S> when i do this:
S>
S> String[] names= ViewState["custnames"].ToString().Split('|');
S> foreach (String n in names)
S> {
S> String[] u = n.ToString().Split('|');
S> Response.Write(u[0]);
S> Response.Write(u[1]);
S> }
S> I'm getting this error:
S> Index was outside the bounds of the array, but if I do this
S> String[] names= ViewState["custnames"].ToString().Split('|');
S> foreach (String n in names)
S> {
S> String[] u = n.ToString().Split('|');
S> Response.Write(u[0]);
S> }
S> I can see the first name in the string with no problem. So am I
S> missing something or doing something wrong in the sytnax?
S>
S> S>
Hi Steve,

S>
John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|ks@gmai

The <string>.Split(<separator>) method can be helpful.
http://msdn2.microsoft.com/en-us/library/system.string.split.aspx

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
 
S

Steve

I've done that and I can see the string that is being passed

John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected],

I can see that just fine, hell i can even parse it out by the commas, its
splitting this string:
John|Smith|[email protected]

thats joking on me

Alex Meleta said:
Hi Steve,

Probably the exception occurs in that piece of code:
S> Response.Write(u[1]);

You should check the boundaries.
Anyway, you can try to analyse that input string is correct, for doing
this change your code to something like code below:

foreach (String n in names)
{
foreach(String justForTest in n.ToString().Split('|'))
{ Response.WriteLine(justForTest); }
Response.WriteLine("-");
}

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



S> I am currently doing that but when I split the first string by the
S> comma(,) its fine, its the second string John|Smith|[email protected]
S> thats giving me a fit.
S> S> when i do this:
S> S> String[] names= ViewState["custnames"].ToString().Split('|');
S> foreach (String n in names)
S> {
S> String[] u = n.ToString().Split('|');
S> Response.Write(u[0]);
S> Response.Write(u[1]);
S> }
S> I'm getting this error:
S> Index was outside the bounds of the array, but if I do this
S> String[] names= ViewState["custnames"].ToString().Split('|');
S> foreach (String n in names)
S> {
S> String[] u = n.ToString().Split('|');
S> Response.Write(u[0]);
S> }
S> I can see the first name in the string with no problem. So am I
S> missing something or doing something wrong in the sytnax?
S> S>
Hi Steve,

S>
John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|ks@gmai

The <string>.Split(<separator>) method can be helpful.
http://msdn2.microsoft.com/en-us/library/system.string.split.aspx

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
 
M

Mark Rae [MVP]

sure:

code:

<snip>

Hmm - can't see anything obviously wrong with that...

I'm clutching at straws now, but could you tell me what s.Length returns?
I'm wondering if there's some sort of CultureInfo conflict here which has
caused the Split method to fail to interpret the pipe symbol correctly... If
s.Length returns 1 instead of 3, I think that might be the reason...
 
S

Steve

I'm only using the one string right now just to parse it out. I'm using this
string only right now.
John|Smith|[email protected],"


this returns me 80
foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');
Response.Write(astrElements[0].Length);
}

this returns me 520
foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');
Response.Write(strRecord.Length);
}

this returns me 2
String[] userDetails = "John|Smith|[email protected],"
Response.Write(userDetails.Length);
 
A

Alex Meleta

Hi Steve,

S> John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected],
Yes, indeed. Because you have the comma at the end of the string and it is
an empty string. You even can split it by '|' but it will be still empty.
So when you try to access it by indexer an exception will be thrown.

So, you should do these:
1. Set up a boundary checks to be sure that length of the array is enough
for access by indexer
2. Bring you string to appropriate format (use Trim methods for cutting or
such) or check that string can be splitted or was splitted correctly.

Hopefully it helps

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com




S>
 
J

Juan T. Llibre

Hi, Steve.

For that string, this works for me:

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(Object obj, EventArgs e)
{
string strRaw = "John|Smith|[email protected]";
string[] astrElements = strRaw.Split('|');

string strFirstName = astrElements[0];
string strLastName = astrElements[1];
string strEmail = astrElements[2];

lblFirstName.Text = strFirstName;
lblLastName.Text = strLastName;
lblEmail.Text = strEmail;

}
</script>
<html>
<body>
<asp:Label ID="lblFirstName" Runat="server" Text=""></asp:Label><br />
<asp:Label ID="lblLastName" Runat="server" Text=""></asp:Label><br />
<asp:Label ID="lblEmail" Runat="server" Text=""></asp:Label><br />
</body>
</html>
------------




Steve said:
I'm only using the one string right now just to parse it out. I'm using this string only right now.
John|Smith|[email protected],"


this returns me 80
foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');
Response.Write(astrElements[0].Length);
}

this returns me 520
foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');
Response.Write(strRecord.Length);
}

this returns me 2
String[] userDetails = "John|Smith|[email protected],"
Response.Write(userDetails.Length);
 
S

Steve

I do that in this

I'm doing that in this;

string temp =
"John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected]"
;
string strFirstName = string.Empty;
string strLastName = string.Empty;
string strEmail = string.Empty;

string[] userDetails = temp.Split(',');

foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');

strFirstName = astrElements[0];
strLastName = astrElements[1];
strEmail = astrElements[2];
}
Response.Write(strFirstName + " " + strLastName + " " +
strEmail);

but i'm only getting the last person in the string. Also if I use trim(),
I'm getting errors when i compile
 
M

Mark Rae [MVP]

I'm only using the one string right now just to parse it out. I'm using
this string only right now.
John|Smith|[email protected],"

this returns me 80

That simply isn't possible! There aren't even 80 letters in that string,
never mind 80 elements!

Are you following the method I showed you?

Specifically, you need first to split the string by the comma to give an
array of strings for each record.

Then you need to split each record by the pipe symbol to give an array of
individual elements.

I don't think you're doing that...
 
M

Mark Rae [MVP]

but i'm only getting the last person in the string.

Hmm - is it possible that each record is overwriting the previous one so
that only the last record remains? I don't think Response.Write would do
that, but we're starting to run out of options here!
Also if I use trim(), I'm getting errors when i compile

You will do, because C# is case-sensitive. Try this instead:
string[] userDetails = temp.Trim(',').Split(',');
 
J

Juan T. Llibre

re:
!> but i'm only getting the last person in the string. Also if I use trim(),

To get around that, after :

string[] astrElements = strRecord.Split('|');

....iterate through the array :

Response.Write(astrElements[0].ToString() + "<br />" );
Response.Write(astrElements[1].ToString() + "<br />" );
Response.Write(astrElements[2].ToString() + "<br />" );

You can also resplit astrElements and parse the individual records.






Steve said:
I do that in this

I'm doing that in this;

string temp = "John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected]" ;
string strFirstName = string.Empty;
string strLastName = string.Empty;
string strEmail = string.Empty;

string[] userDetails = temp.Split(',');

foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');

strFirstName = astrElements[0];
strLastName = astrElements[1];
strEmail = astrElements[2];
}
Response.Write(strFirstName + " " + strLastName + " " + strEmail);

but i'm only getting the last person in the string. Also if I use trim(), I'm getting errors when i compile
Alex Meleta said:
Hi Steve,

S> John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected],
Yes, indeed. Because you have the comma at the end of the string and it is an empty string. You even can split it by
'|' but it will be still empty. So when you try to access it by indexer an exception will be thrown.
So, you should do these:
1. Set up a boundary checks to be sure that length of the array is enough for access by indexer
2. Bring you string to appropriate format (use Trim methods for cutting or such) or check that string can be splitted
or was splitted correctly.

Hopefully it helps

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com




S>
 
J

Juan T. Llibre

Here's the complete parser example :

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(Object obj, EventArgs e)
{
string temp = "John|Smith|[email protected],Greg|Henry|[email protected],Kelly|Smith|[email protected]";
string strFirstName = string.Empty;
string strLastName = string.Empty;
string strEmail = string.Empty;

string[] userDetails = temp.Split(',');

foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');

Response.Write(astrElements[0].ToString() + "<br />" );
Response.Write (astrElements[1].ToString() + "<br />" );
Response.Write (astrElements[2].ToString() + "<br />" );
}
}
</script>
<html>
<body>
</body>
</html>

----------------

You can also resplit astrElements on the pipe for the individual FirstName, LastName, Email items.





Mark Rae said:
Steve said:
but i'm only getting the last person in the string.

Hmm - is it possible that each record is overwriting the previous one so that only the last record remains? I don't
think Response.Write would do that, but we're starting to run out of options here!
Also if I use trim(), I'm getting errors when i compile

You will do, because C# is case-sensitive. Try this instead:
string[] userDetails = temp.Trim(',').Split(',');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top