在VB中将数据集转换为字符串

时间:2013-03-08 01:47:35

标签: asp.net vb.net

编译代码时遇到错误。它说数据集不能转换为字符串..请帮助我..我已经通过使用SQL存储过程完成了这个..我必须在主表中保存员工姓名以检索ID然后ID应该检索保留详细信息来自详细信息表。

 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

这是我的leave.aspx.vb代码

 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

2 个答案:

答案 0 :(得分:1)

该行

EmployeeID = TESTING.GetEmpID(ddlEmpName.Text)

看起来像我的问题。

您应该使用:

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

答案 1 :(得分:1)

您正在设置EmployeeID,如下所示:

EmployeeID = TESTING.GetEmpID(ddlEmpName.Text)

GetEmpID方法返回DataSet个对象。因此,EmployeeID引用DataSet,而不是等于单个ID值。稍后,您尝试将EmployeeID传递给EmpLeaveDetails方法,如下所示:

ds1 = TESTING.EmpLeaveDetails(EmployeeID)

但是,EmpLeaveDetails方法会将String作为参数。因此,您尝试将DataSet传递给期望String的方法。由于DataSet对象无法自动转换为String类型,编译器会给出错误。

要修复它,您需要让GetEmpID返回一个字符串,否则您需要从DataSet检索单个字符串值,然后再将其传递给EmpLeaveDetails方法