tags:

views:

389

answers:

1

Hi!

I'm writing application, which connects to CRM using CRM SDK 4. In the first version, I've been using app.config file (section 'connectionStrings')

<connectionStrings>
  <add name="mycrm" connectionString="Authentication Type=Integrated; Server=http://ServerName/OrganizationName;"/&gt;
</connectionStrings>

and (in code):

DataContext ctx = new DataContext("mycrm");

and it works fine. But now, application should let user to choose authentification type, type server name, user id, password, etc. I've created connection string, but I don't know how to use it. DataContext and CrmConnection objects, AFAIK, has only constructors with connectionString name (from app.config file) as parameter. When I try to use my connectionString I get exception:

    System.ApplicationException was unhandled

Message="Unable to load the connection string name 'Authentication Type=Integrated; Server=http://ServerName/OrganizationName;'" Source="Microsoft.Xrm.Client" StackTrace: at Microsoft.Xrm.Client.CrmConnection.GetConnectionStringFrom(String connectionStringName) at Microsoft.Xrm.Client.CrmConnection..ctor(String connectionStringName, String connectionString) at Microsoft.Xrm.Client.CrmConnection..ctor(String connectionStringName) at ARP.EstateExtensions.PaymentsUploader.MainWindows..ctor(String connectionString) in C:\Users\mrobaszynski\Desktop\PU\PaymentsUploader\MainWindows.xaml.cs:line 38 at ARP.EstateExtensions.PaymentsUploader.LoginWindow.bOK_Click(Object sender, RoutedEventArgs e) in C:\Users\mrobaszynski\Desktop\PU\PaymentsUploader\LoginWindow.xaml.cs:line 92 at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Application.RunInternal(Window window) at PaymentsUploader.App.Main() in C:\Users\mrobaszynski\Desktop\PU\PaymentsUploader\obj\Release\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.NullReferenceException Message="Object reference not set to an instance of an object." Source="Microsoft.Xrm.Client" StackTrace: at Microsoft.Xrm.Client.CrmConnection.GetConnectionStringFrom(String connectionStringName) InnerException:

+2  A: 

No, the other post is wrong - do NOT use reflection. The proper way to do this would be to use CrmConnection.Parse(customConnectionString) to build a crm connection. You can then construct the data context with that connection in the constructor.

var crm = new XrmDataContext(CrmConnection.Parse(customConnectionString));

Shan McArthur

www.shanmcarthur.net

Shan McArthur