nolc
June 12th, 2002, 11:35 AM
Hey,
Does anybody know how to Update a Database from a Dataset? I keep on erroring out at "drCurrent = tblParty.Rows.Find(Me.txtPartyID.Text)" within the Button1_Click. The error messages says "Table does not have a primary key." Is there any way around this besides adding a primery key?
MY GOAL IS.....to be able to hit a SQL Server Database, pass a few parameters to a stored procedure, get the dataset, update, the dataset & FINALLY update the database with the dataset!!!
If someone could help me out...I would be the happiest man alive! ;) Thanks again.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Private strConn As String = "server=WNT;database=sql;uid=sa;pwd=;"
Private SQLconn As SqlConnection
Private da As SqlDataAdapter
Private ds, dset As DataSet
Private cmd As SqlCommand
Private drow As DataRow
Private tblParty As DataTable
Private drCurrent As DataRow
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
ds = GetInfo(txtPartyID.Text, txtOrderNumber.Text)
tblParty = ds.Tables("Table")
MessageBox.Show(tblParty.Rows.Count.ToString)
drow = tblParty.Rows(0)
Me.txtPartyID.Text = drow("fldPartyID")
Me.txtOrderNumber.Text = drow("fldOrderNumber")
Me.txtPartyType.Text = drow("fldPartyType")
Me.txtPartyFName.Text = drow("fldPartyFName")
Me.txtPartyLName.Text = drow("fldPartyLName")
Me.txtPartySSN.Text = drow("fldPartySSN")
End Sub
Public Function GetInfo(ByVal PartyID As String, ByVal OrdNum As String) As DataSet
' Create Instance of Connection and Command Object
Dim myConnection As SqlConnection = New SqlConnection(strConn)
Dim myCommand As SqlCommand = New SqlCommand("spPARTYINFO", myConnection)
' Mark the Command
myCommand.CommandType = CommandType.StoredProcedure
' Add Parameters
Dim p1 As SqlParameter = New SqlParameter("@nvchPartyID", SqlDbType.VarChar, 50)
p1.Value = PartyID
myCommand.Parameters.Add(p1)
Dim p2 As SqlParameter = New SqlParameter("@nvchOrdNum", SqlDbType.VarChar, 50)
p2.Value = OrdNum
myCommand.Parameters.Add(p2)
' Execute the command
myConnection.Open()
da = New SqlDataAdapter(myCommand)
''' Create an instance of a DataSet, and retrieve data from the Authors table.
ds = New DataSet("Table")
'da.FillSchema(ds, SchemaType.Source, "Table")
da.Fill(ds, "Table")
Return ds
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''*****************
''BEGIN EDIT CODE
'Dim tblParty As DataTable = ds.Tables("Table")
'Dim tblContact As DataTable = ds.Tables("tblContact")
drCurrent = tblParty.Rows.Find(Me.txtPartyID.Text)
drCurrent.BeginEdit()
drCurrent("tblPartyFName") = Me.txtPartyFName.Text '"352" & drCurrent("phone").ToString.Substring(3)
drCurrent.EndEdit()
MsgBox("Record edited successfully")
'END EDIT CODE
'*****************
'BEGIN SEND CHANGES TO SQL SERVER
Dim objCommandBuilder As New SqlCommandBuilder(da)
da.Update(ds, "Table")
MsgBox("SQL Server updated successfully" & Chr(13) & "Check Server explorer to see changes")
'' END SEND CHANGES TO SQL SERVER
''*****************
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' drow = ds.Tables("Table").Rows(0)
MessageBox.Show(drow("fldPartyFName")) '("fldPartyFName"), drow("fldPartyLName"), drow("fldPartySSN"))
End Sub
End Class
Does anybody know how to Update a Database from a Dataset? I keep on erroring out at "drCurrent = tblParty.Rows.Find(Me.txtPartyID.Text)" within the Button1_Click. The error messages says "Table does not have a primary key." Is there any way around this besides adding a primery key?
MY GOAL IS.....to be able to hit a SQL Server Database, pass a few parameters to a stored procedure, get the dataset, update, the dataset & FINALLY update the database with the dataset!!!
If someone could help me out...I would be the happiest man alive! ;) Thanks again.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Private strConn As String = "server=WNT;database=sql;uid=sa;pwd=;"
Private SQLconn As SqlConnection
Private da As SqlDataAdapter
Private ds, dset As DataSet
Private cmd As SqlCommand
Private drow As DataRow
Private tblParty As DataTable
Private drCurrent As DataRow
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
ds = GetInfo(txtPartyID.Text, txtOrderNumber.Text)
tblParty = ds.Tables("Table")
MessageBox.Show(tblParty.Rows.Count.ToString)
drow = tblParty.Rows(0)
Me.txtPartyID.Text = drow("fldPartyID")
Me.txtOrderNumber.Text = drow("fldOrderNumber")
Me.txtPartyType.Text = drow("fldPartyType")
Me.txtPartyFName.Text = drow("fldPartyFName")
Me.txtPartyLName.Text = drow("fldPartyLName")
Me.txtPartySSN.Text = drow("fldPartySSN")
End Sub
Public Function GetInfo(ByVal PartyID As String, ByVal OrdNum As String) As DataSet
' Create Instance of Connection and Command Object
Dim myConnection As SqlConnection = New SqlConnection(strConn)
Dim myCommand As SqlCommand = New SqlCommand("spPARTYINFO", myConnection)
' Mark the Command
myCommand.CommandType = CommandType.StoredProcedure
' Add Parameters
Dim p1 As SqlParameter = New SqlParameter("@nvchPartyID", SqlDbType.VarChar, 50)
p1.Value = PartyID
myCommand.Parameters.Add(p1)
Dim p2 As SqlParameter = New SqlParameter("@nvchOrdNum", SqlDbType.VarChar, 50)
p2.Value = OrdNum
myCommand.Parameters.Add(p2)
' Execute the command
myConnection.Open()
da = New SqlDataAdapter(myCommand)
''' Create an instance of a DataSet, and retrieve data from the Authors table.
ds = New DataSet("Table")
'da.FillSchema(ds, SchemaType.Source, "Table")
da.Fill(ds, "Table")
Return ds
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''*****************
''BEGIN EDIT CODE
'Dim tblParty As DataTable = ds.Tables("Table")
'Dim tblContact As DataTable = ds.Tables("tblContact")
drCurrent = tblParty.Rows.Find(Me.txtPartyID.Text)
drCurrent.BeginEdit()
drCurrent("tblPartyFName") = Me.txtPartyFName.Text '"352" & drCurrent("phone").ToString.Substring(3)
drCurrent.EndEdit()
MsgBox("Record edited successfully")
'END EDIT CODE
'*****************
'BEGIN SEND CHANGES TO SQL SERVER
Dim objCommandBuilder As New SqlCommandBuilder(da)
da.Update(ds, "Table")
MsgBox("SQL Server updated successfully" & Chr(13) & "Check Server explorer to see changes")
'' END SEND CHANGES TO SQL SERVER
''*****************
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' drow = ds.Tables("Table").Rows(0)
MessageBox.Show(drow("fldPartyFName")) '("fldPartyFName"), drow("fldPartyLName"), drow("fldPartySSN"))
End Sub
End Class