Update CRM 2011 using OrganizationServiceContext - dynamics-crm

How do you update records in CRM 2011 using OrganizationServiceContext? Can anyone provide a simple example? Thanks!
This is my code:
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Linq;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Client.Services;
using System.Data.Services;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Web.Security;
using System.Data;
using System.Collections.Specialized;
using System.Web.SessionState;
using System;
using System.Web.Profile;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Text;
using System.Web.Caching;
using Telerik.Web.UI;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Data.Entity;
using System.Data.Entity;
public partial class LeadShareEditPanel : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void imgBtnSaveNote_Click(object sender, ImageClickEventArgs e)
{
Uri organizationUri = new Uri("http://server/CRMT/XRMServices/2011/Organization.svc");
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential("user", "password", "domain");
OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
// Get the IOrganizationService
//Get OrganizationServiceContext -the organization service context class implements the IQueryable interface and
//a .NET Language-Integrated Query (LINQ) query provider so we can write LINQ queries against Microsoft Dynamics CRM data.
using (var service = new OrganizationService(orgProxy))
using (var context = new CrmOrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
contact.JobTitle = "Developer";
context.UpdateObject(contact);
context.SaveChanges();
contact.EMailAddress1 = "bob#contoso.com";
context.UpdateObject(contact);
context.SaveChanges();
}
}
}

This is an example from the 5.05 SDK help file
using (var service = new OrganizationService(connection))
using (var context = new CrmOrganizationServiceContext(service))
{
var contact = context.CreateQuery<Contact>().First(c => c.FirstName == "Bob");
contact.JobTitle = "Developer";
context.UpdateObject(contact);
context.SaveChanges();
contact.EMailAddress1 = "bob#contoso.com";
context.UpdateObject(contact);
context.SaveChanges();
}
If you haven't already you can download the SDK from here crm 2011 sdk . I would highly reccomend it as it has lots of great samples. The current version is 5.06.
Hope that helps.

Related

Does Linq Select does not work with IList?

I'm working with Visual Studio 2019 and it suggested a conversion for a foreach to Linq which then doesn't compile:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
static void Main(string[] args)
{
var list = new List<object>();
list.Add("One");
list.Add("Two");
list.Add("Three");
// To make the point...
var iList = (IList)list;
// This doesn't compile and generates CS1061: IList does not contain a definition for Select.
// I thought Linq gave Select to list types?
var toStringList = iList.Select(s => s.ToString());
// The original foreach version:
var outputList = new List<string>();
foreach (var item in iList)
{
var itemToString = item.ToString();
outputList.Add(itemToString);
}
}
Linq is a set of extension methods on everything that implements IEnumerable<T>.
The non-generic IList doesn't implement that interface, so Linq won't work on it.
Visual Studio 2019 Professional with ReSharper doesn't suggest Select for me:

Scrape a table using ScrapySharp and HtmlAgilityPack

I am trying to scrape an economic calendar from a specific website. Actually, I tried many times without any success, I don't know where I am wrong. Can you help me, pls?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using ScrapySharp.Network;
namespace Calendar
{
class Program
{
static void Main(string[] args)
{
var url = "https://www.fxstreet.com/economic-calendar";
var webGet = new HtmlWeb();
if (webGet.Load(url) is HtmlDocument document)
{
var nodes = document.DocumentNode.CssSelect("#fxst-calendartable tbody").ToList();
foreach (var node in nodes)
{
// event_time
Debug.Print(node.CssSelect("div div").Single().InnerText);
// event-title
Debug.Print(node.CssSelect("div a").Single().InnerText);
}
}
Console.WriteLine("");
Console.ReadLine()
}
}
}
What error are you getting?
If you want to publish the event names and times from the website, I am assuming you need to read the table.
You can do so using
HtmlNode tablebody = doc.DocumentNode.SelectSingleNode("//table[#class='fxs_c_table']/tbody");
foreach(HtmlNode tr in tablebody.SelectNodes("./tr[#class='fxs_c_row']"))
{
Console.WriteLine("\nTableRow: ");
foreach(HtmlNode td in tr.SelectNodes("./td"))
{
Console.WriteLine(td.SelectSingleNode("./span").InnerText);
}
}
Get hold of the table with the class attribute and then use relevant XPATH to traverse the elements. Please post the error you are getting with your code.

System.Data.EvaluateException: 'Cannot find column [NameID].'

I wanted to use a search textbox to filter a datagridview in Visual Studio, where you will enter a certain primary key (i.e. NameID) to filter the results in the datagridview. It throws the error which is noted in the title.
I have tried changing the code from NameID to [NameID] but this action did not work.
private void TxtNamecSearch_TextChanged(object sender, EventArgs e)
{
DataView dtvNames = new DataView(dtNames);
dtvNames.RowFilter = string.Format("Convert
(NameID,'System.String') Like '%{0}%'", TxtNameSearch.Text); //
Errror occurs on this line
NameDataGridView.DataSource = dtvNames;
}
I expect to type in the textbox, the primary key [NameID] which is a numeric number and the results in the datagridview will automatically filter by the column "NameID"
I have not added the DataColumn named NameID to my DataTable, as this column name "NameID" is being retrieved from MySQL database and it is populated into the Datagridview.
As a result, I had to change the code and this code, below, now works fine: -
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
public class Form1
{
private BindingSource NameBindingSource;
private void Form1_Load(System.Object sender, System.EventArgs e)
{
// Create a new DataTable
DataTable dtName = new DataTable();
// Add the NameID column
dtName.Columns.Add(new DataColumn("NameID"));
// Create a new BindingSource
NameBindingSource = new BindingSource();
// Bind the DataTable to the BindingSource
NameBindingSource.DataSource = dtName;
// Bind the BindingSource to the DataGridView
NameDataGridView.DataSource = NameBindingSource;
}
private void TxtNameSearch_TextChanged(System.Object sender, System.EventArgs e)
{
// Set the filter
NameBindingSource.Filter = string.Format("Convert
(NameID, 'System.String') LIKE '%{0}%'", TxtNameSearch.Text);
}
}

Scandit Barcode Scanner Shows Blank camera on scanning

I am trying to implement scandit barcode scanner in my application.i downloaded its sample demo and its works fine.
same code i tried to implement in my app.but it shows black screen on scanning .
I gave camera access also. cant find any thing missing.
Please help if someone also faced same issue. any suggestion most appreciated.
thanks in advance
This is my code
using FormBot.ViewModels.Abstract;
using Scandit.BarcodePicker.Unified;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace FormBot.ViewModels
{
public class SerialNumberViewModel: BaseViewModelDemo
{
private string _recognizedCode;
public ICommand StartScanningCommand => new Command(async () => await StartScanning());
public string RecognizedCode
{
get
{
return (_recognizedCode == null) ? "" : "Code scanned: " + _recognizedCode;
}
set
{
_recognizedCode = value;
}
}
public SerialNumberViewModel()
{
ScanditService.ScanditLicense.AppKey = "Key";
ScanditService.BarcodePicker.DidScan += BarcodePickerOnDidScan;
}
private async void BarcodePickerOnDidScan(ScanSession session)
{
RecognizedCode = session.NewlyRecognizedCodes.LastOrDefault()?.Data;
await ScanditService.BarcodePicker.StopScanningAsync();
}
private async Task StartScanning()
{
await ScanditService.BarcodePicker.StartScanningAsync(false);
}
}
}
in app.xaml.cs
private static string appKey = "key";
ScanditService.ScanditLicense.AppKey = appKey;
set android:hardwareAccelerated="true" in AndroidManifest.xml file worked for me.

MVC3 LINQ SubmitChanges Invalid

In the method below db.SubmitChanges is shown as invalid/not recognized by intellisense.
This is my first attempt to update records in a database using LINQ and the method may contain other logic/syntactical errors that I havent yet uncovered as well. What is causing the SubmitChanges to be incorrect?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
public void updateInfo(RefillViewModel _myRefillViewModel) {
try {
decimal patid = _myRefillViewModel.Patient.Patient_ID;
decimal rxid = _myRefillViewModel.Rx.Rx_ID;
CAHODEntities db = new CAHODEntities();
List<Fill> FillList = db.Fills.Where(p => p.Rx.Rx_ID == rxid && p.Rx.Patient_ID == patid && p.Status == "UnFilled").ToList();
foreach (var item in FillList)
{
if (FillList.Count() == 0)
{
item.Status = "Requested";
}
}
db.SubmitChanges();
}
}
Are you using EF? Because in Entity Framework it's SaveChanges() not SubmitChanges().

Resources