Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

DataSet to String to DataSet

5.00/5 (1 vote)
10 Nov 2011CPOL 22.2K  
Converting DataSet to String to DataSet

It took me three days searching the Net on how to make my DataSet(s) String type. I also asked for help but in some ways, could not make the code run. I then tried this simple yet very handy trick. If you have more than 10 reports and each one has its own DataSet, you would want to avoid doing long Select Cases, If statements, right!

A simple trick I have found is this:

  • Var_Ds is the string representing the DataSet name
  • da is the Adapter
  • myDs will be my new DataSet
VB
Dim Var_ds as String = DsVarName & ".xsd"
Dim da As SqlDataAdapter = New SqlDataAdapter(comm)
Dim myDS As New DataSet(Var_ds)

Below is a working code (part of my working program):

VB
'my table name
Dim myRPT As String = "RPT_" & RM.rptID
'my report name (I'm using Crystal)
Dim xRpt As String = RM.rptID & "_rpt.rpt"
Dim oRpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument
oRpt.Load(xRpt)

'RM.procName is my Stored Proc Name
Dim comm As SqlCommand = New SqlCommand(RM.procName, conn)
comm.CommandTimeout = 0
comm.CommandType = CommandType.StoredProcedure

Dim objParam As SqlParameter
objParam = comm.Parameters.Add("@transdate", SqlDbType.NVarChar, 10)
objParam.Direction = ParameterDirection.Input
objParam.Value = txndate

Dim objParam1 As SqlParameter
objParam1 = comm.Parameters.Add("@currency_cd", SqlDbType.NVarChar, 3)
objParam1.Direction = ParameterDirection.Input
objParam1.Value = curr_cd
comm.ExecuteNonQuery()

'And here's my DataSet name into String
'Why i did this, i need to recreate this for my 52 Crystal Reports
'and instead doing such long coding.. i did this.

Dim Rptds As String = RM.rptID & "_ds.xsd"
Dim da As SqlDataAdapter = New SqlDataAdapter(comm)
Dim myDS As New DataSet(Rptds) '< ----- String to DataSet

da.Fill(myDS, myRPT)
oRpt.SetDataSource(myDS)
ReportMain.CrystalReportViewer2.ReportSource = oRpt

Call CrystalReportProperties_()
conn.Close()

I hope this helps. Any comments/suggestions are welcome!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)