user1617943
user1617943

Reputation: 11

Convert Dataset into String in VB

I am having an error with compiling the codes. It says that dataset cant be converted to string.. Please help me.. I have done this by using SQL Stored procedure.. I have to get the employee name saved in master table to retrieve the ID and then ID should retrieve the leave details from leave details table.

 Public Shared Function GetEmpID(ByVal EmpName As String) As DataSet
    Dim db As Database = DatabaseFactory.CreateDatabase(HttpContext.Current.Session("CompanyID").ToString) ' TO Get Database Connection
    Dim dbCommand As DbCommand = db.GetStoredProcCommand("spRetrieveEmpID") ' Stored Procedure to Execute
    db.AddInParameter(dbCommand, "EmployeeName", DbType.String, EmpName) ' Parameter for Stored Procedure
    Return db.ExecuteDataSet(dbCommand) ' Execute Stored Procedure
End Function

Public Shared Function EmpLeaveDetails(ByVal EmployeeID As String) As DataSet
    Dim db As Database = DatabaseFactory.CreateDatabase(HttpContext.Current.Session("CompanyID").ToString) ' TO Get Database Connection
    Dim dbCommand As DbCommand = db.GetStoredProcCommand("spHRLeaveEntitlement") ' Stored Procedure to Execute
    db.AddInParameter(dbCommand, "EmployeeID", DbType.String, EmployeeID) ' Parameter for Stored Procedure
    Return db.ExecuteDataSet(dbCommand) ' Execute Stored Procedure
End Function

And this is my leave.aspx.vb codes

 Private Sub ddlEmpName_SelectedIndexChanged(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles ddlEmpName.SelectedIndexChanged

    EmployeeID = TESTING.GetEmpID(ddlEmpName.Text)

    If ds.Tables(0).Rows.Count > 0 Then
        dgLeaveDetails.DataSource = ds
        dgLeaveDetails.DataBind()
    End If

    ds1 = TESTING.EmpLeaveDetails(EmployeeID)
    dgLeaveDetails.DataSource = ds1

    ddlEmpName.SelectedIndex = -1
    dgLeaveDetails.Rebind()

End Sub

Upvotes: 0

Views: 3176

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

You are setting EmployeeID, like this:

EmployeeID = TESTING.GetEmpID(ddlEmpName.Text)

The GetEmpID method returns a DataSet object. Therefore, EmployeeID references a DataSet, rather than equaling a single ID value. Later, you try to pass EmployeeID to the EmpLeaveDetails method like this:

ds1 = TESTING.EmpLeaveDetails(EmployeeID)

However, the EmpLeaveDetails method takes a String as a parameter. Therefore, you are trying to pass a DataSet into a method that expects a String. Since DataSet objects cannot be automatically converted to the String type, the compiler gives the error.

To fix it, you need to either have the GetEmpID return a string, or else you need to retrieve the single string value from the DataSet before passing it to the EmpLeaveDetails method.

Upvotes: 1

SSS
SSS

Reputation: 5403

The line

EmployeeID = TESTING.GetEmpID(ddlEmpName.Text)

looks like the problem to me.

You should use:

Dim dst As Dataset = TESTING.GetEmpID(ddlEmpName.Text)
EmployeeID = CStr(dst.Tables(0).Rows(0).Item("EmployeeID"))

Upvotes: 1

Related Questions