assign dataset row value to function

itortu
itortu used Ask the Experts™
on
i got just one more error in my program (well that is what i think)

i have this function:

Public Function GetStateByGeo(ByVal strGeo$) As DataSet

then i try to assign the dataset value to the function like this:

            If (Not IsNothing(ds)) Then
                If (ds.Tables(0).Rows.Count > 0) Then
                    GetStateByGeo = ds.Tables(0).Rows(0).ToString
                End If
            End If

but i get error:

Value of type 'System.Data.DataRow' cannot be converted to 'System.Data.DataSet'.      

how can the data set (recordset) can be assigned to the function ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008
Commented:
Try this:

Public Function GetStateByGeo(ByVal strGeo As String) As Strinng
   If Not ds Is Nothing AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
      Return ds.Tables(0).Rows(0).ToString
    End If
    Return ""
End Function

Bob
You should create a new datarow in the function.  Set it equal to the ds row you select and then add it to the dataset you wish to return.

Does that make sense?
Do you want to return one row of the table or the whole table in the return dataset?
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
than yo for asking, i want to return one row fo the table

Author

Commented:
i tried your suggestion this way:

    Public Function GetStateByGeo(ByVal strGeo$) As DataSet

        Dim intGeoState As Integer
        Dim strSQL As String = ""
        Dim strFlag As String = ""
        Dim ds As DataSet = Nothing
        dim dr as DataRow

        Try

            intGeoState = CInt(Mid(strGeo, 1, 2))

            strFlag = GetTableFlag("LOCSTATE")

            strSQL = "SELECT * FROM LOCSTATE" & strFlag & " WHERE locgeostate = " & intGeoState

            ds = m_objSQLServerSQLDATA.GetDataSet(strSQL, CommandType.Text, 0)

            If (Not IsNothing(ds)) AndAlso ds.Tables(0).Rows.Count > 0 Then
                Return ds.Tables(0).Rows(0).ToString
            End If

        Catch ex As Exception

        Finally
            ds = Nothing
        End Try
        Return ds
    End Function


this is how the function gets called from another function:

ds = o_data.GetStateByGeo(strGeo)

now i get the error:

Value of type 'String' cannot be converted to 'System.Data.DataSet'.      

Implement divinewind80's solution by ....

            If (Not IsNothing(ds)) Then
                If (ds.Tables(0).Rows.Count > 0) Then
                    dim rtnDS as Dataset
                   rtnDS = ds.Clone()
                   rtnDS.Tables(0).Rows.Add(ds.Tables(0).Rows(0).ItemArray)
                    GetStateByGeo = rtnDS
                End If
            End If

Author

Commented:
sorry this line

  Public Function GetStateByGeo(ByVal strGeo$) As DataSet

is actually
  Public Function GetStateByGeo(ByVal strGeo$) As string

the error:

Value of type 'String' cannot be converted to 'System.Data.DataSet'.      

is given now by this part:

ds = o_data.GetStateByGeo(strGeo)

Most Valuable Expert 2012
Top Expert 2008

Commented:
Me.TextBox1.Text = o_Data.GetStateByGeo(strGeo)

Bob

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial