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:

QUESTION:
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))
    {
        conn.Open();
        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
   Retrieving Items from a Picklist via Javascript
Saleslogix is very light on how to use client side processes to do things in the Saleslogix web client.&n
Posted on Aug 29, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Saleslogix Xbar update coming soon
Scheduled to be released 8/29/2014 Features in Update •Installation improvements •Enterp
Posted on Aug 21, 2014 by Scott Weber to SalesLogix Product Blog
 
   How do you add a parameter for File Attachment Size and Timeout in Saleslogix 8.1?
Question: Is it possible to set a parameter for file attachment size and timeout in Salelogix? If
Posted on Aug 07, 2014 by Dale Richter to SalesLogix Questions & Answers
 
   Fixing Duplicate Saleslogix FieldIndex Values in the SECTABLEDEFS
I had to fix a client's SECTABLEDEFS table that contained duplicate FieldIndex values.  FieldInd
Posted on Aug 07, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Saleslogix XBar Installation
When installing the new XBar application I found the instructions a little vague so I thought I would pas
Posted on Jul 18, 2014 by Mark Duthoy to SalesLogix Support
 
Comments

 

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.

-Ryan

May 21, 2008 7:31 PM
 

kenp said:

Perfect... Thank Ryan

May 22, 2008 5:32 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
All contents Copyright © 2014 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