Question

I'm trying to save multiple rows from a gridview into sql database. The gridview is manually built in code, thus it is not databind... The reason for this is because this is a grid for parts that has to be added to a quote... Okey so I done the saving part, the only thing is, is that I have to set a foreach to get the data from that row (which I done), but the variables will be set constantly and only getting the last rows data... Can anybody please help me out? This is what I've done:

    conn = new SqlConnection(GetConnectionString());

    string sql = "INSERT INTO CC_ItemsRequested (Item,Quantity, Price, ModelID, DateCreatedOn ) VALUES (@Item, @Quantity, @Price, @ModelID, @DateCreatedOn)";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        int row = GridView1.Rows.Count;

        cmd.Parameters.AddWithValue("@Item", myPart);
        cmd.Parameters.AddWithValue("@Quantity", myQuantity);
        cmd.Parameters.AddWithValue("@Price", myPrice);
        cmd.Parameters.AddWithValue("@ModelID", methodID);
        cmd.Parameters.AddWithValue("@DateCreatedOn", DateTime.Now);

        for (int i = 0; i < row; i++)
        {
            cmd.Parameters["@Item"].Value = myPart;
            cmd.Parameters["@Quantity"].Value = myQuantity;
            cmd.Parameters["@Price"].Value = myPrice;
            cmd.Parameters["@ModelID"].Value = methodID;
        }

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();

This will save the data, but I need to do something so that each row will be saved...This is the foreach that I wrote:

     //foreach (GridViewRow rows in GridView1.Rows)
        //{
        //    if (rows.RowType == DataControlRowType.DataRow)
        //    {
        //        string myPart = rows.Cells[0].Text;
        //        string myQuantity = rows.Cells[1].Text;
        //        string myPrice = rows.Cells[2].Text; 
        //    }

But the problem is, it is not going to get all the rows in the grid's data, it will basically only save the last row then....

Any ideas please? Thanks in advance!

Was it helpful?

Solution

I refactored your code a little bit: the SqlCommand has to be executed for each rows, and the parameters have to be retrieve from the GridView before.

conn.Open();

using(SqlCommand cmd = new SqlCommand(sql, conn))
{
    var now = DateTime.Now;

    foreach (GridViewRow row in GridView1.Rows)
    {
        string myPart = row.Cells[0].Text;
        string myQuantity = row.Cells[1].Text;
        string myPrice = row.Cells[2].Text;
        //... etc

        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@Item", myPart);
        cmd.Parameters.AddWithValue("@Quantity", myQuantity);
        cmd.Parameters.AddWithValue("@Price", myPrice);
        cmd.Parameters.AddWithValue("@ModelID, methodID);
        cmd.Parameters.AddWithValue("@DateCreatedOn, now);

        cmd.ExecuteNonQuery();
    }
}

conn.Close();

I am not sure you want to store Price, Quantity and numeric values as text. You probably want to do some validation first, and do some conversions instead of picking the Text property. What not take the .Value of the cell (if their CellValueType is defined correctly) ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top