none
multiple listboxes and values to a database.

    Question

  • I have 4 listboxes which all contain data. My application inserts 4 textbox values to the listboxes at the same time. What I now want to do is insert all the values from all the listboxes at the same time into the database. So all the values from the listboxes that have the same indexes need to be inserted in the same table at the same time. I managed to get the values from one listbox into the database but can't get the others to. Im using a stored proc to pass values from the listboxes to the sql database when the web app iterates through the listboxes

      try
            {
                System.Configuration.Configuration rootWebConfig =
                       System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
                System.Configuration.ConnectionStringSettings connString;
                connString =
                       rootWebConfig.ConnectionStrings.ConnectionStrings["IntercoConnectionString"];/*Name of connection in web.config file */
                string conn1 = connString.ConnectionString;
                SqlConnection conn = new SqlConnection(conn1);
                conn.Open();
                
                int value = 0;
                for (int i = 0; i < ListBox1.Items.Count; i++)
                {
                    value = 0;
                    SqlCommand cmd = new SqlCommand("quoteInsert", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    value += Convert.ToInt32(ListBox1.Items[i].Text);
                    cmd.Parameters.AddWithValue("Height",value);
                    cmd.ExecuteNonQuery();
                }
    
                int value2 = 0;
                for (int w = 0; w < ListBox2.Items.Count; w++)
                {
                    value2 = 0;
                    SqlCommand cmd1 = new SqlCommand("quoteInsert", conn);
                    cmd1.CommandType = CommandType.StoredProcedure;
                    value2 += Convert.ToInt32(ListBox2.Items[w].Text);
                    cmd1.Parameters.AddWithValue("Breadth", value2);
                    cmd1.ExecuteNonQuery();
                }
                conn.Close();
    
    
            }
            catch (SqlException connError)
            {
                MessageBox.Show("Connection error, unable to connect to database", "Connection Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


    Monday, June 4, 2012 1:53 PM

Answers

  • I actually wasn't thinking, as the indexes will be the same while I iterate through one listbox I can just pull the values from the others and specify the same counter. 

     int valueWidth = 0;
                int valueHeight = 0;
                int valueLength = 0;
                int valueParcels = 1;
                int valueActMass = 0;
                int valueQuoteID = ID;
                for (int i = 0; i < widthList.Items.Count; i++)
                {
                    valueQuoteID = 0 + ID + 1;
    
                    valueWidth = 0;
                    
                    valueHeight = 0;
    
                    valueLength = 0;
    
                    valueActMass = 0;
                    SqlCommand cmd = new SqlCommand("quoteInsert", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    valueWidth += Convert.ToInt32(widthList.Items[i].Text);
                    valueHeight += Convert.ToInt32(heightList.Items[i].Text);
                    valueLength += Convert.ToInt32(lengthList.Items[i].Text);
                    valueActMass += Convert.ToInt32(weightList.Items[i].Text);
                    cmd.Parameters.AddWithValue("Breadth", valueWidth);
                    cmd.Parameters.AddWithValue("Height", valueHeight);
                    cmd.Parameters.AddWithValue("Length", valueLength);
                    cmd.Parameters.AddWithValue("ActMass", valueActMass);
                    cmd.Parameters.AddWithValue("NoParcels", valueParcels);
                    cmd.Parameters.AddWithValue("QuoteID", valueQuoteID);
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
               


    Tuesday, June 5, 2012 7:18 AM

All replies

  • I would like to see the stored procedure as well because there are 2 arguments,

    plus the table structure too.

    As your insertion logic seems to be same I suggest you to use a method to do the insertion.

    Pass the listbox to the method.  so there is no need for the repeated logic and you can find the problem.

    Insert(ListBox lbx, string column)

    Call like this:

    Insert(listBox1, "Height");
    Insert(listBox2, "Breadth");


    Resolving n Evolving in C# (http://jeanpaulva.com)


    Monday, June 4, 2012 5:41 PM
  • I actually wasn't thinking, as the indexes will be the same while I iterate through one listbox I can just pull the values from the others and specify the same counter. 

     int valueWidth = 0;
                int valueHeight = 0;
                int valueLength = 0;
                int valueParcels = 1;
                int valueActMass = 0;
                int valueQuoteID = ID;
                for (int i = 0; i < widthList.Items.Count; i++)
                {
                    valueQuoteID = 0 + ID + 1;
    
                    valueWidth = 0;
                    
                    valueHeight = 0;
    
                    valueLength = 0;
    
                    valueActMass = 0;
                    SqlCommand cmd = new SqlCommand("quoteInsert", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    valueWidth += Convert.ToInt32(widthList.Items[i].Text);
                    valueHeight += Convert.ToInt32(heightList.Items[i].Text);
                    valueLength += Convert.ToInt32(lengthList.Items[i].Text);
                    valueActMass += Convert.ToInt32(weightList.Items[i].Text);
                    cmd.Parameters.AddWithValue("Breadth", valueWidth);
                    cmd.Parameters.AddWithValue("Height", valueHeight);
                    cmd.Parameters.AddWithValue("Length", valueLength);
                    cmd.Parameters.AddWithValue("ActMass", valueActMass);
                    cmd.Parameters.AddWithValue("NoParcels", valueParcels);
                    cmd.Parameters.AddWithValue("QuoteID", valueQuoteID);
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
               


    Tuesday, June 5, 2012 7:18 AM