Im just a couple of hours into learning this VB.NET stuff and ive hit a bit of a snag. Im trying to store a new record back into my database. I know this is a pretty elementry question but I looked at my book and some online sites and I cant see what im doing thats any different. I am crapping out at the line:
dscmd.Update(ds, "Customers")
Below is my code, the above line can be found near the bottom:
dscmd.Update(ds, "Customers")
Below is my code, the above line can be found near the bottom:
Code:
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strConnection As String = "Data Source=localhost;Initial Catalog=Northwind;User Id=someuser;Password=somepass; Trusted_Connection=yes"
Dim cn As SqlConnection = New SqlConnection(strConnection)
Dim strSelect As String = "SELECT ContactName, City FROM Customers"
Dim dscmd As New SqlDataAdapter(strSelect, cn)
Dim ds As New DataSet()
Dim dt As DataTable
SNIPPED OUT VB.NET GENERATED CODE
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
cn.Open()
Catch openException As SqlException
MsgBox("Exception Caught: " & openException.Message)
End Try
Try
dscmd.Fill(ds, "Customers")
Catch fillException As System.Exception
MsgBox("Exception Caught: " & fillException.Message)
End Try
cn.Close()
dt = ds.Tables("Customers")
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
cn.Open()
Dim newRow As DataRow = dt.NewRow()
newRow("ContactName") = Me.txtName
newRow("City") = Me.txtCity
dt.Rows.Add(newRow)
Try
dscmd.Update(ds, "Customers") PROBLEM LINE
Catch invalid As InvalidOperationException
MsgBox("Exception Caught: " & invalid.Message)
End Try
cn.Close()
End Sub
End Class
Last edited by a moderator: