problems with updating information from 2 tables
Hi everybody!
I have 2 tables in my database : Customers (id_cust, name, surname) and
Orders(id_cust, product_id);
id_cust is the foreign key on table Customers. I want to show these columns in DataGridView:
id_cust | name | surname | product_id
I have problem because I use DataAdapter and I have this SelectCommand:
"Select id_cust,name,surname,product_id from Customers,Orders where Customers.id_cust = Orders.id_cust)"
and I really get everything to my DataGridView but I have problem with Updating data. The wizard can't give me the UpdateCommand, and DeleteCommand, and InsertCommand. Do You know how can I upadate my 2 tables :Customers, Orders when I have everything in one DataGridView. Please help me!
Thank's for any reply!
Alec CohenPosted Dec 13, 2005, 5:39 PM
Hi
hope this helps .. (this is greatly simplified)
I usually pick up the values from the cells in the grid so ...
int intRow = 1;
int intCol = 0;
string strDate = this.dataGrid1.[intRow,intCol].ToString();
string strValue = this.dataGrid1.[intRow,intCol + 1]/ToString();
// pick up any more more data values required
// Build a Sql Statement
strSql = "Insert into tblSystems (Sy_date,Sy_SomeCol1)
+ "values(" + strDate + "," + strValue + ")";
// create then open a connection
SqlConnection cn = new SqlConnection ( .......................);
cn. open()
// create the command
SqlCommand cmd = cn.CreateCommand();
// now insert your new record
cmd.CommandText = strSql;
cmd.ExecuteNonQuery();
If you have to insert or update another table just build another sql statement and use the same method
You will need to tidy this up a bit (especially indentation!) maybe wrap in a transaction
and trap any errors etc.
Hope this helps