Login / Register  search  syndication  about

          Ryan Farley's Blog

Ryan Farley on .NET Development with a focus on CRM Development for SalesLogix

GetField Equaivalent for C# using Generics

Many people who customize SalesLogix have become accustomed to using the built in "SLX Database Support" script to provide things like the quick and easy GetField function to return a single database value. If you've ventured into the .NET Extensions realm, you might want an easy equivalent. The following is a repost of something I posted around a year ago in the Sage Business Partner newsgroups:

Does anyone have a generic GetField function for C# .Net? One that can handle N DataTypes, Nulls, DbNull, etc.? I am playing with the SLX VB script function, and it doesn't adapt well to the strongly typed fields of C# .NET. Specifically the ExecuteScalar() blows up on 1) TheValue has no type 2) can't handle nulls 3) can't handle int....Any ideas or do I need specific GetFieldInt, GetFieldDateTime, GetFieldString stuff?

This is why we have generics! You could implement the GetField method as method that returns a generic type. That would be the best way to do it! Here's a quick sample:

public T GetField<T>(string Field, string Table, string Where)
    string sql = string.Format("select {0} from {1} where {2}", Field, Table, (Where.Equals(string.Empty) ? "1=1" : Where));
    using (OleDbConnection conn = new OleDbConnection(SlxConnectionString))
        using (OleDbCommand cmd = new OleDbCommand(sql, conn))
            object fieldval = cmd.ExecuteScalar();
            return fieldval == DBNull.Value ? default(T) : (T)fieldval;

Now to use it, you'll specify the type like this:

// for a string field
string accname = GetField<string>("account", "account", "accountid = '" + accid + "'");
// or for an int field
int numemp = GetField<int>("employees", "account", "accountid = '" + accid + "'");
//or for a datetime field
DateTime createdon = GetField<DateTime>("createdate", "account", "accountid = '" + accid + "'");

Get the idea? How's that for cool?! Smile


What's This?
Bookmark and Share

About Ryan Farley

   Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix since 2001 and believes in sharing with the community. He loves C#, Javascript, Python, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

View the SalesLogix Mobile Seveloper Series
View the SalesLogix SData Developer Series
View the Git for the SalesLogix Developer series

Related Content
   Can I Bind an Attachment to a Specific Activity in Infor CRM version 7.54?
Recently I had a user ask if they could bind an attachment to a specific Activity within Infor CRM??
Posted on Jul 07, 2015 by Dale Richter to Infor CRM Questions & Answers
   Infor CRM (formerly SalesLogix) Administrator Locked Out
I thought I had seen all the issues affecting the datalink but this was a new one. We could not login t
Posted on Jun 18, 2015 by Mark Duthoy to SalesLogix Support
   How do I remove users from my Infor CRM system (formerly SalesLogix)?
I had an Administrator ask me for the steps they needed to take in order to remove a user from the Infor
Posted on May 08, 2015 by Dale Richter to Infor CRM Questions & Answers
   Infor CRM (Formerly Saleslogix) v8.1 update 03 and 05- Uncaught Error Scheduling Contact Processes
 In the Infor CRM (formerly Saleslogix) web client, under the Contact area you have the ability to s
Posted on May 07, 2015 by Kris Halsrud to Kris Halsrud's Blog
   Problem Showing All Area, Category, and Issue Values in Infor CRM (Saleslogix) Mobile And How to Fix
There is an issue with the Infor CRM (Saleslogix) Mobile client where, if you have a large list of Area,
Posted on Apr 24, 2015 by Ryan Farley to Ryan Farley's Blog


kenp said:

Still ramping up on the whole .net world so forgive me if this should be obvious...

Where do I put the GetField 'generic' example so that I can access it though C# snippets on the form. Is there a place where I can put it so it would be availble to all forms?

May 21, 2008 7:17 PM

Ryan Farley said:

Hi Ken.

I assume you're referring to SalesLogix Web? Right? This sample code isn't really intended for use there as there are better ways to do this there. This is intended for a GetField-like relacement for C# code working with the LAN client (such as in a .NET Extension).

However, for adding any code that is available for all forms on SLX web, you'd compile the code into a DLL and then add that into the bin folder for the site (in the support files). Then you'd have it available anywhere - although to use it from a business rule you'd need to add a reference for the DLL.


May 21, 2008 7:31 PM

kenp said:

Perfect... Thank Ryan

May 22, 2008 5:32 PM

Leave a Comment

All contents Copyright © 2015 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783

  Follow @CustomerFX on twitter
Follow the best news, tips, and articles
  Subscribe to Customer FX on youtube
Watch SalesLogix tutorial videos from Customer FX
Login / Register