I am new to windows Forms applications...
I am working in VB.NET windows forms and dealing with DataGridView...
I have populated that DataGridView from two Table
Please tell me How to Add , Delete and Edit/Update the records of the DATAGridview back to the DataBase.
I am using SQLSERVER 2008 DataBase
I have two tables 1->CompanyMaster_tbl
in this having Two fields . Cid and CompanyName
,
Cid is the primary key of this table
2->DepartmentMaster_tbl
in this having 4 fields. dtid,dtname,dtphon,dtmail,Cid
.
dtid is the primary key,and Cid is the foreign key.. My data gridview look like this:
Dim adapter As SqlDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'NOTE: I removed the Using statements to ease your tests/understanding, but you shouldn't let the connections opened (or, at least, set connection-release part somewhere)
Dim con As SqlConnection = New SqlConnection() 'SET THE CONNECTION STRING
con.Open()
adapter = New SqlDataAdapter("select c.CompanyName,d.dtName,d.dtPhone,d.dtEmail from CompanyMaster_tbl c join DepartmentMaster_tbl d on c.Cid=d.cId", con)
Dim dt As DataTable = New DataTable()
adapter.Fill(dt) 'Filling dt with the information from the DB
gv.DataSource = dt 'Populating gv with the values in dt
End Sub
in update button i wrote code like this:
Dim dt1 As DataTable = DirectCast(gv.DataSource, DataTable)
adapter.Update(dt1)
but after editing anything in gridview,,i click the update button,,but i am getting error in this row
da.Update(dt1)
Error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
Thanks in advance