none
Dealing with strings (methodology) RRS feed

  • Question

  •  

    Hello everybody! I'm trying hard with LINQ these days and everything is going like a charm. It's a wonderful technology and I'm recommending it to my friends. But I've found a problem that I cannot solve: There are fields with limited storage capacity, like nchars: The client application should know the maximum length so as not to let the users enter longer strings, for example, that could throw exceptions.

     

    What is the best technique to be aware of those limits in order to filter the incorrect entries?

     

    Thanks a lot!!

    Sunday, March 30, 2008 9:16 PM

Answers

  • Hi,

     

    checking for length errors is like any other validation logic. You can use the partial classes to this. In the Object Relational Designer you can select 'View Code' from the context menu of each Object or the DataContext and this will generate the partial classes. If you type in partial intelligence will offer you all partial methods which you can define. It works same as with override.

    In order to show the errors in the datagridview or using the errorprovider you have to implement the IDataErrorInfo interface. So the code may look like

    Code Snippet

    partial class Customer : IDataErrorInfo

    {

    private Dictionary<string, string> errorList = new Dictionary<string, string>();

    // I don't like string literals in my code

    private readonly string colCompanyName = "CompanyName";

     

    // helper method

    private void RemoveEntry(string columnName)

    {

    if (errorList.ContainsKey(columnName))

    errorList.Remove(columnName);

    }

     

    // helper method

    private void AddEntry(string columnName, string message)

    {

    if (errorList.ContainsKey(columnName))

    errorList[columnName] = message;

    else

    errorList.Add(columnName, message);

    }

     

    // helper method

    private bool HasError()

    {

    return (errorList.Count != 0);

    }

     

    // entered when SubmitChanges() is called

    partial void OnValidate(System.Data.Linq.ChangeAction action)

    {

    // revalidate all fields

    CheckCompanyName(this._CompanyName);

    }

     

    // entered when property CompanyName is about changing

    partial void OnCompanyNameChanging(string value)

    {

    CheckCompanyName(value);

    if (this.HasError)

    // prevent update to database

    throw new ApplicationException("errors exists");

    }

     

    // validationlogic for CompanyName

    private void CheckCompanyName(string value)

    {

    // clear old error

    RemoveEntry(colCompanyName);

    // validationlogic

    if (value.Length > 40)

    AddEntry(colCompanyName, "no more than 40 characters allowed");

    }

     

    #region IDataErrorInfo Members

    public string Error

    {

    get

    {

    if (this.HasError)

    return "There are errors";

    else

    string.Empty;

    }

    }

     

    public string this[string columnName]

    {

    get

    {

    if (errorList.ContainsKey(columnName))

    return errorList[columnName];

    else

    return string.Empty;

    }

    }

    #endregion

    }

     

     

    regards

    Philipp

    Sunday, March 30, 2008 10:11 PM

All replies

  • Hi,

     

    checking for length errors is like any other validation logic. You can use the partial classes to this. In the Object Relational Designer you can select 'View Code' from the context menu of each Object or the DataContext and this will generate the partial classes. If you type in partial intelligence will offer you all partial methods which you can define. It works same as with override.

    In order to show the errors in the datagridview or using the errorprovider you have to implement the IDataErrorInfo interface. So the code may look like

    Code Snippet

    partial class Customer : IDataErrorInfo

    {

    private Dictionary<string, string> errorList = new Dictionary<string, string>();

    // I don't like string literals in my code

    private readonly string colCompanyName = "CompanyName";

     

    // helper method

    private void RemoveEntry(string columnName)

    {

    if (errorList.ContainsKey(columnName))

    errorList.Remove(columnName);

    }

     

    // helper method

    private void AddEntry(string columnName, string message)

    {

    if (errorList.ContainsKey(columnName))

    errorList[columnName] = message;

    else

    errorList.Add(columnName, message);

    }

     

    // helper method

    private bool HasError()

    {

    return (errorList.Count != 0);

    }

     

    // entered when SubmitChanges() is called

    partial void OnValidate(System.Data.Linq.ChangeAction action)

    {

    // revalidate all fields

    CheckCompanyName(this._CompanyName);

    }

     

    // entered when property CompanyName is about changing

    partial void OnCompanyNameChanging(string value)

    {

    CheckCompanyName(value);

    if (this.HasError)

    // prevent update to database

    throw new ApplicationException("errors exists");

    }

     

    // validationlogic for CompanyName

    private void CheckCompanyName(string value)

    {

    // clear old error

    RemoveEntry(colCompanyName);

    // validationlogic

    if (value.Length > 40)

    AddEntry(colCompanyName, "no more than 40 characters allowed");

    }

     

    #region IDataErrorInfo Members

    public string Error

    {

    get

    {

    if (this.HasError)

    return "There are errors";

    else

    string.Empty;

    }

    }

     

    public string this[string columnName]

    {

    get

    {

    if (errorList.ContainsKey(columnName))

    return errorList[columnName];

    else

    return string.Empty;

    }

    }

    #endregion

    }

     

     

    regards

    Philipp

    Sunday, March 30, 2008 10:11 PM
  • In theory, it would be possible to look at the attributes that the LINQ to SQL designer places on string fields, parse the DbType property of the attribute, and figure out the field length that way.  That would save you from having to hard-code the lengths anywhere (else) in your application.   I haven't tried this myself (yet). 

     

    Tuesday, April 1, 2008 6:52 PM
  • PS You don't need to resort to reflection to find the attributes.  See this instead: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3085117&SiteID=1

    Wednesday, April 2, 2008 7:35 PM