how to update data in a datagridview to a database

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
i have 2 tables- invoice[ino,date,name,address,total,tax1,tax2] and invoicedetails [ino,product,quantity,rate,amount]. i have a invoicedetails datagridview.
i want to update the data into a database. i tried the following method, but it is not getting added to the database. please help me with it. thanks in advance.
<pre class="prettyprint lang-vb Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
If InvoicedetailsDataGridView.Rows.Count > 0 Then
Me.AddInvoice()
Me.UpdateDatabase()
Me.Close()
Else
MessageBox.Show("You must add at least one line item " _
& "before saving the invoice.", "Invoice Not Saved")
End If
End Sub
Private Sub AddInvoice()
Try
Dim invoiceRow As invoicestockDataSet.invoiceRow = Me.InvoicestockDataSet.invoice.NewinvoiceRow

invoiceRow._date = DateDateTimePicker.Value
invoiceRow.name = CStr(NameTextBox.Text)
invoiceRow.address = CStr(AddressTextBox.Text)
invoiceRow.total = productTotal
invoiceRow.tax1 = tax1
invoiceRow.tax2 = tax2

Me.InvoicestockDataSet.invoice.AddinvoiceRow(invoiceRow)
Catch ex As Exception
MessageBox.Show(ex.Message, ex.GetType.ToString)
End Try
End Sub
Private Sub UpdateDatabase()
Try
Update the Invoices table.
InvoiceTableAdapter.Update(Me.InvoicestockDataSet.invoice)

Get the final invoice ID.
Dim invoiceID As Integer = CInt(InvoiceTableAdapter.GetLastIdentityValue)

Set the final invoice ID for all line items.
For Each row As invoicestockDataSet.invoicedetailsRow In Me.InvoicestockDataSet.invoicedetails
row.ino = invoiceID
Next

Update the InvoiceLineItems table.

InvoicedetailsTableAdapter.Update(Me.InvoicestockDataSet.invoicedetails)
MsgBox("Invoice Saved")
Catch ex As SqlException
MessageBox.Show("Database error # " & ex.Number & "; " & ex.Message, ex.GetType.ToString)
End Try
End Sub[/code]
<br/><hr class="sig shehzi

View the full article
 
Back
Top