Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The data grid in the front end interface looks like below. At least one of the color textbox should have value when the insert button is clicked. And insert a new row if there is data in any one of the color columns. And the table design in the database is like below. The status column refers to the colors in the interface. When the button is clicked, it should do a bulk insert, where it loops through every row and if there is data in the color columns, it should create at least 3 records per row.

ProductName	Category	Green	Yellow	Red
      A	    Not ready	          2015	2017	2018
   B	Ready	                    2016		   2019


Table Design
ProductName	Category	Status	Year
       A	Not ready	1	2015
       A	Not ready	2	2017
       A	Not ready	3	2018
       B	Ready	1	2016
       B	Ready	3	2019


What I have tried:

I tried doing for loop but did not work.
I have not done much with this.
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection())
        {
            connection.ConnectionString = Session[""].ToString();
            foreach (GridViewRow row in wraGrid.Rows)
            {
                string query = "insert into [product] values (@productname,@,@Category,@Status,@Year)";
              SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@productname", row.Cells[""].value ?? DBNull.Value);
            }
        }
    }
Posted
Updated 16-Aug-22 13:14pm
v3
Comments
[no name] 16-Aug-22 16:08pm    
Yes; a loop is required; and some "if's". And without showing some code, that's about as much help as you're probably going to get.
PIEBALDconsult 16-Aug-22 19:19pm    
That's not a bulk load.
What is the DataSource of the Grid? If it's a DataTable, then use the DataTable's DefaultView.
Don't keep creating and throwing away the Command, just set the parameter values.
Your parameters don't seem to match the statement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900