[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# add listbox every two values to a single row in SQL

Posted on 2012-08-24
3
Medium Priority
?
767 Views
Last Modified: 2012-08-28
Can someone please help to modify this code so it adds every 2 values ti a row in SQL. There are two columns. Lastname and Username. The data in listbox looks like this:

watts <-last name
jwatts  <- username
johnson <-last name
djohnson <- username
smith <-last name
jsmith <- username

 string connString = ConfigurationManager.ConnectionStrings["AppConnection"].ToString();
            SqlConnection conn = new SqlConnection(connString);
            
            int number = 0;


            try
            {
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Domain Name", "OU=Groups,OU=OU,DC=Domain Name,DC=local");
                GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "Domain Users Group");
                string str;
                int increementer = 0;
                if (grp != null)
                {
                    foreach (Principal p in grp.GetMembers(false))
                    {
                        UserPrincipal theUser = p as UserPrincipal;

                        if (theUser != null)
                        {
                            if(increementer==0)
                            {
                       
                            //  if (theUser.IsAccountLockedOut())
                            str = (theUser.Name).ToString().Split(',')[0];
                            ListBox1.Items.Add(str);
                            ListBox1.Items.Add((theUser.SamAccountName).ToString());
                            SqlCommand comm = new SqlCommand("INSERT INTO [ITUsers] (LastName, Username) VALUES (@LastName, @Username)", conn);
                            conn.Open();
                            comm.Parameters.AddWithValue("@LastName", this.ListBox1.SelectedValue[increementer].ToString());
                            comm.Parameters.AddWithValue("@Username", this.ListBox1.SelectedValue[(increementer)+1].ToString());

                            comm.ExecuteNonQuery();
                            }
                       
                            else
                                if(increementer!=0)
                            {
                            increementer++;
                            //  if (theUser.IsAccountLockedOut())
                            str = (theUser.Name).ToString().Split(',')[0];
                            ListBox1.Items.Add(str);
                            ListBox1.Items.Add((theUser.SamAccountName).ToString());
                            SqlCommand comm = new SqlCommand("INSERT INTO [ITUsers] (LastName, Username) VALUES (@LastName, @Username)", conn);
                            conn.Open();
                            comm.Parameters.AddWithValue("@LastName", this.ListBox1.SelectedValue[increementer].ToString());
                            comm.Parameters.AddWithValue("@Username", this.ListBox1.SelectedValue[(increementer)+1].ToString());
                            }
                             
                        }

                    }
                }
                

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);

            }
            finally
            {
               
                conn.Close();
                SqlConnection.ClearPool(conn);
         
            }

Open in new window

0
Comment
Question by:pauledwardian
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 1000 total points
ID: 38330162
I'm a little unclear on the bigger picture; can you put it in context - give me a higher-level overview of the purpose of the program, and/or what you're trying to achieve & why?

Have you attempted to modify the code yourself yet - are stumped on something in particular?

FYI, I believe firmly in Give a man a fish, and he eats for a day; teach a man to fish, and he eats for a lifetime; plus all of the Experts here are volunteers - even if you pay for use of the site, we do not get paid. Therefore I'm slightly disinclined to answer questions that read like "I need a program that does X, Y and Z. Give me code." I am, however, very happy to help, explain or teach (but not just do it for you).
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38331917
comm.Parameters.AddWithValue("@LastName", str);
 comm.Parameters.AddWithValue("@Username", theUser.SamAccountName.ToString());

Open in new window

0
 

Author Closing Comment

by:pauledwardian
ID: 38342777
Thanks anyways!!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

834 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