2

My table contains 10 columns. I need to insert a list using c#.
I have stored the details of multiple members, for each count its has to insert the consecutive details in the same row.

  if (members.Count >= 1)

    {
         foreach (Members myList in members)
             {                             
            Command.Parameters.Add("first", SqlDbType.VarChar).Value = myList.first;
            Command.Parameters.Add("last", SqlDbType.VarChar).Value = myList.last;
            Command.Parameters.Add("age", SqlDbType.VarChar).Value = myList.age;
             }
    }

Example : for count=1 the table looks like "fName1","lName1",21

for count=2 the table looks like "fName1","lName1",21,"fname2","lName2",21

please help on this.

4
  • can you show the full code you are using? Commented Sep 17, 2012 at 5:25
  • from your example count=2 do you mean it should insert into col1...col6 and for count=3 it will be col1...col9? If that is true it's a bad design. Each member should be on separate rows Commented Sep 17, 2012 at 8:16
  • @user1671639 give us a pastebin of the code you are using, if you want a good answer. Commented Sep 18, 2012 at 3:15
  • @user1671639 :: Can you post a sample list? I mean to say how the list looks before reaching the foreach loop portion. and based on that list give us how should the tabel will look like. Commented Sep 18, 2012 at 3:47

1 Answer 1

1

The coding style looks ambiguous. Your foreach loop runs for - 'Members' in members. It makes hard to understand what are trying to do. Let me suggest you to refactor your code and let the class name be 'Member'. You can put members in db with ADO.Net (there are other ways too) as follows -

       using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = connection.CreateCommand())
            {
                //select just schema of the table.
                command.CommandText = "select * from members where 1=2;";
                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    using (SqlCommandBuilder builder = new SqlCommandBuilder(adapter))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            foreach (Member item in memebers)
                            {
                                DataRow row = dt.NewRow();

                                row.SetField<string>("", item.FirstName);
                                row.SetField<string>("", item.LastName);
                                row.SetField<int>("", item.Age);
                                //
                                // number of SetField should be equal to number of selected columns.
                                //
                                dt.Rows.Add(row);
                            }
                            adapter.Update(dt);
                        }                         
                    }   
                }
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.