Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Visual Basic
Tip/Trick

DataSet to String to DataSet

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
10 Nov 2011CPOL 21.9K   2   1
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)


Written By
Philippines Philippines
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralApart from giving your Dataset a name I'm not sure what tip ... Pin
Simon_Whale11-Nov-11 5:12
Simon_Whale11-Nov-11 5:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.