Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am new to windows app.
I am trying to update table in DB and successful in that.
But i am not getting how to insert new added rows in DGV to DB.

In my code i have a button on click of that i am updating table in DB. following is the code.
C#
private void btnUpdate_Click(object sender, EventArgs e)
{
    Update();
}

and
C#
private void Update()
{
    try
    {      
        adapter.Update(userTable);  // here i am able to update. But i needed a code that it also catch newly added rows and insert them in DB
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        conn.Close();
    }
}

Please suggest me the way. help me with code to insert and update data in Oracle DB from windows app. Thank you..
Posted
Updated 26-Sep-13 3:51am
v2
Comments
Prasad Khandekar 26-Sep-13 9:55am    
Have a look at http://khanrahim.wordpress.com/2010/04/10/insert-update-delete-with-datagridview-control-in-c-windows-application/.

1 solution

Hi master, (that doesn't sound right though)

When you implement Delete, Insert and Update operations you must have a DataAdapter and define its Delete, Insert and Update commands. You could use this adapter against your datasource and call the adapters' update method. That is the quickest and easiest way to implement these kind of operations.
C#
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM table1", conn);
 adapter.InsertCommand = new SqlCommand("INSERT INTO table1 VALUES(@param1, @param2)", conn);
 adapter.InsertCommand.Parameters.Add("@param1",...);

Just do the same to the Update and Delete commands. Then, Create a DataSet and Fill it using the adapter and make it as a datasource of your datagrid...
C#
DataSet ds = new DataSet();
 adapter.Fill(ds);
 DataGrid1.DataSource = ds;

When you want to send your changes to the database you can just call the adapter.update method...
C#
adapter.Update(ds);

A nice article explaining this in detail :

http://www.codeguru.com/csharp/.net/net_data/datagrid/article.php/c13041

Hope this helps.
 
Share this answer
 
Comments
master77777 27-Sep-13 3:18am    
Hi Rick i started with that only.

I have 3 columns in a table ROW_ID,EFFORT,EMP_ID. Where ROW_ID i have to take from SEQUENCE. I take sequence in a integer from database and that is not visible in datagridview.
Now i have to save that also in DB along with DGV data. that creating problem. If you could help me regrding that.
Rick van Woudenberg 30-Sep-13 4:25am    
Hi master,

Could you give me the code on how you get the ROW_ID values and how you store the rest in the datagridview. You may have to add a column. Please bear in mind that the database is updated from the underlying datasource and not from the datagridview itself. So adding something to the datagradview may to always be visible in the database.
master77777 2-Oct-13 8:53am    
I am processed in that. I am updating adapter on row_leave event of gridview.

and adding new row on the run as

DataRow newCustomersRow = userTable.NewRow();

newCustomersRow["ROW_ID"] = row_id;
newCustomersRow["PAR_ROW_ID"] = GridVariablesproperty.SR;

newCustomersRow["CREATED"] = dt;
newCustomersRow["EFFORT"] = Convert.ToInt32(GridVariablesproperty.eff_in_mins);
newCustomersRow["EMP_ID"] = Convert.ToInt32(GridVariablesproperty.eff_emp_id);
newCustomersRow["NOTES"] = GridVariablesproperty.eff_notes;

userTable.Rows.Add(newCustomersRow);

But i am getting error because row_leave is firing again on single row leave..
and try to update the same record in data table.

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