In the Gen class file I have got this structure:
Public Structure StructElements
Public ID As Integer
Public Name As String
End Structure
An then this function:
Public Function FillElements(_param As String, count As Integer) As StructElements
Dim i As Integer = 0
Dim myConn As SqlConnection = New SqlConnection
Dim myCmd As SqlCommand
Dim myReader As SqlDataReader
myConn.ConnectionString = sc.ConfigurationManager.ConnectionStrings("sidConnectionString").ConnectionString
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT * FROM " & _param
Select Case _param
Case = "Scopes"
Dim Scope(count) As StructElements
myConn.Open()
myReader = myCmd.ExecuteReader()
While myReader.Read
Scope(i).ID = myReader.Item(0)
Scope(i).Name = myReader.Item(1).ToString
i += 1
End While
myReader.Close()
myCmd.Dispose()
myConn.Close()
myConn.Dispose()
Return Scope
...
End Function
Then in the edit.aspx codebehind I have:
Public Class edit
...
Public istGen = New Gen()
...
Public iScopes As Integer
Public Scope(iScopes) As StructElements
...
Private Sub edit_Init(sender As Object, e As EventArgs) Handles Me.Init
...
iScopes = istGen.CountElements("Scopes") - 1
ReDim Scope(iScopes)
Scope = istGen.FillElements("Scopes", iScopes)
...
End Sub
End Class
This is the error I get:
Value of type 'Gen.StructElements()' cannot be converted to 'Gen.StructElements'.
I have figured out the problem is with the "Return Scope" of the FillElements function since it's expecting as an output a StructElements and not an array of StructElements (that's what the Scope variable is). How should I change my code in order for the function output to be an array of StructElements?
I have tried to look for an answer on the web but didn't find much. Someone was suggesting to use lists instead of arrays, but that would request to heavily refurbish my code and project, so if there is an easy way to do it I would strongly prefer it.
Thanks in advance for your support.
Regards.
Hi Please can someone show me how to return multiple values from a function? I have my function in a seperate file within App_Code and here it is:
Public Function GetQuoteStatus(ByVal QuoteID As String) As String
Dim quoteStatus As String
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = ConfigurationManager.AppSettings("quotingSystemConnectionString")
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT Status FROM Quote WHERE QuoteID =" & QuoteID & ";"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
quoteStatus = lrd("Status")
End While
Catch ex As Exception
Finally
con.Close()
End Try
Return quoteStatus
End Function
To get the returned quoteStatus from within another vb file in my site I would normally use:
Dim statusHelper As New quoteStatusHelper()
Dim quoteStatus As String = statusHelper.GetQuoteStatus("QuoteID")
However this is all good and well for one returned value but what If I wanted to return multiple values... How do I access them?
Many thanks!
You could use
Public Function GetQuoteStatus(ByVal QuoteID As String) As String()
This let you return a string array, so there is no limit to data you can have.
The cons is you have to parse the array.
You could even use
Public Function GetQuoteStatus(ByVal QuoteID As String,
Byref Second As String) As String
This let you return a string as result and set the value of another var (Second); you can use more than one Byref variables to return multiple values...
The cons is that you have to statically declare your function and modifiy all previous calls. In this case the use of refactoring is reccomended!
EDITED:
You could even return a class for example.
Declare a class that fits your needs (with all the fields, getters, setters and constructors) and inside your function you could create an instance of that class, fill every field and return this class.
Easy to implement, easy to use.
EDITED AGAIN:
Public Class MyClass
Public Property Val1 As String
Public Property Val2 As String
Public Property Val3 As String
Public Sub New(ByVal newVal1 As String, ByVal newVal2 As String, ByVal newVal3 As String)
Val1 = newVal1
Val2 = newVal2
Val3 = newVal3
End Sub
End Class
Public Function GetInfo() As MyClass
Dim mc As New MyClass("test1", "test2", "test3")
Return mc
End Function
You may return multiple values using ByRef and arrays (see #Marco post). You may also return/pass an object for your purpose.
For instance,
Public Class Info
Public Property No As Integer
Public Property Name As String
End Class
....
Public Function GetInfo() As Info
Dim inf As New Info
inf.No = 10
inf.Name = "A"
Return inf
End Function
....
I believe that the "correct/best" way to deal with a single function that needs to return a couple of discrete (typically primitive) types is a Tuple(Of T1, T2, T3, T4, T5, T6, T7, TRest).
Something like this
Public Function MyExampleMethod() As Tuple(Of String, Integer, Guid)
Return New Tuple(Of String, Integer, Guid)("Value1", 2, Guid.NewGuid)
End Function
System.DirectoryServices.AccountManagement can be extended to support additional properties for reading and writing AD properties.
Is anyone aware of a full/complete sample implementation that works for AD, Exchange 2003 or 2010?
There isn't anything online that I know of, but you are welcome to my collection (which I've included).
One thing you'll probably notice about my code is that I've almost completely replaced the standard get/set operations with my own code which writes directly to the underlying DirectoryEntry. This is because the built in set operation is not designed to handle data types which are arrays of arrays (such as the jpegPhoto attribute which is an array of byte arrays, with each byte array representing a picture).
First is a bunch of extension methods which I use for my various get/set operations.
''' <summary>
''' Checks if an attribute is available on the underlying object.
''' </summary>
<Extension()> _
Public Function IsAttributeDefined(ByVal prin As Principal, ByVal attribute As String) As Boolean
'since some attributes may not exist in all schemas check to see if it exists first
Dim uo As DirectoryEntry = DirectCast(prin.GetUnderlyingObject(), DirectoryEntry)
'check for property, if it's not found return an empty array
Return uo.Properties.Contains(attribute)
End Function
#Region "Get Helpers"
''' <summary>
''' This function is the foundation for retrieving data
''' </summary>
<Extension()> _
Public Function ExtensionGetAttributeObject(ByVal prin As Principal, ByVal attribute As String) As Object()
'check if the attribute exists on this object
If IsAttributeDefined(prin, attribute) Then
'if property exists then return the data
Dim dirObj As DirectoryEntry = prin.GetUnderlyingObject()
Dim val As Object() = (From c As Object In dirObj.Properties(attribute) Select c).ToArray()
Return val
Else
'return an empty array if the attribute is not defined
Return New Object(-1) {}
End If
End Function
''' <summary>
''' This is the primary function for retrieving attributes that contain only one value
''' </summary>
<Extension()> _
Public Function ExtensionGetSingleValue(ByVal prin As Principal, ByVal attribute As String) As Object
'get the object
Dim attributeValues() As Object = ExtensionGetAttributeObject(prin, attribute)
'if the item length = 1 then return the first value, else don't
If attributeValues.Length = 1 Then
Return attributeValues(0)
Else
Return Nothing
End If
End Function
''' <summary>
''' Returns the string value of an attribute
''' </summary>
''' <remarks>(null if no value found)</remarks>
<Extension()> _
Public Function ExtensionGetSingleString(ByVal prin As Principal, ByVal attribute As String) As String
Dim o As Object = ExtensionGetSingleValue(prin, attribute)
If o IsNot Nothing Then
Return o.ToString()
Else
Return String.Empty
End If
End Function
''' <summary>
''' Returns all of the strings contained in a multi-value attribute
''' </summary>
<Extension()> _
Public Function ExtensionGetMultipleString(ByVal prin As Principal, ByVal attribute As String) As String()
'get the object array for this attribute
Dim attributeValues() As Object = ExtensionGetAttributeObject(prin, attribute)
'create a string array of the same length as the object array
Dim array As String() = New String(attributeValues.Length - 1) {}
'and copy over all items, converting them to strings as we go
For i As Integer = 0 To attributeValues.Length - 1
array(i) = attributeValues(i).ToString()
Next
'return the string array
Return array
End Function
''' <summary>
''' Returns the date value of an attribute
''' </summary>
''' <remarks>(null if no value found)</remarks>
<Extension()> _
Public Function ExtensionGetSingleDate(ByVal prin As Principal, ByVal attribute As String) As String
Dim o As Object = ExtensionGetSingleValue(prin, attribute)
If o IsNot Nothing Then
Dim dt As DateTime = Convert.ToDateTime(o)
Return dt
Else
Return Nothing
End If
End Function
''' <summary>
''' Returns the principle represented by a column containing a single distinguished name
''' </summary>
<Extension()> _
Public Function ExtensionGetSingleDistinguishedName(ByVal prin As Principal, ByVal attribute As String) As Principal
'get the distinguished name of the object as a string
Dim dn As String = ExtensionGetSingleString(prin, attribute)
'check for null
If String.IsNullOrEmpty(dn) Then
Return Nothing
End If
'get the principal represented by the DN
Dim prinF As Principal = Principal.FindByIdentity(prin.Context, dn)
'if it exists then prepare to return it
If prinF IsNot Nothing Then
'if the object is a userprincipal then get the user detailed principal for it.
If TypeOf prinF Is UserPrincipal Then
prinF = UserDetailedPrinciple.FindByIdentity(prin.Context, prinF.Name)
End If
'return the principal
Return prinF
End If
'if all else fails return nothing
Return Nothing
End Function
<Extension()> _
Public Function ExtensionGetMultipleDistinguishedNames(ByVal prinParent As Principal, ByVal attribute As String) As Principal()
'get the distinguished name of the object as a string
Dim dn() As String = ExtensionGetMultipleString(prinParent, attribute)
'array to hold list of principles
Dim al As New List(Of Principal)()
For Each d As String In dn
'get the principal represented by the DN
Dim prin As Principal = Principal.FindByIdentity(prinParent.Context, d)
'if it exists then prepare to return it
If prin IsNot Nothing Then
'if the object is a userprincipal then get the user detailed principal for it.
If TypeOf prin Is UserPrincipal Then
prin = UserDetailedPrinciple.FindByIdentity(prin.Context, prin.Name)
ElseIf TypeOf prin Is GroupPrincipal Then
prin = GroupPrincipal.FindByIdentity(prin.Context, prin.Name)
End If
'return the principal
al.Add(prin)
End If
Next
'return list of principles
Return al.ToArray()
End Function
''' <summary>
''' Gets the bytes contained in an Octet String
''' </summary>
<Extension()> _
Public Function ExtentsionGetBytes(ByVal prin As Principal, ByVal attribute As String) As Byte()
'get the data
Dim o As Object = ExtensionGetSingleValue(prin, attribute)
'check for nulls
If o Is Nothing Then
Return Nothing
End If
'get the byte array
Dim byteArray() As Byte = DirectCast(o, Byte())
'return the data
Return byteArray
End Function
''' <summary>
''' Gets the image contained in an Octet String type attribute
''' </summary>
<Extension()> _
Public Function ExtensionGetImage(ByVal prin As Principal, ByVal attribute As String) As Image
'get bytes for attribute
Dim bytearray() As Byte = ExtentsionGetBytes(prin, attribute)
'if none returned return nothing
If bytearray Is Nothing Then
Return Nothing
End If
'read the bytes into a memory stream
Dim ms As New MemoryStream(bytearray)
'convert the memory stream to a bitmap and return it
Return New Bitmap(ms)
End Function
<Extension()> _
Public Function ExtensionGetImages(ByVal prin As Principal, ByVal attribute As String) As Image()
'get all values in attribute
Dim vals() As Object = ExtensionGetAttributeObject(prin, attribute)
'array to hold images to be returned
Dim al As New List(Of Image)()
For Each o As Object In vals
'get bytes
Dim bytearray() As Byte = DirectCast(o, Byte())
'if no data skip entry
If bytearray Is Nothing Then
Continue For
End If
'read the bytes into a memory stream
Dim ms As New MemoryStream(bytearray)
'convert the memory stream to a bitmap and add to the array
al.Add(New Bitmap(ms))
Next
'return the list of images as an array.
Return al.ToArray()
End Function
#End Region
#Region "Set Helpers"
Private Sub ExtensionSetDE(ByVal de As DirectoryEntry, ByVal attribute As String, ByVal value As Object)
'check value, if it's null then don't add (null means clear only)
If value IsNot Nothing Then
de.Properties(attribute).Add(value)
End If
End Sub
<Extension()> _
Public Sub ExtensionSetValue(ByVal prin As Principal, ByVal attribute As String, ByVal value As Object)
Dim uo As DirectoryEntry = prin.GetUnderlyingObject()
uo.Properties(attribute).Clear()
ExtensionSetDE(uo, attribute, value)
End Sub
<Extension()> _
Public Sub ExtensionSetStringValue(ByVal prin As Principal, ByVal attribute As String, ByVal value As String)
If String.IsNullOrEmpty(value) Then
value = Nothing
End If
ExtensionSetValue(prin, attribute, value)
End Sub
<Extension()> _
Public Sub ExtensionSetMultipleValueDirect(ByVal prin As Principal, ByVal attribute As String, ByVal values() As Object)
'Normal ExtensionSet does not support saving array type values (octet string)
' so we set it directly on the underlying object
Dim uo As DirectoryEntry = prin.GetUnderlyingObject()
uo.Properties(attribute).Clear()
If values IsNot Nothing Then
For Each v As Object In values
ExtensionSetDE(uo, attribute, v)
Next
End If
End Sub
<Extension()> _
Public Sub ExtensionSetImage(ByVal prin As Principal, ByVal attribute As String, ByVal img As Image)
'set data to attribute
ExtensionSetValue(prin, attribute, img.SaveImageToByteArray())
End Sub
<Extension()> _
Public Sub ExtensionSetImages(ByVal prin As Principal, ByVal attribute As String, ByVal img() As Image)
'array list to hold the values temporarily
Dim al As New ArrayList()
'convert each image into a byte array
For Each i As Image In img
al.Add(i.SaveImageToByteArray())
Next
'set image array as value on attribute
ExtensionSetMultipleValueDirect(prin, attribute, al.ToArray())
End Sub
<Extension()> _
Public Function SaveImageToByteArray(ByVal img As Image) As Byte()
'create a memory strea
Dim ms As New MemoryStream()
'write the image to the stream
img.Save(ms, Imaging.ImageFormat.Jpeg)
'save data to a byte array
Dim bytes() As Byte = ms.ToArray()
Return bytes
End Function
<Extension()> _
Public Sub ExtensionSetMultipleDistinguishedNames(ByVal prin As Principal, ByVal attribute As String, ByVal dns() As Principal)
'convert user principles into distinguished names
Dim sc As New ArrayList()
For Each u As UserDetailedPrinciple In dns
sc.Add(u.DistinguishedName)
Next
ExtensionSetMultipleValueDirect(prin, attribute, sc.ToArray())
End Sub
''' <summary>
''' Helps set the Thumbnail photo by resizing main photo and also saving original (possibly resized to 300xvariable)
''' to JpegPhoto.
''' </summary>
''' <param name="imgO">The iamge to use as the users thumbnail photo</param>
''' <remarks>You still NEED to call .Save() after calling this sub
''' as this sub does not call save().
''' </remarks>
<Extension()> _
Public Sub SetUserPhoto(ByVal prin As Principal, ByVal imgO As Image)
'resize the image for thumbnail
Dim imgN As Bitmap = ResizeImage(imgO, 100)
'check if we need to resize for medium sized image (300px high max
Dim imgM As Bitmap
If imgO.Height > 300 Then
imgM = ResizeImage(imgO, 300)
Else
imgM = imgO
End If
'save small image to the users profile
ExtensionSetImage(prin, "thumbnailPhoto", imgN)
'save original to the jpegPhoto attribute
ExtensionSetImages(prin, "jpegPhoto", New Image() {imgM})
End Sub
Private Function ResizeImage(ByVal imgO As Bitmap, ByVal Height As Integer) As Bitmap
'if the image is smaller/equal to the requested height return original
If imgO.Height <= Height Then
Return imgO
End If
'images are fixedHeightxVariable, so we need to calculate the variable portion
Dim width As Integer = (Convert.ToDecimal(imgO.Width) / Convert.ToDecimal(imgO.Height)) * Height
'resize the image
Dim imgN As New Bitmap(width, Height)
Dim g As Graphics = Graphics.FromImage(imgN)
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
'draw in resized form
g.DrawImage(imgO, 0, 0, width, Height)
'return resized image
Return imgN
End Function
<Extension()> _
Public Function Rename(ByVal prin As Principal, ByVal NewName As String) As Principal
'escape commas
NewName = NewName.Replace(",", "\,")
'get directory object for move
Dim de As DirectoryEntry = prin.GetUnderlyingObject()
'move
de.Rename(String.Format("CN={0}", NewName))
de.CommitChanges()
'get the new object by name and return it
Return New ADConnection(prin.Context).GetPrincipalByName(prin.Guid.ToString())
End Function
#End Region
Here is the code in action in my custion UserPrinciple:
<DirectoryObjectClass("user")> _
<DirectoryRdnPrefix("CN")> _
Public Class UserDetailedPrinciple
Inherits UserPrincipal
<DirectoryProperty("initials")> _
Public Property MiddleInitial() As String
Get
Return ExtensionGetSingleString("initials")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("initials", value)
End Set
End Property
<DirectoryProperty("wWWHomePage")> _
Public Property HomePage() As String
Get
Return ExtensionGetSingleString("wWWHomePage")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("wWWHomePage", value)
End Set
End Property
<DirectoryProperty("url")> _
Public Property URLs() As String()
Get
Return ExtensionGetMultipleString("url")
End Get
Set(ByVal value As String())
ExtensionSetMultipleValueDirect("url", value)
End Set
End Property
<DirectoryProperty("info")> _
Public Property Notes() As String
Get
Return ExtensionGetSingleString("info")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("info", value)
End Set
End Property
Public ReadOnly Property ObjectType() As String
Get
Dim types() As String = ExtensionGetMultipleString("objectClass")
Return types.Last()
End Get
End Property
<DirectoryProperty("thumbnailPhoto")> _
Public Property ThumbnailPhoto() As Image
Get
Return ExtensionGetImage("thumbnailPhoto")
End Get
Set(ByVal value As Image)
ExtensionSetImage("thumbnailPhoto", value)
End Set
End Property
<DirectoryProperty("thumbnailLogo")> _
Public Property ThumbnailLogo() As Image
Get
Return ExtensionGetImage("thumbnailLogo")
End Get
Set(ByVal value As Image)
ExtensionSetImage("thumbnailLogo", value)
End Set
End Property
<DirectoryProperty("jpegPhoto")> _
Public Property JpegPhoto() As Image()
Get
Return ExtensionGetImages("jpegPhoto")
End Get
Set(ByVal value As Image())
ExtensionSetImages("jpegPhoto", value)
End Set
End Property
<DirectoryProperty("title")> _
Public Property Title() As String
Get
Return ExtensionGetSingleString("title")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("title", value)
End Set
End Property
<DirectoryProperty("department")> _
Public Property Department() As String
Get
Return ExtensionGetSingleString("department")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("department", value)
End Set
End Property
<DirectoryProperty("company")> _
Public Property Company() As String
Get
Return ExtensionGetSingleString("company")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("company", value)
End Set
End Property
<DirectoryProperty("manager")> _
Public Property Manager() As UserDetailedPrinciple
Get
Dim mgr As UserDetailedPrinciple = ExtensionGetSingleDistinguishedName("manager")
If mgr IsNot Nothing Then
If Me.Guid <> mgr.Guid Then
Return mgr
End If
End If
Return Nothing
End Get
Set(ByVal value As UserDetailedPrinciple)
'check for nulls
If value Is Nothing Then
ExtensionSetStringValue("manager", Nothing)
Else
ExtensionSetStringValue("manager", value.DistinguishedName)
End If
End Set
End Property
<DirectoryProperty("assistant")> _
Public Property Assistant() As UserDetailedPrinciple
Get
Dim assist As UserDetailedPrinciple = ExtensionGetSingleDistinguishedName("assistant")
If assist IsNot Nothing Then
Return assist
End If
Return Nothing
End Get
Set(ByVal value As UserDetailedPrinciple)
'check for nulls
If value Is Nothing Then
ExtensionSetStringValue("assistant", Nothing)
Else
ExtensionSetStringValue("assistant", value.DistinguishedName)
End If
End Set
End Property
<DirectoryProperty("directReports")> _
Public Property DirectReports() As Principal()
Get
Dim dReports As Principal() = ExtensionGetMultipleDistinguishedNames("directReports")
Return dReports
End Get
Set(ByVal value As Principal())
ExtensionSetMultipleDistinguishedNames("directReports", value)
End Set
End Property
<DirectoryProperty("homePhone")> _
Public Property HomePhone() As String
Get
Return ExtensionGetSingleString("homePhone")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("homePhone", value)
End Set
End Property
<DirectoryProperty("pager")> _
Public Property Pager() As String
Get
Return ExtensionGetSingleString("pager")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("pager", value)
End Set
End Property
<DirectoryProperty("otherTelephone")> _
Public Property OtherTelephone() As String()
Get
Return ExtensionGetMultipleString("otherTelephone")
End Get
Set(ByVal value As String())
ExtensionSetMultipleValueDirect("otherTelephone", value)
End Set
End Property
<DirectoryProperty("physicalDeliveryOfficeName")> _
Public Property PhysicalLocation() As String
Get
Return ExtensionGetSingleString("physicalDeliveryOfficeName")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("physicalDeliveryOfficeName", value)
End Set
End Property
<DirectoryProperty("l")> _
Public Property AddressCity() As String
Get
Return ExtensionGetSingleString("l")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("l", value)
End Set
End Property
<DirectoryProperty("postOfficeBox")> _
Public Property AddressPOBox() As String
Get
Return ExtensionGetSingleString("postOfficeBox")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("postOfficeBox", value)
End Set
End Property
<DirectoryProperty("st")> _
Public Property AddressState() As String
Get
Return ExtensionGetSingleString("st")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("st", value)
End Set
End Property
<DirectoryProperty("streetAddress")> _
Public Property Address() As String
Get
Return ExtensionGetSingleString("streetAddress")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("streetAddress", value)
End Set
End Property
<DirectoryProperty("postalCode")> _
Public Property AddressZipCode() As String
Get
Return ExtensionGetSingleString("postalCode")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("postalCode", value)
End Set
End Property
<DirectoryProperty("c")> _
Public Property AddressCountry() As String
Get
Return ExtensionGetSingleString("c")
End Get
Set(ByVal value As String)
ExtensionSetStringValue("c", value)
End Set
End Property
<DirectoryProperty("whenCreated")> _
Public ReadOnly Property Created() As Nullable(Of DateTime)
Get
Return ExtensionGetSingleDate("whenCreated")
End Get
End Property
<DirectoryProperty("whenChanged")> _
Public ReadOnly Property LastModified() As Nullable(Of DateTime)
Get
Return ExtensionGetSingleDate("whenChanged")
End Get
End Property
Public Sub New()
MyBase.New(ADConnection.CurrentADPrincipalContext)
End Sub
Public Sub New(ByVal context As PrincipalContext)
MyBase.New(context)
End Sub
Public Overloads Shared Function FindByIdentity(ByVal context As PrincipalContext, ByVal identityValue As String) As UserDetailedPrinciple
Return DirectCast(Principal.FindByIdentityWithType(context, GetType(UserDetailedPrinciple), identityValue), UserDetailedPrinciple)
End Function
Public Overloads Shared Function FindByIdentity(ByVal context As PrincipalContext, ByVal identityType As IdentityType, ByVal identityValue As String) As UserDetailedPrinciple
Return DirectCast(Principal.FindByIdentityWithType(context, GetType(UserDetailedPrinciple), identityType, identityValue), UserDetailedPrinciple)
End Function
End Class
I'm creating an application where people can insert an event with information such as eventname, location, date, hours, ...
There are 3 steps. the first 2 steps with the inserts were succesfully inserted. But at step 3 where they have to insert a startDay (Startdag), endDay(Einddag), starthour(startUurt), endHour(eindUur) I can't get it fixed.
In the presentationlayer I'm using the calendarExtender for Startday and Endday with format d MMMM yyyy with and target txt_sDag and txt_eDag
In dataset "DAL_Planit3.xsd" I've given endDay and startDay as type "datetime" and startHour and endHour as type String (becoz we will only read it, we won't be doing anything with it).
Can someone please help me?
StepThree.aspx.vb
Protected Sub btn_next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_next.Click
Try
Dim BLLdata As New BLL_Data
Dim eventID As Integer = Session.Item("eventID")
Dim plaatsID As Integer = Session.Item("plaatsID")
Dim data As New CL_Data
data.Einddag = txt_eDag.Text
data.Startdag = CDate(txt_sDag.Text)
data.Einddag = CDate(txt_eDag.Text)
data.Einduur = txt_eUur.Text
data.StartUur = txt_sUur.Text
data.FKEvenementId = eventID
data.FKPlaatsId = plaatsID
BLLdata.insertData(data)
lbl_feedback.Text = "Data succesful inserted!"
Catch ex As Exception
lbl_feedback.Text = "Data not inserted! " + ex.Message
End Try
End Sub
End Class
*BLL_Data.vb*
' INSERT DATA
Public Function insertData(ByVal data As CL_Data) As String
Dim feedback As String = Nothing
Try
adapterDatum3.Insert(data.Startdag, data.Einddag, data.StartUur, data.Einduur, data.FKPlaatsId, data.FKEvenementId)
'adapterDatum.Insert(data.Startdag, data.Einddag, data.StartUur, data.Einduur, data.FKPlaatsId, data.FKEvenementId)
feedback = "Data is toegevoegd"
Catch ex As Exception
feedback = ex.Message
End Try
Return feedback
End Function
CL_Data
Public Class CL_Data
Private iDataId As Integer
Public Property DataID() As Integer
Get
Return iDataId
End Get
Set(ByVal value As Integer)
iDataId = value
End Set
End Property
Private dStartDag As Date
Public Property Startdag() As Date
Get
Return dStartDag
End Get
Set(ByVal value As Date)
dStartDag = value
End Set
End Property
Private dEindDag As Date
Public Property Einddag() As Date
Get
Return dEindDag
End Get
Set(ByVal value As Date)
dEindDag = value
End Set
End Property
Private tStartUur As String
Public Property StartUur() As String
Get
Return tStartUur
End Get
Set(ByVal value As String)
tStartUur = value
End Set
End Property
Private tEindUur As String
Public Property Einduur() As String
Get
Return tEindUur
End Get
Set(ByVal value As String)
tEindUur = value
End Set
End Property
Private iFKPlaatsId As Integer
Public Property FKPlaatsId() As Integer
Get
Return iFKPlaatsId
End Get
Set(ByVal value As Integer)
iFKPlaatsId = value
End Set
End Property
Private iFKEvenementId As Integer
Public Property FKEvenementId() As Integer
Get
Return iFKEvenementId
End Get
Set(ByVal value As Integer)
iFKEvenementId = value
End Set
End Property
End Class
i am using the Json.net to serialize an object. the specific object is eventInstance.Properties which is the properties of a windows event log.
i am getting a
Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property
for C# an example is shown here
string json = JsonConvert.SerializeObject(joe, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
my line of code is below but i am not sure how to make it work in VB or if it is even possible
command.Parameters.AddWithValue("#f18", JsonConvert.SerializeObject(eventInstance.Properties(), New JsonSerializerSettings() {ReferenceLoopHandling = ReferenceLoopHandling.Ignore}))
i get an error that states 'ReferenceLoopHandling' is an enum type and cannot be used as an expression
thanks for the help
You can use below code:
Private Function getJSON(sJSON As String) As String
Dim objNews = New List(Of News)()
Dim news = New News()
news.id = ""
news.title = "blah"
Dim lst = New List(Of Object)()
lst.Add(New With {.video_identifier = "id1"})
lst.Add(New With {.video_identifier = "id2"})
news.video_identifier = lst.ToArray()
objNews.Add(news)
Return Newtonsoft.Json.JsonConvert.SerializeObject(New With {.data = objNews})
End Function
Class News
Public Property title As String
Get
Return _title
End Get
Set
_title = value
End Set
End Property
Private _title As String
Private _sId As String
Public Property id As String
Get
Return _sId
End Get
Set
_sId = value
End Set
End Property
Private _youtube_videos As Object() = New List(Of Object)().ToArray()
Public Property video_identifier As Object()
Get
Return _youtube_videos
End Get
Set
_youtube_videos = value
End Set
End Property
End Class
Public Class YoutubeVideos
Private _video_identifier As String
Public Property video_identifier As String
Get
Return _video_identifier
End Get
Set
_video_identifier = value
End Set
End Property
End Class