Here is some code from the CRUDOperations.vb file in Microsoft Dynamics CRM 2011 SDK:
' Retrieve the account containing several of its attributes.
Dim cols As New ColumnSet(New String() { "name", "address1_postalcode", "lastusedincampaign" })
Dim retrievedAccount As Account = CType(_service.Retrieve("account", _accountId, cols), Account)
Console.Write("retrieved, ")
' Update the postal code attribute.
retrievedAccount.Address1_PostalCode = "98052"
' The address 2 postal code was set accidentally, so set it to null.
retrievedAccount.Address2_PostalCode = Nothing
Now if I am understanding correctly, the following line:
Dim retrievedAccount As Account = CType(_service.Retrieve("account", _accountId, cols), Account)
Doesn't seem to actually do anything?
It calls _service.Retrieve("account", _accountId, cols)
It casts the return value to type Account.
It stores the return value in the variable retrievedAccount.
And then the subsequent lines modify the properties of the account that is referenced by retrievedAccount.
Related
I'm working on a Sindows Forms application to help keep inventory of some scanners. I'm using Linq2Sql, each table has an id column. On my repair history form. I'm trying to use the serial number from the inventory table so it goes to the database and looks up the sID from the table and it returns the correct value, but when I go to send all the entered data to the history table it gets a null reference exception.
Dim db As New DataClasses1DataContext
Dim rep As Scanner_Repair_History
Dim scan = (From Scanner_Inventory In db.Scanner_Inventories Where scannerid.Text = Scanner_Inventory.SN Select Scanner_Inventory.SID).FirstOrDefault
rep.SID = scan
rep.Date_Broken = datebroke.Value
rep.Description = description.Text
rep.Send_Date = senddate.Text
rep.Recieve_Date = recievedate.Text
rep.Cost = cost.Text
rep.PlantID = plantid.Text
rep.BID = brokenid.Text
rep.RMAnumber = rmanum.Text
db.Scanner_Repair_Histories.InsertOnSubmit(rep)
db.SubmitChanges()
is that me but you didn't instanciate your "rep" variable
You don't have a defined object for placement with a 'new' keyword but I am also curious if it is a system.type.
Update based on Jinx88909
You may be returning an entire POCO Object that may be null and have a null property. You can adjust this most times by doing a null condition if you are using .NET 4.5 and up. '?.' operator.
Dim db As New DataClasses1DataContext
'I need to be a new object and not instantiated as Nothing
Dim rep As New Scanner_Repair_History
'You have the potential for a nothing value here as 'FirstOrDefault' includes a potential Nothing'.
'I would get the entire object and then just a property of it after the fact
Dim scan = (From Scanner_Inventory In db.Scanner_Inventories Where scannerid?.Text = Scanner_Inventory?.SN Select Scanner_Inventory).FirstOrDefault?.Sid
If scan IsNot Nothing Then
rep.SID = scan 'Could you maybe want scan.Id or something similar?
rep.Date_Broken = datebroke.Value
rep.Description = description.Text
rep.Send_Date = senddate.Text
rep.Recieve_Date = recievedate.Text
rep.Cost = cost.Text
rep.PlantID = plantid.Text
rep.BID = brokenid.Text
rep.RMAnumber = rmanum.Text
db.Scanner_Repair_Histories.InsertOnSubmit(rep)
db.SubmitChanges()
Else
Console.WriteLine("I got nothing for you with the inputs you put in!")
End If
I have some code that populates a SOAP request in VB.NET. I get the data from a SQL query and run through the objects members to populate each one. I'm trying to find a quick(ish) way to not provide the member when it is empty rather than checking each value before applying it. As the SQL data comes in from a pipe delimited file in the first place we never get NULL just empty cells which get sent in the SOAP request as "".
Is there a way to define an objects member using a variable rather than its literal name?
UpLB_request_Items.Spare8 = Convert.ToString(SourceDataSet.tables(0).Rows(i)(colSPARE8))
UpLB_request_Items.Spare9 = Convert.ToString(SourceDataSet.tables(0).Rows(i)(colSPARE9))
UpLB_request_Items.Spare10 = Convert.ToString(SourceDataSet.tables(0).Rows(i)(colSPARE10))
Call IterateObject(UpLB_request_Items)
So this populates each member with the value from the SQL data (SourceDataSet), then I could send the completed object down to a Sub to check each value before I actually send it to the webservice
Sub IterateObject(objName)
Dim CollName = ""
For Each m As System.Reflection.PropertyInfo In objName.GetType().GetProperties()
If m.CanRead Then
If m.PropertyType.Name = "String" Then
CollName = m.Name
Dim CollVal = CallByName(objName, CollName, CallType.Get)
If CollVal = "" Then
objName.CollName = Nothing 'this is the tricky bit
End If
End If
End If
Next
End Sub
The bit where it obviously breaks is objName.CollName = Nothing as it will then say that the object doesn't have a member called CollName and I'm not sure if there is a way to force the code to evaluate CollName to its value (e.g. SPARE8 rather than the string "CollName")
I went with this in the end
UpLB_request_Items.Spare4 = Strings.Replace(Convert.ToString(SourceDataSet.Tables(0).Rows(i)(colSPARE4)), "", Nothing)
How do I send a list of MDX parameters to SSRS via ParameterValue in VB?
I’m not sure if that is the correct question, but here are the symptoms: When I send one store ID to SSRS I get back valid data. But as soon as I string together a series of stores IDs it returns – (dashes) where numeric values should be.
So this returns valid sales numbers:
Private parametersRdl(1) As ParameterValue
parametersRdl(0) = New ParameterValue()
parametersRdl(0).Name = "StoreLocations"
parametersRdl(0).Value = "[Stores].[Store Location].[Stores].&[456789]”
parametersRdl(1) = New ParameterValue()
parametersRdl(1).Name = "EndDate"
parametersRdl(1).Value = "[Current Date].[YearMonthDate].[Month].&[2012-12-01T00:00:00]"
This returns dashes instead of sales numbers:
Private parametersRdl(1) As ParameterValue
parametersRdl(0) = New ParameterValue()
parametersRdl(0).Name = "StoreLocations"
parametersRdl(0).Value = "[Stores].[Store Location].[Stores].&[456789], "[Stores].[Store Location].[Stores].&[123456]”
parametersRdl(1) = New ParameterValue()
parametersRdl(1).Name = "EndDate"
parametersRdl(1).Value = "[Current Date].[YearMonthDate].[Month].&[2012-12-01T00:00:00]"
I have tried various parameter separators, such as &, comma, ‘\,’, etc.
Does ParameterValue accept a list? Do I set or append parametersRdl(0).Value multiple times?
Of course when I set a default set in BIDS it works great. When I send a list via the web the logs show it appends them with \, (slash comma) but that doesn’t seem to work.
Visual Studio 2010, VB.net, SSRS 2010 (management), SSRS 2005 (execution)
Thanks for all your help!
OK, so here’s the answer. You can add multiple values for the same parameter as long as you reference the parameter by name. There are a few ways to do this.
The first method allows you to define the exact amount of list objects, however the second method makes it much easier to add a bunch of values. Regardless, each method allows you to add a list of MDX values to the same SSRS parameter.
Method 1: Add values by specific index.
Private parametersRdl(7) As ParameterValue
‘ Add your other parameters
parametersRdl(5) = New ParameterValue()
parametersRdl(5).Name = "StoresStoreLocation"
parametersRdl(5).Value = "[Stores].[Store Location].[Stores].&[123]"
parametersRdl(6) = New ParameterValue()
parametersRdl(6).Name = "StoresStoreLocation"
parametersRdl(6).Value = "[Stores].[Store Location].[Stores].&[456]"
Method 2: Split a string value into a list and loop through it.
Private parametersRdl(50) As ParameterValue
Dim StoresString As String = "123, 456, 789, 741, 852, 963"
Dim StoresList() As String = StoresString.Split(",")
For n As Integer = 0 To StoresList.Count - 1
parametersRdl(n) = New ParameterValue()
parametersRdl(n).Name = "StoresStoreLocation"
parametersRdl(n).Value = "[Stores].[Store Location].[Stores].&[" + StoresList(n) + "]"
Next
P.S. As usual it took many hours of banging my head against my desk to figure this out. I hope it helps.
I'm new to VBA and i need help.
I want to create vba function which takes table name as input, and distinct specific field from that table. I created function, and it works when i run it in vba immediate window (when i use debug.print command to display results). But when i call this function in sql, instead whole field values, it returns just last one. I'm not good at vba syntax so i need help to understand. Does function can return more than one value? If can, how, and if not what else to use? Here's my code:
Public Function TableInfo(tabela As String)
Dim db As Database
Dim rec As Recordset
Dim polje1 As Field, polje2 As Field
Dim sifMat As Field, pogon As Field, tipVred As Field
Set db = CurrentDb()
Set rec = db.OpenRecordset(tabela)
Set sifMat = rec.Fields("Field1")
Set pogon = rec.Fields("Field2")
Set tipVred = rec.Fields("Field3")
For Each polje1 In rec.Fields
For Each polje2 In rec.Fields
TableInfo = pogon.Value
rec.MoveNext
Next
Next
End Function
Any help is appreciated.
The problem is with this line probably:
TableInfo = pogon.Value
It runs inside the loop and returns the last value of the loop.
Instead of returning one value TableInfo, you may try to return something similar to a Collection or an Array.
Inside the loop, append values in the Collection and after the loop, return the Collection back from the function.
Edit:
I have re-written the code shared by you:
Public Function TableInfo(tabela As String) as String()
Dim db As Database
Dim rec As Recordset
Dim polje1 As Field, polje2 As Field
Dim sifMat As Field, pogon As Field, tipVred As Field
Dim returnValue() As String
Dim i as Integer
Set db = CurrentDb()
Set rec = db.OpenRecordset(tabela)
Set sifMat = rec.Fields("Field1")
Set pogon = rec.Fields("Field2")
Set tipVred = rec.Fields("Field3")
' I am not going to modify this but I think we can do away with two For Each loops.
' Just iterate over rec like
' For Each r In rec -> please use proper naming conventions and best practices
' and access each field as r("Field1") and r("Field2")
For Each polje1 In rec.Fields
For Each polje2 In rec.Fields
returnValue(i) = pogon.Value
i = i + 1
rec.MoveNext
Next
Next
TableInfo = returnValue
End Function
Please note: I have not tested this code but I assume this should work for you. Also, I have assumed that you want to return String() array. Please change the data type if you want to return some other type.
When you call the array (as posted in theghostofc's answer), you will need to do something like this:
Dim TableInfo() As String
For i = LBound(TableInfo) To UBound(TableInfo)
YourValue = TableInfo(i)
... Process some code that uses YourValue
Next i
If you're not looping through your array, you're not going to get each individual value out of it.
Set domSession = CreateObject("Notes.NotesSession")
Dim domDatabase
Set domDatabase = domSession.GetDatabase("server Name", "xyz.nsf")
Set domView = domDatabase.GetView("All Projects")
Up to here it's ok. But I try lot but unable to read the document values.
I am looking for the vbscript code to read the document data.
Add this:
Dim i%
Dim s$
Dim doc As Object
For i% = 1 to domView.EntryCount
Set doc = domView.GetNthDocument(i%)
s$ = doc.GetItemValue("itemname")(0)
' Do whatever you want with the string value which is now stored in s$
Next
This code assumes the items are text type. You could use simply Dim s to get any item type (number, date).
To get column values from the view you can use doc.ColumnValues(2). This returns the value of 3rd column (0 is first).