passing a dataset...where's my data?
-
I am trying to separate my data access code from my form code. I have a created a data access class containing a function to accept an EmployeeID and return a dataset containing just information for that employee. When I try to display the employee information on the form I am getting the column name rather than the value contained in the column. What am I doing wrong? Form Code
Private Sub frmMaint_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Try dsSpcInstrFRM = m_DAL.SpecialInstructions(strEmpIdFromChooseFrm) dtSpcInstrFRM = dsSpcInstrFRM.Tables("SpcInstructions") lblSpcInstructions.Text = dtSpcInstrFRM.Columns("SpcInstr").ToString Catch ex As Exception ‘Error Routine End Try End Sub
Data Access CodeFriend Function SpecialInstructions(ByVal m_strEmpID As String) As Dataset Dim sqlRiskDR As SqlDataReader Dim SpecialInstructionsDS As New DataSet Dim SpcInstrDT As New DataTable Dim SpcInstrDR As DataRow Try sqlRiskCN = New SqlConnection(My.Settings.SQLRiskCNString) sqlRiskCN.Open() 'Use Stored Procedure strSql = "GetSpecialInstructions " & "'" & m_strEmpID & " '" sqlCM = New SqlCommand(strSql, sqlRiskCN) sqlRiskDR = sqlCM.ExecuteReader() SpecialInstructionsDS.DataSetName = "SpecialInstructions" SpecialInstructionsDS.Tables.Add(SpcInstrDT) SpcInstrDT.TableName = "SpcInstructions" SpcInstrDT.Columns.Add("EmpID").Unique = False SpcInstrDT.Columns.Add("SpcInstr") Do While sqlRiskDR.Read() SpcInstrDR = SpcInstrDT.NewRow SpcInstrDR.Item("EmpID") = sqlRiskDR.Item("EmpID").ToString SpcInstrDR.Item("SpcInstr") = sqlRiskDR.Item("EmergInstructions").ToString.TrimEnd SpcInstrDT.Rows.Add(SpcInstrDR) Loop sqlRiskDR.Close() sqlRiskCN.Close() Catch ex As Exception ‘Error Routine End Try Return SpecialInstructionsDS End Function
-
I am trying to separate my data access code from my form code. I have a created a data access class containing a function to accept an EmployeeID and return a dataset containing just information for that employee. When I try to display the employee information on the form I am getting the column name rather than the value contained in the column. What am I doing wrong? Form Code
Private Sub frmMaint_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Try dsSpcInstrFRM = m_DAL.SpecialInstructions(strEmpIdFromChooseFrm) dtSpcInstrFRM = dsSpcInstrFRM.Tables("SpcInstructions") lblSpcInstructions.Text = dtSpcInstrFRM.Columns("SpcInstr").ToString Catch ex As Exception ‘Error Routine End Try End Sub
Data Access CodeFriend Function SpecialInstructions(ByVal m_strEmpID As String) As Dataset Dim sqlRiskDR As SqlDataReader Dim SpecialInstructionsDS As New DataSet Dim SpcInstrDT As New DataTable Dim SpcInstrDR As DataRow Try sqlRiskCN = New SqlConnection(My.Settings.SQLRiskCNString) sqlRiskCN.Open() 'Use Stored Procedure strSql = "GetSpecialInstructions " & "'" & m_strEmpID & " '" sqlCM = New SqlCommand(strSql, sqlRiskCN) sqlRiskDR = sqlCM.ExecuteReader() SpecialInstructionsDS.DataSetName = "SpecialInstructions" SpecialInstructionsDS.Tables.Add(SpcInstrDT) SpcInstrDT.TableName = "SpcInstructions" SpcInstrDT.Columns.Add("EmpID").Unique = False SpcInstrDT.Columns.Add("SpcInstr") Do While sqlRiskDR.Read() SpcInstrDR = SpcInstrDT.NewRow SpcInstrDR.Item("EmpID") = sqlRiskDR.Item("EmpID").ToString SpcInstrDR.Item("SpcInstr") = sqlRiskDR.Item("EmergInstructions").ToString.TrimEnd SpcInstrDT.Rows.Add(SpcInstrDR) Loop sqlRiskDR.Close() sqlRiskCN.Close() Catch ex As Exception ‘Error Routine End Try Return SpecialInstructionsDS End Function
-
Because you are reading the value from the column information in the data set, not from the data rows. Use the Rows property. --- b { font-weight: normal; }
-
Using the rows property makes sense, but now it brings up the question of how I actually retrieve the data from the dataset. As you can probably tell I am new at this. What code do I need to add to my form to get this to work? Thanks!
lblSpcInstructions.Text = dtSpcInstrFRM.Items("SpcInstr").ToString Go here for more info "People who never make mistakes, never do anything." My Blog
-
lblSpcInstructions.Text = dtSpcInstrFRM.Items("SpcInstr").ToString Go here for more info "People who never make mistakes, never do anything." My Blog