Good Morning All
I'm using Visual Studio 2019
I have an issue with a datagridview that I just cannot resolve....after a lot of wasted time!
My datagridview is populated programmatically from an SQL table, and clicking an update button needs to either update the database, or insert new records (I've not looked at this yet!)
When I click the Update button I am getting an error message as shown below. The full code for the form is as below with the error line annotated:
Any help, advise or a point in the right direction would be gratefully received!!
Thanks
Steve
I'm using Visual Studio 2019
I have an issue with a datagridview that I just cannot resolve....after a lot of wasted time!
My datagridview is populated programmatically from an SQL table, and clicking an update button needs to either update the database, or insert new records (I've not looked at this yet!)
When I click the Update button I am getting an error message as shown below. The full code for the form is as below with the error line annotated:
Code:
Imports System.Data.SqlClient
Public Class ProductsServices
Private bindingSource1 As BindingSource
Public UpdateType As String = String.Empty
Private con As SqlConnection
Private cmd As SqlCommand
Private sda As SqlDataAdapter
Private ds As DataSet
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles butCancel.Click
Me.Close()
Main.Show()
End Sub
Private Sub ProductsServices_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
dgvProducts.AutoGenerateColumns = True
Using con As New SqlConnection(Main.DWDataConstr)
Using cmd As New SqlCommand("Select [Prod ID], [Prod Ref code],[Prod description],[Prod Default Price],[Prod Stock Value],[Prod Delivery Cost],[Prod Weight],[Prod Stock Effect],SEQ,VATRate,RentalPeriod,Active from [Products and services] Order By SEQ, [Prod description]")
Using sda As New SqlDataAdapter()
sda.SelectCommand = cmd
cmd.Connection = con
con.Open()
Using ds As New DataSet
sda.Fill(ds)
dgvProducts.DataSource = ds.Tables(0)
End Using
'con.Close()
End Using
End Using
End Using
dgvProducts.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
dgvProducts.BorderStyle = BorderStyle.Fixed3D
dgvProducts.EditMode = DataGridViewEditMode.EditOnEnter
dgvProducts.RowHeadersVisible = False
' Prod ID
dgvProducts.Columns(0).Visible = False
'Prod Ref Code
dgvProducts.Columns(1).Width = 100
dgvProducts.Columns(1).HeaderCell.Value = "Reference Code"
' Prod Description
dgvProducts.Columns(2).Width = 300
dgvProducts.Columns(2).HeaderCell.Value = "Product Description"
' Prod Default Price
dgvProducts.Columns(3).Width = 70
dgvProducts.Columns(3).HeaderCell.Value = "Default Price"
dgvProducts.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvProducts.Columns(3).DefaultCellStyle.Format = "C2"
' Prod Stock Value
dgvProducts.Columns(4).Width = 70
dgvProducts.Columns(4).HeaderCell.Value = "Stock Value"
dgvProducts.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvProducts.Columns(4).DefaultCellStyle.Format = "C2"
' Prod Delivery Cost
dgvProducts.Columns(5).Width = 70
dgvProducts.Columns(5).HeaderCell.Value = "Delivery Cost"
dgvProducts.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvProducts.Columns(5).DefaultCellStyle.Format = "C2"
' Prod Weight
dgvProducts.Columns(6).Width = 50
dgvProducts.Columns(6).HeaderCell.Value = "Weight"
dgvProducts.Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomRight
dgvProducts.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
' Prod Stock Effect
dgvProducts.Columns(7).Width = 50
dgvProducts.Columns(7).HeaderCell.Value = "Stock Effect"
dgvProducts.Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
dgvProducts.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
' Sequence
dgvProducts.Columns(8).Width = 50
dgvProducts.Columns(8).HeaderCell.Value = "SEQ"
dgvProducts.Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
dgvProducts.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
' VAT Rate
dgvProducts.Columns(9).Width = 70
dgvProducts.Columns(9).HeaderCell.Value = "VAT Rate"
dgvProducts.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvProducts.Columns(9).DefaultCellStyle.Format = "0.00\%"
' Rental Period
dgvProducts.Columns(10).Width = 50
dgvProducts.Columns(10).HeaderCell.Value = "Rental Period"
dgvProducts.Columns(10).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
dgvProducts.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
' Active
dgvProducts.Columns(11).Width = 50
dgvProducts.Columns(11).HeaderCell.Value = "Active"
Catch __unusedSqlException1__ As SqlException
MessageBox.Show("To be determined", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
System.Threading.Thread.CurrentThread.Abort()
End Try
End Sub
Private Sub butUpdate_Click(sender As Object, e As EventArgs) Handles butUpdate.Click
Try
Dim table As New DataTable()
bindingSource1 = dgvProducts.DataSource ' ERROR LINE HERE
table = bindingSource1.DataSource
sda.Update(table)
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Any help, advise or a point in the right direction would be gratefully received!!
Thanks
Steve