1

I've designed two tables (Sale and SaleList),here is a relationship between them (one to many) the sale table is the master and the second is the child, Sale Fields are (Id - SaleNum - CustomerName - Description),SaleList Fields are (Id - SaleId - Num - ItemName - Qut - Unit - SalePrice - Total),Iam adding the rows manually to gridview and passing the item data to it from itemcard table, I want to enter the data in the rows to the database, and you create a button to perform this operation, suppose you add two rows when you press the button to save the data in the database, it transfer only one row out of two rows This is my problem

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        Sale sale = new Sale()
        {
            SaleNum = int.Parse(txtSaleId.Text),
            CustomerName = txtCustomer.Text,
            Description = txtDescription.Text
        };
        db.Sales.InsertOnSubmit(sale);
        db.SubmitChanges();


        for (int i = 0; i < gvSale.DataRowCount; i++)
        {                
            SaleList sList = new SaleList()
            {
                SaleId = int.Parse(gvSale.GetRowCellValue(i,"SaleId").ToString()),
                //ItemId = int.Parse(row.Cells[3].Value.ToString()),
                Num = gvSale.GetRowCellValue(i, "Num").ToString(),
                ItemName = gvSale.GetRowCellValue(i, "ItemName").ToString(),
                Qut = double.Parse(gvSale.GetRowCellValue(i, "Qut").ToString()),
                Unit = gvSale.GetRowCellValue(i, "Unit").ToString(),
                SalePrice = double.Parse(gvSale.GetRowCellValue(i, "SalePrice").ToString()),
                Total = 0,
            };
            db.SaleLists.InsertOnSubmit(sList);
            db.SubmitChanges();
        }            
    }

The problem is inserting only the data in the first row to database.

  • Pro-tips for posting: this is a help site, so readers know you need assistance. Thus, adding "please help me" (or the txtspk equivalent) may be read as a form of begging that will just earn you downvotes. Keep it succinct please! – halfer Jan 15 '17 at 14:35
  • I am not sure you have explained your problem very well. Would you show us the contents of your database table before and after the code operation, and explain to us what you expected? – halfer Jan 15 '17 at 14:37
  • I am using sql database,ia have created 2 tables : – user7421595 Jan 15 '17 at 14:41
  • ^ Please add further information **in the question** by using the [edit function](https://stackoverflow.com/posts/41662062/edit), thanks. – halfer Jan 15 '17 at 14:43
  • Check gvSale.DataRowCount, I'm sure it's 0. You have to use gvSale.RowCount. – Marko Juvančič Jan 18 '17 at 09:54
  • I do not want to save the data directly, what I want is to add data separately from the database or indirectly, I have analyzed the problem by focusing on the first row in gridview and it worked Successfully. thank you for you response. my regards – user7421595 Jan 18 '17 at 18:30

0 Answers0