Solved

Manipulate arrays, and placing the resualts to SQL

Posted on 2006-11-15
4
229 Views
Last Modified: 2010-04-16
I've got the following code:

Console.WriteLine(page);
            int itemId = 1;
            int parentId = 0;
            int crtPos = 0;

            string allEntries = page.Replace("\n", string.Empty);
            allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t", string.Empty);

            string[] values = allEntries.Split('[');
            string crtValue = string.Empty;
            string query = string.Empty;
            string initialCrtValue = string.Empty;
            string paveWithIdAndParentId = string.Empty;
            string pageWithIdAndParentId = "[";

            ArrayList parents = new ArrayList();

            for ( int i = 0 ; i < values.Length ; i++ )
            {
                crtValue = values[i];

                if ( crtValue.Length == 0 )
                {
                    continue;
                }
                initialCrtValue = "[" + crtValue;

                itemId ++;
                parentId = itemId;

                crtPos = crtValue.IndexOf(']');
                if ( crtPos == -1 )
                {
                    if ( parents.Count > 0 )
                        parentId = (int)parents[parents.Count - 1];
                    parents.Add(itemId);
                }
                else
                {
                    crtValue = crtValue.Remove(crtPos, 1);
                    crtPos = crtValue.IndexOf(']');
                    if ( parents.Count > 0 )
                    {
                        parentId = (int)parents[parents.Count - 1];
                    }
                    while ( crtPos != -1 )
                    {
                        if ( parents.Count > 0 )
                        {
                            parents.RemoveAt(parents.Count - 1);
                        }
                        crtValue = crtValue.Remove(crtPos, 1);
                        crtPos = crtValue.IndexOf(']');
                    }
                }

                //query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES (%s%d,%d)", values, id, parentId);
                query = string.Format("({0}{1},{2})", crtValue, itemId, parentId);
                Console.WriteLine(query);
                // execute query string formated above.
                int firstComaPos = initialCrtValue.IndexOf(',');
                if ( firstComaPos == -1 ) continue;
                initialCrtValue = initialCrtValue.Insert(firstComaPos + 1, string.Format("{0},{1},", itemId, parentId));
                pageWithIdAndParentId += (initialCrtValue);





Do you think you can help me with getting another problem I have, its pretty much the same, but I want to capture all the value and place it in the query string (insert)

So if I had the following

['home.gif', 'Homepage', 'http://www.google.com', 'main','this is a test', '1', '2'

will get converted to:

query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES (%a,%b,%c,%d,%e,%f,%g)", icon, title, url, target, desc, itemid, parentid);


Dont know if that made sense?
0
Comment
Question by:mousemat24
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Agarici earned 500 total points
ID: 17945419
string someString = @"['home.gif', 'Homepage', 'http://www.google.com', 'main','this is a test', '1', '2'";
string[] values = someString.Split(',');
if( values.Length != 7 )
    // see how to handle this case

query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu_URL,menu_Target ,menu_description, Id, ParentId) VALUES ({0},{1},{2},{3},{4},{5},{6})", values[0],values[1],values[2],values[3],values[4],values[5],values[6]);

A.
0
 

Author Comment

by:mousemat24
ID: 17945688
Will you code work with the following string, i..e will it go through all each element of the array:

string page = "[[null,'dasdas','_self','sada',[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada',[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada'],[null,'dasdas','_self','sada']]],[null,'dasdas','_self','sada']]";

Thanks Agarici
0
 
LVL 11

Expert Comment

by:Agarici
ID: 17945696
certenly not

but it will work on 'crtValue' variable from the code you posted

A.
0
 

Author Comment

by:mousemat24
ID: 17961294
Thanks Agarici  for your help again, man you .NET developers are good!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now