Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Database as follows;
 Date         datetime
 Session      int
 Course       varchar(20)
Faculty_ Code varchar(20)

but i want save the data grid view data in to the data base.
C#
private void BtnSave_Click(object sender, EventArgs e)
        {
            Save Details();
        }

 private void SaveDetails()
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("date", typeof(string));
                dt.Columns.Add("sess", typeof(int));
                dt.Columns.Add("crs", typeof(string));
                dt.Columns.Add("Fac", typeof(string));
                for (int i = 0; i < datagridView.RowCount; i++)
                {
                    for (int j = 2; j < datagridView.ColumnCount; j++)
                    {
                        if (datagridView[j, i].Value != null)
                  
                        {
dt.Rows.Add(datagridView[0, i].Value.ToString(), datagridView[1, i].Value.ToString(), datagridView[j, i].Value.ToString(), datagridView.Columns[j].HeaderText.ToString());
                        }
                    }
                }
                dataGridView1.DataSource = dt;
            }
            catch (Exception Ex1)
            {
                MessageBox.Show(Ex1.ToString(), "Error", MessageBoxButtons.OK);
            }
        }

i save the data grid view data into the another data grid view data using data table.

i want to save the data grid view data in to the data base.

how to do.please help me.
Posted
Updated 11-Jan-13 18:51pm
v2
Comments
Kuthuparakkal 12-Jan-13 0:43am    
Any error ?
Sandeep Mewara 12-Jan-13 0:52am    
Ok, now what is the issue that you face when you try to do what you want?

1 solution

I assume you are developing this application in winforms.
What you need to do is take every row from dataGridView store it in your table, something like below,
OR
you can do this in a very efficient way by passing parameters which i'd prefer you to do, but the important point is you got the idea for storing this in database
C#
for (int i = 0; i < dataGridView.Rows.Count-1; i++)
            {
                string query = "insert into tableName(Date ,Session ,Course Faculty_ Code)" + "values('"+Convert.ToDateTime(dataGridView.Rows[i].Cells[0].Value.ToString())+"', '"+int.Parse(dataGridView.Rows[i].Cells[1].Value.ToString()) +"', '"+dataGridView.Rows[i].Cells[2].Value.ToString() +"', '"+dataGridView.Rows[i].Cells[3].Value.ToString()+"'";

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query,yourConnection);
cmd.ExecuteNonQuery();
}

Good Luck
 
Share this answer
 

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