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
   Determining User Rights in the Infor CRM (Formerly Saleslogix) LAN Client
I recently had a client ask how to get a query to tell which users have rights to add, edit, and delete v
Posted on Apr 03, 2015 by Kris Halsrud to Kris Halsrud's Blog
   How do I wrap text in Textboxes in Infor CRM (formerly Saleslogix)?
Recently an Infor CRM administrator contacted me in regards to many of their textboxes for Notes in the I
Posted on Apr 01, 2015 by Dale Richter to Infor CRM Questions & Answers
   InforCRM (formerly Saleslogix) Mobile - Clearing Out Earlier Customizations
If you started with an earlier version of SalesLogix Mobile, let's say version 7.5.4, and customized
Posted on Mar 30, 2015 by Mark Duthoy to SalesLogix Support
   How do you set the Base Directory when Deploying your Web Client on Infor CRM 8.1?
Question:How do I make sure my Base Directory is set correctly on Infor CRM ? Recently I had a custo
Posted on Mar 06, 2015 by Dale Richter to Infor CRM Questions & Answers
   How do I fix a corrupt Picklist in Infor CRM version 8.1?
I had a user ask how to fix a corrupt picklist. The picklist did not show any values when trying to e
Posted on Feb 06, 2015 by Dale Richter to Infor CRM Questions & Answers


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