Showing posts with label dataset. Show all posts
Showing posts with label dataset. Show all posts

Friday, March 23, 2012

How to reference a dataset passed to script

Hi, I have a dataset created via the Execute SQL Statement control and wish to pass it into a script control that within the vb.net script I wish to perform different functions to it.

The main goal is to take the dataset loop through it and record values into an array then take that array and loop through that so I can send an email containing the information within the array.

Can anyone help?

If inside a script component the common method is to use row

So you can do row.xxx where xxx is the field name... then using row.next row etc you can loop and such.

|||Ok, how do we reference the dataset? I appreciate your answer Dave but its very vague.

So I have the execute SQL statement create a dataset named "dataset1" using an ado.net object type.

Then within the script component how to we initialize, and work with "dataset1"? I wish to loop through "dataset1" and perform various functions on it. Sure the row access sounds great for the looping part but what of the other parts?|||Use a foreach loop to open that ADO recordset and loop through it. There are plenty of examples in this forum.|||I know that method and that won't work for what i'm trying to do, or at least i don't think so.

All I want to know is how to reference a dataset from an execute sql statement within a script component. I wish to reference this dataset within the script component and loop through and create an array with specific information. Then I want to use the array as loop through that to create a body of an email. So PLEASE answer the question of ->>>>>

How do I reference a dataset after it being populated via an Execute SQL Statement?

<<<<<--
I tried
dim results as dataset
dim reader as datatablereader
reader = results.tables(0).createdatareaders
do while reader.read
execute statements
loop

I get the error that reader is referenced to no table.|||

I misunderstood your initial question, I thought you needed basic access. I was thinking you used the execute sql command component to create the data set. So if you execute the sql, and then have the output of that component connect to a script component you can just use the Row call I mentioned, because it automatically ties to the "input" dataset from the sql execute component.

|||Here is my code. I have OBJ_NULL_SET as the result set object from the execute sql components output.

Public Sub Main()
'Assume success

Try
Dim results As DataSet

results = CType(ReadVariable("OBJ_NULL_SET"), DataSet)
For Each tbl As DataTable In results.Tables
System.Windows.Forms.MessageBox.Show(tbl.TableName)
Next

Catch ex As Exception
Dts.Events.FireError(-1, "N/A", ex.ToString(), "", 0)
End Try
Dts.TaskResult = Dts.Results.Success

End Sub

Private Function ReadVariable(ByVal varName As String) As Object
Dim result As Object

Try
Dim vars As Variables
Dts.VariableDispenser.LockForRead(varName)
Dts.VariableDispenser.GetVariables(vars)
Try
result = vars(varName).Value
Catch ex As Exception
Throw ex
Finally
vars.Unlock()
End Try
Catch ex As Exception
Throw ex
End Try

Return result
End Function

When the msgbox pops up it says Table with nothing else. It does not display the name of the dataset so i'm sure that i've done something wrong where the dataset is not being referenced properly.|||

You are already referencing the dataset by iterating through the DataTables. The first table's name so happens to be "Table", and that's what the message box call shows.

So, for the only DataTable of the DataSet, iterate through the rows using standard ADO.NET. For example, call this function right after you're first MsgBox() call, and it will display the column names and stringify the data. You're refencing the data from the Execute SQL task at this point, and what is done beyond that is up to your imagination.

Private Sub DisplayTable(ByVal tbl As DataTable)

Dim dr As DataRow, dc As DataColumn

Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()

For colNum As Integer = 0 To tbl.Columns.Count - 1

dc = tbl.Columns(colNum)

sb.Append(dc.ColumnName).Append(",")

Next

Dts.Events.FireInformation(1, "Columns", sb.ToString(), "", 0, True)

For rowNum As Integer = 0 To tbl.Rows.Count - 1

sb.Remove(0, sb.Length)

dr = tbl.Rows(rowNum)

For itemOffset As Integer = 0 To dr.ItemArray.Length - 1

sb.Append(dr.ItemArray(itemOffset)).Append(",")

Next

Dts.Events.FireInformation(1, "Data", sb.ToString(), "", 0, True)

Next

End Sub

sql

Wednesday, March 7, 2012

how to read from sql data set

Code Snippet

Dim ds As New DataSet

Dim adp As New SqlDataAdapter("Select MaxID,ValueName from tblSettings where ID=1", cn)

adp.Fill(ds)

If IsDBNull(ds.Tables(0).Columns("MaxID")) Then

GetMaxID = 1

Else

GetMaxID = ds.Tables(0).Columns("MaxID")

End If

the ds.Tables(0).Columns("MaxID") showing a value like this. ( Please tell how to read from sql data set)

{System.Data.DataColumn}

AllowDBNull: True

AutoIncrement: False

AutoIncrementSeed: 0

AutoIncrementStep: 1

Caption: "MAxid"

ColumnMapping: Element {1}

ColumnName: "MAxid"

Container: Nothing

DataType: {Name = "Decimal" FullName = "System.Decimal"}

DateTimeMode: UnspecifiedLocal {3}

DefaultValue: {System.DBNull}

DesignMode: False

Expression: ""

ExtendedProperties: Count = 0

MaxLength: -1

Namespace: ""

Ordinal: 0

Prefix: ""

ReadOnly: False

Site: Nothing

Table: {System.Data.DataTable}

Unique: False

Replace your SQL statement with this:

"Select isNull(MaxID, 1) As GetMaxID, ValueName from tblSettings where ID=1"

No need to check null value in your VB code.

How to read a single row in a strongly types dataset?

I'm using strongly typed datasets in my first ASP.NET 2.0 web application. I come from ASP Classic, not an earlier version of .NET, and feel like I'm in another world. I'm slowly getting my head around datasets, but one thing I can't find any information on is how to read the data in a single record?

I'm not talking about for next loops, or accessing row information as a repeater or other control is filled. I'm talking about reading the data returned by a method that returns a table containing a single record. This is what I have:

Dim BookAdapter As New BooksTableAdapters.BooksBBTableAdapter
Dim organizations As Books.BooksBBDataTable
Dim organization As Books.BooksBBRow

organizations = BookAdapter.GetDataByOneBook(sBookID)

Dim sDropOff As String
Dim sOrganization As String
Dim sContact
For Each organization In organizations
If organization.DropOff = 1 Then
sDropOff = "True"
Else
sDropOff = "False"
End If
sOrganization = organization.Organization
sContact = organization.ContactName
Next

sBody = "Books Listing" & Chr(10) & Chr(10)
sBody = sBody & "Organization: " & sOrganization & Chr(10)
sBody = sBody & "Contact Name: " & sContact & Chr(10)
sBody = sBody & "Email: " & organization.Email & Chr(10)
sBody = sBody & "Phone Number: " & organization.Phone & Chr(10)

I'm using the FOR NEXT, but this is silly since I only have one record. GetDataByOneBook(sBookID) does exactly what is says, it returns a single book with a specific bookID.

Not only is this silly, it doesn't work. Using sContact as an example, it's Dim'd as a string. In the FOR NEXT loop, organization.ContactName has the right value, and it appears to be assigned to sContact correctly, but when I try to use sContact in sBody, I get an error saying that sContact has been used before it is assigned a value. Maybe the variables lose their scope outside the loop? Maybe I could get around this by building sBody inside the loop, but there has to be a better way!

Diane

I believe you want to do something like the following with your code:

1Dim BookAdapterAs New BooksTableAdapters.BooksBBTableAdapter2Dim organizationsAs Books.BooksBBDataTable3Dim organizationAs Books.BooksBBRow45 organizations = BookAdapter.GetDataByOneBook(sBookID)67If (organizations.Count > 0)Then89 sBody ="Books Listing" & Chr(10) & Chr(10)10 sBody = sBody &"Organization: " & organizations[0].Organization & Chr(10)11 sBody = sBody &"Contact Name: " & organizations[0].ContactName & Chr(10)12 sBody = sBody &"Email: " & organizations[0].Email & Chr(10)13 sBody = sBody &"Phone Number: " & organizations[0].Phone & Chr(10)1415End If
I do not have your XSD, so I can not test this.
|||

Thank you!

Diane