D
dennist
can't transfer value from class to form
I have a very simple test application I wrote to learn
how to transfer a value from a class to the form that
called it. I have no problem setting the value in the
class. I have no problem updating the database. But
this isn't really an adonet question, it's a windows form
question.
If I use a public property(code follows), the message
boxes show the right numbers for the ID field, but the
textbox is filled with 0. If I try to use a public
shared property, mID is underlined with the
message 'cannot refer to an instance member of a class
from within a shared method or shared member initializer
without an explicit instance of the class'.
I don't know what to do with that statement. I'm also
afraid it'll carry me far afield without putting the
right number in the textbox. Can somebody tell me how to
get the right number in the textbox from a class. It has
to be from a class, because in the real application the
whole updating of the table occurs in a class.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnBuild As
System.Windows.Forms.Button
Friend WithEvents TextBox1 As
System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.btnBuild = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'btnBuild
'
Me.btnBuild.Location = New System.Drawing.Point
(16, 24)
Me.btnBuild.Name = "btnBuild"
Me.btnBuild.Size = New System.Drawing.Size(72, 24)
Me.btnBuild.TabIndex = 0
Me.btnBuild.Text = "Build"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point
(176, 88)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(96, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.btnBuild)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnBuild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBuild.Click
'Dim strConn as string
Dim strSQL As String
'strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
'Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT * FROM DateType ORDER BY
DateType;"
Dim da As New OleDbDataAdapter(strSQL, cn)
Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
'cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")
Dim dsA As New ds1
da.Fill(dsA, dsA.Tables(0).TableName)
Dim tblDateType As ds1.DateTypeDataTable =
dsA.Tables(0)
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "cxpQW"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
'rowDateType.ID = 11
'rowDateType.ID = Integer.MaxValue
tblDateType.AddDateTypeRow(rowDateType)
'da.InsertCommand = New OleDb.OleDbCommand
("INSERT INTO datetype
(ID,DateType,CreateDate,ChangeDate,Active) values
(?,?,?,?,?)", cn)
da.InsertCommand = New OleDb.OleDbCommand("INSERT
INTO datetype(DateType,CreateDate,ChangeDate,Active)
values (?,?,?,?)", cn)
'INSERT INTO [Order Details] (OrderID, ProductID,
Quantity, UnitPrice) VALUES (?, ?, ?, ?)
'Dim cmdGetIdentity As New OleDbCommand("SELECT
@@IDENTITY", cn)
AddHandler da.RowUpdated, AddressOf OnRowUpDated
'Try
' da.InsertCommand.Parameters.Add("@ID",
OleDb.OleDbType.Integer, 4, "ID")
'Catch er As Exception
' MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
'End Try
da.InsertCommand.Parameters.Add("@DateType",
OleDb.OleDbType.VarChar, 255, "DateType")
da.InsertCommand.Parameters.Add("@CreateDate",
OleDb.OleDbType.Date, 8, "CreateDate")
da.InsertCommand.Parameters.Add("@ChangeDate",
OleDb.OleDbType.Date, 8, "ChangeDate")
da.InsertCommand.Parameters.Add("@Active",
OleDb.OleDbType.Boolean, 2, "Active")
Try
da.Update(dsA, "DateType")
Catch er As System.Exception
'MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
'Finally
' cn.Close()
End Try
Dim dr As DataRow
Dim iRow As Integer
iRow = dsA.Tables(0).Rows.Count - 1
dr = dsA.Tables(0).Rows(iRow)
'dr("ID").
'You now have access to all fields in the last
row through the DataRow object (dr)
cn.Close()
Dim cls1 As New Class1
TextBox1.Text = cls1.gID
End Sub
Friend Sub OnRowUpDated(ByVal sender As Object, ByVal
args As OleDb.OleDbRowUpdatedEventArgs)
'include a variable and a command to retrieve the
'identity value from the access database
'Dim strConn, strSQL As String
'strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
'Dim cn As New OleDbConnection(strConn)
'cn.Open()
Dim int1 As Integer = 0
Dim cmd1 As OleDb.OleDbCommand = New
OleDb.OleDbCommand("SELECT @@IDENTITY", cn)
If args.StatementType = StatementType.Insert Then
'Retrieve the identity value and store it in
the ID column
int1 = CInt(cmd1.ExecuteScalar)
MsgBox(int1)
Dim cls1 As New Class1
cls1.gID = int1
cls1.msg()
'MsgBox(int1)
End If
End Sub
Friend Sub HandleRowUpdated(ByVal sender As Object, _
ByVal e As
OleDbRowUpdatedEventArgs)
Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
cn.Open()
Dim cmdGetIdentity As New OleDbCommand("SELECT
@@IDENTITY", cn)
If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
e.Row("ID") = CType
(cmdGetIdentity.ExecuteScalar, Integer)
e.Row.AcceptChanges()
End If
End Sub
End Class
Public Class Class1
Private mID As Integer
Public Property gID()
Get
Return mID
End Get
Set(ByVal Value)
mID = Value
End Set
End Property
Sub msg()
MsgBox(gID.ToString)
End Sub
End Class
naturally, this statement must be changed each time:
rowDateType.DateType = "cxpQW"
dennist
I have a very simple test application I wrote to learn
how to transfer a value from a class to the form that
called it. I have no problem setting the value in the
class. I have no problem updating the database. But
this isn't really an adonet question, it's a windows form
question.
If I use a public property(code follows), the message
boxes show the right numbers for the ID field, but the
textbox is filled with 0. If I try to use a public
shared property, mID is underlined with the
message 'cannot refer to an instance member of a class
from within a shared method or shared member initializer
without an explicit instance of the class'.
I don't know what to do with that statement. I'm also
afraid it'll carry me far afield without putting the
right number in the textbox. Can somebody tell me how to
get the right number in the textbox from a class. It has
to be from a class, because in the real application the
whole updating of the table occurs in a class.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form
Designer.
InitializeComponent()
'Add any initialization after the
InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnBuild As
System.Windows.Forms.Button
Friend WithEvents TextBox1 As
System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.btnBuild = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'btnBuild
'
Me.btnBuild.Location = New System.Drawing.Point
(16, 24)
Me.btnBuild.Name = "btnBuild"
Me.btnBuild.Size = New System.Drawing.Size(72, 24)
Me.btnBuild.TabIndex = 0
Me.btnBuild.Text = "Build"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point
(176, 88)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(96, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.btnBuild)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnBuild_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnBuild.Click
'Dim strConn as string
Dim strSQL As String
'strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
'Dim cn As New OleDbConnection(strConn)
strSQL = "SELECT * FROM DateType ORDER BY
DateType;"
Dim da As New OleDbDataAdapter(strSQL, cn)
Dim ds As New DataSet
ds.DataSetName = "ds1"
cn.Open()
da.FillSchema(ds, SchemaType.Source, "DateType")
'cn.Close()
ds.WriteXmlSchema("H:\HasbaraNET\ado.net
tests\CodeUpdate\ds.xsd")
Dim dsA As New ds1
da.Fill(dsA, dsA.Tables(0).TableName)
Dim tblDateType As ds1.DateTypeDataTable =
dsA.Tables(0)
Dim rowDateType As ds1.DateTypeRow
rowDateType = tblDateType.NewDateTypeRow
rowDateType.DateType = "cxpQW"
rowDateType.CreateDate = Now
rowDateType.ChangeDate = Now
rowDateType.Active = True
'rowDateType.ID = 11
'rowDateType.ID = Integer.MaxValue
tblDateType.AddDateTypeRow(rowDateType)
'da.InsertCommand = New OleDb.OleDbCommand
("INSERT INTO datetype
(ID,DateType,CreateDate,ChangeDate,Active) values
(?,?,?,?,?)", cn)
da.InsertCommand = New OleDb.OleDbCommand("INSERT
INTO datetype(DateType,CreateDate,ChangeDate,Active)
values (?,?,?,?)", cn)
'INSERT INTO [Order Details] (OrderID, ProductID,
Quantity, UnitPrice) VALUES (?, ?, ?, ?)
'Dim cmdGetIdentity As New OleDbCommand("SELECT
@@IDENTITY", cn)
AddHandler da.RowUpdated, AddressOf OnRowUpDated
'Try
' da.InsertCommand.Parameters.Add("@ID",
OleDb.OleDbType.Integer, 4, "ID")
'Catch er As Exception
' MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
'End Try
da.InsertCommand.Parameters.Add("@DateType",
OleDb.OleDbType.VarChar, 255, "DateType")
da.InsertCommand.Parameters.Add("@CreateDate",
OleDb.OleDbType.Date, 8, "CreateDate")
da.InsertCommand.Parameters.Add("@ChangeDate",
OleDb.OleDbType.Date, 8, "ChangeDate")
da.InsertCommand.Parameters.Add("@Active",
OleDb.OleDbType.Boolean, 2, "Active")
Try
da.Update(dsA, "DateType")
Catch er As System.Exception
'MessageBox.Show("Type = " &
er.GetType.ToString & vbCr & "Message = " & er.Message)
'Finally
' cn.Close()
End Try
Dim dr As DataRow
Dim iRow As Integer
iRow = dsA.Tables(0).Rows.Count - 1
dr = dsA.Tables(0).Rows(iRow)
'dr("ID").
'You now have access to all fields in the last
row through the DataRow object (dr)
cn.Close()
Dim cls1 As New Class1
TextBox1.Text = cls1.gID
End Sub
Friend Sub OnRowUpDated(ByVal sender As Object, ByVal
args As OleDb.OleDbRowUpdatedEventArgs)
'include a variable and a command to retrieve the
'identity value from the access database
'Dim strConn, strSQL As String
'strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
'Dim cn As New OleDbConnection(strConn)
'cn.Open()
Dim int1 As Integer = 0
Dim cmd1 As OleDb.OleDbCommand = New
OleDb.OleDbCommand("SELECT @@IDENTITY", cn)
If args.StatementType = StatementType.Insert Then
'Retrieve the identity value and store it in
the ID column
int1 = CInt(cmd1.ExecuteScalar)
MsgBox(int1)
Dim cls1 As New Class1
cls1.gID = int1
cls1.msg()
'MsgBox(int1)
End If
End Sub
Friend Sub HandleRowUpdated(ByVal sender As Object, _
ByVal e As
OleDbRowUpdatedEventArgs)
Dim strConn, strSQL As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;OLE
DB Services=-4;Data Source=H:\HasbaraNET\ado.net
tests\DateTypes.mdb;"
Dim cn As New OleDbConnection(strConn)
cn.Open()
Dim cmdGetIdentity As New OleDbCommand("SELECT
@@IDENTITY", cn)
If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
e.Row("ID") = CType
(cmdGetIdentity.ExecuteScalar, Integer)
e.Row.AcceptChanges()
End If
End Sub
End Class
Public Class Class1
Private mID As Integer
Public Property gID()
Get
Return mID
End Get
Set(ByVal Value)
mID = Value
End Set
End Property
Sub msg()
MsgBox(gID.ToString)
End Sub
End Class
naturally, this statement must be changed each time:
rowDateType.DateType = "cxpQW"
dennist