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
   Mobile 3.0.3 for Saleslogix v7.5.4, v8.0.0 & v8.1.0 is now available!
Saleslogix Mobile v3.0.3 is now available in the customer portals. This update addresses all issues int
Posted on Apr 14, 2014 by Scott Weber to SalesLogix Product Blog
 
   Editing Filters in Saleslogix Web Client 8.0
Is it possible to add or remove Saleslogix filters from the List view in Accounts?
Posted on Apr 08, 2014 by SalesLogix Support to SalesLogix Questions & Answers
 
   Creating a Unique Saleslogix table ID in a SQL Function
A couple of months ago Ryan Farley wrote an article about how to use a SQL stored procedure to create a S
Posted on Mar 25, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Saleslogix Integration with Google - WOW!
Saleslogix will be releasing their Google Integration this month and from what I have seen, it is fantast
Posted on Mar 18, 2014 by Scott Weber to SalesLogix Product Blog
 
   Global Joins in the Saleslogix Web Client
Question: How can I make a Global Join stick in the Saleslogix Web Client? Currently, when I create a
Posted on Mar 07, 2014 by SalesLogix Support to SalesLogix Questions & Answers
 
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