Hi Kalpesh,
Thank you for your interset. Here is a summary (and then the code)
Here is the latest of what I have found.
If I assign a literal when I define the variable then the DataAcces
class sqlCommand works and returns the data.
\\
Friend Shared _fkJob As String = "18713191-e116-4d92-8cc2-c2d4e8c34fdb"
//
If I use a comboBox to assign the value to the variable then the
DataAccess class sqlCommand does not work. The value is assigned before
the attempt to fill the dataAdapter
1.) Friend Shared _fkJob As String
2.) User makes selection in ComboBox and SelectedValue become, for
example "18713191-e116-4d92-8cc2-c2d4e8c34fdb"
3.) Verify Selected value = "18713191-e116-4d92-8cc2-c2d4e8c34fdb"
4.) Verify _fkJob = "18713191-e116-4d92-8cc2-c2d4e8c34fdb"
5.) Me._fkJob = Me.cboSelectJob.SelectedValue.ToString
In other words the code only works if the value is assigned during
variable definition.
Here is "f050SqOO.vb" code (includes a class that extends combobox so
it will accept nulls)
\\
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlDataAdapter
Imports System.Configuration.ConfigurationSettings
Public Class f050SqOO
Inherits System.Windows.Forms.Form
#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
Me.StartPosition = FormStartPosition.CenterScreen
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.
Public WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Protected Friend WithEvents cboSelectJob As NComboBox
Friend WithEvents btnLoad As System.Windows.Forms.Button
Friend WithEvents btnReset As System.Windows.Forms.Button
Friend WithEvents lblfkJob As System.Windows.Forms.Label
Friend WithEvents btnVerifySelectedValue As System.Windows.Forms.Button
Friend WithEvents btnVerifyVariable As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(f050SqOO))
Me.btnLoad = New System.Windows.Forms.Button
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.cboSelectJob = New CLIP.NComboBox
Me.btnReset = New System.Windows.Forms.Button
Me.lblfkJob = New System.Windows.Forms.Label
Me.btnVerifySelectedValue = New System.Windows.Forms.Button
Me.btnVerifyVariable = New System.Windows.Forms.Button
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'btnLoad
'
Me.btnLoad.Location = New System.Drawing.Point(528, 8)
Me.btnLoad.Name = "btnLoad"
Me.btnLoad.Size = New System.Drawing.Size(72, 23)
Me.btnLoad.TabIndex = 2
Me.btnLoad.Text = "Load"
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 88)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(592, 296)
Me.DataGrid1.TabIndex = 30
'
'cboSelectJob
'
Me.cboSelectJob.Location = New System.Drawing.Point(8, 8)
Me.cboSelectJob.Name = "cboSelectJob"
Me.cboSelectJob.NSelectedValue = Nothing
Me.cboSelectJob.Size = New System.Drawing.Size(248, 21)
Me.cboSelectJob.TabIndex = 29
'
'btnReset
'
Me.btnReset.Location = New System.Drawing.Point(528, 40)
Me.btnReset.Name = "btnReset"
Me.btnReset.Size = New System.Drawing.Size(72, 23)
Me.btnReset.TabIndex = 33
Me.btnReset.Text = "Reset"
'
'lblfkJob
'
Me.lblfkJob.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblfkJob.ForeColor = System.Drawing.SystemColors.GrayText
Me.lblfkJob.Location = New System.Drawing.Point(8, 40)
Me.lblfkJob.Name = "lblfkJob"
Me.lblfkJob.Size = New System.Drawing.Size(248, 24)
Me.lblfkJob.TabIndex = 37
Me.lblfkJob.Tag = ""
'
'btnVerifySelectedValue
'
Me.btnVerifySelectedValue.Location = New System.Drawing.Point(264, 8)
Me.btnVerifySelectedValue.Name = "btnVerifySelectedValue"
Me.btnVerifySelectedValue.Size = New System.Drawing.Size(128, 23)
Me.btnVerifySelectedValue.TabIndex = 38
Me.btnVerifySelectedValue.Text = "Verify Selected Value"
'
'btnVerifyVariable
'
Me.btnVerifyVariable.Location = New System.Drawing.Point(264, 40)
Me.btnVerifyVariable.Name = "btnVerifyVariable"
Me.btnVerifyVariable.Size = New System.Drawing.Size(128, 23)
Me.btnVerifyVariable.TabIndex = 39
Me.btnVerifyVariable.Text = "Verify Variable"
'
'f050SqOO
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(616, 398)
Me.Controls.Add(Me.btnVerifyVariable)
Me.Controls.Add(Me.btnVerifySelectedValue)
Me.Controls.Add(Me.lblfkJob)
Me.Controls.Add(Me.btnReset)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.cboSelectJob)
Me.Controls.Add(Me.btnLoad)
Me.Icon = CType(resources.GetObject("$this.Icon"),
System.Drawing.Icon)
Me.Name = "f050SqOO"
Me.Text = "f050SqOO"
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
#Region " Variables "
Private DAL As New CLIP.DataAccess
Private _dataSet1 As CLIP.dsTables
Private _dt As DataTable
Private _bStillLoadingcboSelectJob As Boolean = True
'The code only work if the value is assigned during definition
Friend Shared _fkJob As String '=
"18713191-e116-4d92-8cc2-c2d4e8c34fdb"
#End Region
#Region " Buttons - [ btnLoad | btnReset ] "
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLoad.Click
Try
DAL.da050OperFrm.Fill(_dataSet1, "tbl050Oper")
Catch ex As Exception
MessageBox.Show("Exception Message: " & vbCrLf & ex.Message)
MessageBox.Show("Exception Source: " & vbCrLf & ex.Source)
MessageBox.Show("Exception StackTrace: " & vbCrLf & ex.StackTrace)
End Try
'datagrid
_dt = _dataSet1.tbl050Oper
Me.DataGrid1.DataSource = _dt
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnReset.Click
'clear the datagrid
Me.DataGrid1.DataSource = Nothing
Me.DataGrid1.TableStyles.Clear()
Me.lblfkJob.Text = ""
Call LoadTheForm()
If Me.cboSelectJob.Text.Length > 0 Then
Me._fkJob = Me.cboSelectJob.SelectedValue.ToString
End If
End Sub
#End Region
#Region " Form_Load "
Private Sub f050SqOO_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me._dataSet1 = New CLIP.dsTables
Call LoadTheForm()
Me.cboSelectJob.SelectedValue = DBNull.Value
End Sub
Private Sub LoadTheForm()
Call LoadCboSelectJob()
End Sub
#End Region
#Region " Data code - [ LoadCboSelectJob ] "
Private Sub LoadCboSelectJob()
DAL.da010SoO_Cbo.Fill(_dataSet1, "tbl010Job")
'cboSelectJob
Dim dm1 As String = "tbl010Job.JobDescription"
Dim vm1 As String = "tbl010Job.pkJobId"
Me.cboSelectJob.DataSource = _dataSet1
Me.cboSelectJob.DisplayMember = dm1
Me.cboSelectJob.ValueMember = vm1
_bStillLoadingcboSelectJob = False
End Sub
#End Region
#Region " Capture Changes - [ cboSelectJob ] "
Private Sub cboSelectJob_SelectedValueChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles cboSelectJob.SelectedValueChanged
'If _bStillLoadingcboSelectJob Then
' 'Do Nothing
'Else
' Me._fkJob = Me.cboSelectJob.SelectedValue.ToString
' Me.lblfkJob.Text = _fkJob
'End If
End Sub
#End Region
Private Sub btnVerifySelectedValue_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnVerifySelectedValue.Click
Me._fkJob = Me.cboSelectJob.SelectedValue.ToString
MessageBox.Show("SelectedValue: " &
Me.cboSelectJob.SelectedValue.ToString)
End Sub
Private Sub btnVerifyVariable_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnVerifyVariable.Click
Me.lblfkJob.Text = _fkJob
MessageBox.Show("Variable: " & Me._fkJob)
End Sub
End Class
Public Class NComboBox
Inherits System.Windows.Forms.ComboBox
Public Property NSelectedValue() As Object
Get
Return SelectedValue
End Get
Set(ByVal Value As Object)
SelectedValue = Value
If (Value Is DBNull.Value And SelectedIndex <> -1) Then
SelectedValue = Value
End If
End Set
End Property
End Class
//
Here is "DataAccess.vb"
\\
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Configuration
Public Class DataAccess
Friend sqlConn As New SqlConnection
Friend da050OperFrm As New SqlDataAdapter
Friend da010SoO_Cbo As New SqlDataAdapter
Private cmd050Oper_sel As New SqlCommand
Private cmd010SoO_Sel As New SqlCommand
'[Constructor]
Public Sub New()
MyBase.New()
Call InitializeComponent()
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
'=== [Connection String] ========================
Dim connectionString As String
connectionString =
ConfigurationSettings.AppSettings("ConnectionString")
Me.sqlConn.ConnectionString = connectionString
da050OperFrm.SelectCommand = cmd050Oper_sel
da010SoO_Cbo.SelectCommand = cmd010SoO_Sel
With cmd050Oper_sel
.CommandType = CommandType.StoredProcedure
.CommandText = "usp_050Oper_sel"
.Connection = sqlConn
With cmd050Oper_sel.Parameters
.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte),
"", DataRowVersion.Current, Nothing))
'Filter... WHERE (sqfkJob = @sqfkJob)
.Add("@fkJob", SqlDbType.Text).Value = f050SqOO._fkJob
End With
End With
With cmd010SoO_Sel
.CommandType = CommandType.StoredProcedure
.CommandText = "usp_SoO_Cbo"
.Connection = sqlConn
With cmd010SoO_Sel.Parameters
.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte),
"", DataRowVersion.Current, Nothing))
End With
End With
End Sub
End Class
//
Here is "dsTables.vb" (dataset class)
\\
Public Class dsTables
Inherits DataSet
'[DataTables]
Friend tbl010Job As New DataTable
Friend tbl050Oper As New DataTable
'[Constructor]
Public Sub New()
MyBase.New()
Call InitializeComponent()
End Sub
'[Initialize class component]
Private Sub InitializeComponent()
'[Add Tables to the DataSet]
Me.tbl010Job = Me.Tables.Add("tbl010Job")
Me.tbl050Oper = Me.Tables.Add("tbl050Oper")
'[Add Columns to the DataTables]
' tbl010Job [5-5-7-5-1]
Dim pkJobId As DataColumn = Me.tbl010Job.Columns.Add("pkJobId",
GetType(String))
pkJobId.MaxLength = 36
pkJobId.AllowDBNull = False
Dim JobNumber As DataColumn = Me.tbl010Job.Columns.Add("JobNumber",
GetType(String))
JobNumber.MaxLength = 7
JobNumber.AllowDBNull = False
Dim CustomerName As DataColumn =
Me.tbl010Job.Columns.Add("CustomerName", GetType(String))
CustomerName.MaxLength = 35
CustomerName.AllowDBNull = False
Dim JobDescription As DataColumn =
Me.tbl010Job.Columns.Add("JobDescription", GetType(String))
JobDescription.MaxLength = 75
JobDescription.AllowDBNull = False
Dim ReferenceJobNumber As DataColumn =
Me.tbl010Job.Columns.Add("ReferenceJobNumber", GetType(String))
ReferenceJobNumber.MaxLength = 7
ReferenceJobNumber.AllowDBNull = True
Dim ShopPsi As DataColumn = Me.tbl010Job.Columns.Add("ShopPsi",
GetType(Byte))
ShopPsi.AllowDBNull = True
Dim SystemVoltage As DataColumn =
Me.tbl010Job.Columns.Add("SystemVoltage", GetType(Int32))
SystemVoltage.AllowDBNull = True
Dim SystemPhase As DataColumn = Me.tbl010Job.Columns.Add("SystemPhase",
GetType(Int32))
SystemPhase.AllowDBNull = True
Dim SystemAmps As DataColumn = Me.tbl010Job.Columns.Add("SystemAmps",
GetType(String))
SystemAmps.MaxLength = 3
SystemAmps.AllowDBNull = True
Dim HasCircuitBreakersYN As DataColumn =
Me.tbl010Job.Columns.Add("HasCircuitBreakersYN", GetType(Boolean))
HasCircuitBreakersYN.AllowDBNull = False
Dim AllProcessesAddedYN As DataColumn =
Me.tbl010Job.Columns.Add("AllProcessesAddedYN", GetType(Boolean))
AllProcessesAddedYN.AllowDBNull = False
Dim AllTasksAddedYN As DataColumn =
Me.tbl010Job.Columns.Add("AllTasksAddedYN", GetType(Boolean))
AllTasksAddedYN.AllowDBNull = False
Dim AllProcessesConfiguredYN As DataColumn =
Me.tbl010Job.Columns.Add("AllProcessesConfiguredYN", GetType(Boolean))
AllProcessesConfiguredYN.AllowDBNull = False
Dim ApprovedYN As DataColumn = Me.tbl010Job.Columns.Add("ApprovedYN",
GetType(Boolean))
ApprovedYN.AllowDBNull = False
Dim CompletedYN As DataColumn = Me.tbl010Job.Columns.Add("CompletedYN",
GetType(Boolean))
CompletedYN.AllowDBNull = False
Dim IsMasterYN As DataColumn = Me.tbl010Job.Columns.Add("IsMasterYN",
GetType(Boolean))
IsMasterYN.AllowDBNull = False
Dim Archive As DataColumn = Me.tbl010Job.Columns.Add("Archive",
GetType(Boolean))
Archive.AllowDBNull = False
Dim jcUserNote As DataColumn = Me.tbl010Job.Columns.Add("jcUserNote",
GetType(String))
jcUserNote.MaxLength = 100
jcUserNote.AllowDBNull = False
Dim jcCreatedOn As DataColumn = Me.tbl010Job.Columns.Add("jcCreatedOn",
GetType(DateTime))
jcCreatedOn.AllowDBNull = False
Dim jcCreatedBy As DataColumn = Me.tbl010Job.Columns.Add("jcCreatedBy",
GetType(String))
jcCreatedBy.MaxLength = 25
jcCreatedBy.AllowDBNull = False
Dim jcEditedOn As DataColumn = Me.tbl010Job.Columns.Add("jcEditedOn",
GetType(DateTime))
jcEditedOn.AllowDBNull = False
Dim jcEditedBy As DataColumn = Me.tbl010Job.Columns.Add("jcEditedBy",
GetType(String))
jcEditedBy.MaxLength = 25
jcEditedBy.AllowDBNull = False
Dim jcrowversion As DataColumn =
Me.tbl010Job.Columns.Add("jcrowversion", GetType(Byte()))
jcrowversion.AllowDBNull = True
' tbl050Oper
Dim pkOperId As DataColumn = Me.tbl050Oper.Columns.Add("pkOperId",
GetType(String))
pkOperId.MaxLength = 36
pkOperId.AllowDBNull = False
Dim sqfkJob As DataColumn = Me.tbl050Oper.Columns.Add("sqfkJob",
GetType(String))
sqfkJob.MaxLength = 36
sqfkJob.AllowDBNull = False
Dim sqComponentDescription As DataColumn =
Me.tbl050Oper.Columns.Add("sqComponentDescription", GetType(String))
sqComponentDescription.MaxLength = 50
sqComponentDescription.AllowDBNull = False
Dim sqAction As DataColumn = Me.tbl050Oper.Columns.Add("sqAction",
GetType(String))
sqAction.MaxLength = 50
sqAction.AllowDBNull = True
Dim sqDuration As DataColumn = Me.tbl050Oper.Columns.Add("sqDuration",
GetType(Decimal))
sqDuration.AllowDBNull = True
Dim sqSooOrder As DataColumn = Me.tbl050Oper.Columns.Add("sqSooOrder",
GetType(Decimal))
sqSooOrder.AllowDBNull = True
Dim sqSooPrecedent As DataColumn =
Me.tbl050Oper.Columns.Add("sqSooPrecedent", GetType(Byte))
sqSooPrecedent.AllowDBNull = True
Dim sqSooDelay As DataColumn = Me.tbl050Oper.Columns.Add("sqSooDelay",
GetType(String))
sqSooDelay.MaxLength = 255
sqSooDelay.AllowDBNull = True
Dim sqSooNote As DataColumn = Me.tbl050Oper.Columns.Add("sqSooNote",
GetType(String))
sqSooNote.MaxLength = 255
sqSooNote.AllowDBNull = True
Dim sqCreatedOn As DataColumn =
Me.tbl050Oper.Columns.Add("sqCreatedOn", GetType(DateTime))
sqCreatedOn.AllowDBNull = False
Dim sqCreatedBy As DataColumn =
Me.tbl050Oper.Columns.Add("sqCreatedBy", GetType(String))
sqCreatedBy.MaxLength = 25
sqCreatedBy.AllowDBNull = False
Dim sqEditedOn As DataColumn = Me.tbl050Oper.Columns.Add("sqEditedOn",
GetType(DateTime))
sqEditedOn.AllowDBNull = False
Dim sqEditedBy As DataColumn = Me.tbl050Oper.Columns.Add("sqEditedBy",
GetType(String))
sqEditedBy.MaxLength = 25
sqEditedBy.AllowDBNull = False
Dim sqrowversion As DataColumn =
Me.tbl050Oper.Columns.Add("sqrowversion", GetType(Byte()))
sqrowversion.AllowDBNull = True
'[Additional Column properties for primary keys] [4-12]
pkJobId.ReadOnly = True 'Not AutoIncrement
pkOperId.ReadOnly = True 'Not AutoIncrement
'[Simple PK Constraints - (one line)] [16]
tbl010Job.PrimaryKey = New DataColumn() {tbl010Job.Columns("pkJobID")}
tbl050Oper.PrimaryKey = New DataColumn()
{tbl050Oper.Columns("pkOperId")}
End Sub
End Class
//
Thank you,
dbuchanan