Hello,I am Vanen,from Mauritius Island,new to c# and vs.

I am getting difficulties while saving values from my datagrid to database.
If possible,option for selecting all the rows and option for selecting which rows to save to database from the datagrid.

Can someone guide me?please!
Thanks in advance.

Post some code of your's...

    {
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }

        try
        {
         int i = 0;
         if (dgvfareaudit.Rows[i].Cells[2].Value == DBNull.Value)
            {
              MessageBox.Show("Datagrid is empty!");
            }

            dap.InsertCommand = new SqlCommand("INSERT INTO dbo.fare_table(fromsector,sectorto,farebasis) VALUES('" +
            dgvfareaudit.Rows[i].Cells["fromsector"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["sectorto"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["farebasis"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["owrt"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["currencycode"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["lcf"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["nuc"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["carriercode"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["rule"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["mpm"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["gi"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["frbd"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["svf"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["svt"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["fvf"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["fvt"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["primecode"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["seasonalcode"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["pow"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["pod"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["fptc"].Value.ToString() + "','" +
            dgvfareaudit.Rows[i].Cells["fli"].Value.ToString() + "')",conn);
            //}
            conn.Open();
            //dap.InsertCommand.Connection = conn;
            ////dap.InsertCommand.CommandText = SqlSelect;
            //dap.InsertCommand.CommandTimeout = 0;
            //dap.InsertCommand = new SqlCommand(SqlSelect, conn);
            dap.InsertCommand.ExecuteNonQuery();
            MessageBox.Show("Record Successfully Saved", "Symphony", MessageBoxButtons.OK, MessageBoxIcon.Information);


            conn.Close();



        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }

What do you want to do in general?
if you want to select some specific rows from the datagridview to insert their values in the database you can loop using foreach through just the datagridview selected rows and not all the datagridview rows like:

foreach(DataGridViewRow dgvRow in yourDataGridView.SelectedRows)
{
// TODO: code to insert data in the database
}

Your insert statement specified only three columns within the 'fare_table' while you are inserting many values. The values to be inserted must match the number of columns you specified within the table and if you want to insert more than one value within the same column you should put them together like

INSERT INTO dbo.fare_table(fromsector,sectorto,farebasis) VALUES(value1, value2, value3+value4)

Notice value3 and value 4 have no comma to separate them so there values will be add together in the column farebasis.

Hope this help you in what you want

Thanks,I have found the solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.