locked
Multiple textboxes/One table column RRS feed

  • Question

  • User135480177 posted

    I have an application I am working on redeveloping. It was developed so that users would enter data into a textbox and if there was more than one value they would separate by commas. I modified the database so there would not be any comma separated data and I created three additional textboxes for the user to enter the data. On button click I need  to create a new row for each of the three additional textboxes if they have a value. 

    Tuesday, February 6, 2018 8:38 PM

All replies

  • User1400794712 posted

    Hi scottdg,

    On button click I need  to create a new row for each of the three additional textboxes if they have a value. 

    Are you confused of this? If so, we can add multiple to Database like this:

    protected void Button1_Click(object sender, EventArgs e)
    {
        string queryString =
            "INSERT Months(month,year) "
                + "VALUES(@month,2018);";
        string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            if (!String.IsNullOrEmpty(TextBox1.Text))
            {
                SqlCommand command = new SqlCommand(queryString, conn);
                command.Parameters.AddWithValue("@month", TextBox1.Text);
                command.ExecuteNonQuery();
            }
            if (!String.IsNullOrEmpty(TextBox2.Text))
            {
                SqlCommand command = new SqlCommand(queryString, conn);
                command.Parameters.AddWithValue("@month", TextBox2.Text);
                command.ExecuteNonQuery();
            }
            if (!String.IsNullOrEmpty(TextBox3.Text))
            {
                SqlCommand command = new SqlCommand(queryString, conn);
                command.Parameters.AddWithValue("@month", TextBox3.Text);
                command.ExecuteNonQuery();
            }
            conn.Close();
        }
                
    }

    If you're confused by other things, please feel free to post back and explain your problem in detail.

    Best Regards,

    Daisy

    Wednesday, February 7, 2018 9:08 AM
  • User135480177 posted

    Thanks - I guess I was overthinking this a bit. 

    I ended up handling this on the stored procedure end with multiple inserts into the same table with the different text box values. 

    Wednesday, February 7, 2018 4:54 PM