none
Error: "string must be exactly one character long"???? RRS feed

  • Question

  • I'm getting this error message when running a LINQtoSQL query. My entity classes are as follows:

    [Table(Name="Activities")]
        public partial class Activity
        {
            #region Fields
            [Column(IsPrimaryKey = true, DbType="SmallDateTime NOT NULL")]
            public DateTime Activity_Date { get; set; }

            [Column(IsPrimaryKey = true, DbType = "Char(1) NOT NULL", CanBeNull=false)]
            public char Activity_Type { get; set; }

            [Column(DbType="TinyInt NOT NULL")]
            public byte Activity_Distance {get; set; }

            [Column(DbType = "Char(10) NOT NULL", CanBeNull=false)]
            public string Activity_Duration {get; set; }

            [Column(DbType = "Char(35) NOT NULL", CanBeNull=false)]
            public string Activity_Route {get; set; }
            #endregion

            private EntityRef<Type> _Type;

            [Association(Name = "Type_Activity", Storage = "_Type",
                        ThisKey = "Activity_Type", OtherKey = "Type_Id", IsForeignKey = true, DeleteRule = "NO ACTION")]

            public Type Type
            {
                get { return this._Type.Entity; }
                set { this._Type.Entity = value; }
            }
        }

        [Table(Name="Types")]
        public partial class Type
        {
            #region Fields
            [Column(IsPrimaryKey = true, DbType = "Char(1) NOT NULL", CanBeNull = false)]
            public char Type_Id { get; set; }

            [Column(DbType = "Char(8) NOT NULL", CanBeNull = false)]
            public char Type_Desc { get; set; }
            #endregion

            private EntitySet<Activity> _Activities;

            [Association(Storage = "_Activities", ThisKey = "Type_Id", OtherKey = "Activity_Type")]

            public EntitySet<Activity> Activities
            {
                get { return this._Activities; }
                set { this._Activities.Assign(value); }

    I am joining on Activities.Activity_Type and Types.Type_Id.

    I'd appreciate some guidance on how to overcome this error because I can't see what I may have done incorrectly.

    I don't know if it's relevant but my query is as follows:

    private static void GetAct_Types(DBDataContext db)
            {
                var act_types =
                    from a in db.Activities
                    from t in db.Types
                    where a.Activity_Type == t.Type_Id
                    select new { a.Activity_Date,
                                 a.Activity_Type,
                                 t.Type_Desc,
                                 a.Activity_Distance,
                                 a.Activity_Duration,
                                 a.Activity_Route};

                foreach (var row in act_types)
                {
                    Console.WriteLine("{0}\t; {1}\t; {2}\t; {3}\t; {4}",
                                       row.Activity_Date,
                                       row.Type_Desc,
                                       row.Activity_Distance,
                                       row.Activity_Duration,
                                       row.Activity_Route);
                }
            }


            }
        }

    Friday, October 10, 2008 4:16 AM

Answers

  • Two issues:

     

    1)         [Column(DbType = "Char(8) NOT NULL", CanBeNull = false)]
            public char Type_Desc { get; set; }

     

    ...should be "public string Type_Desc" since the underlying column in the db is a char(8)... I think this is the cause of the error you're seeing. (trying to stick more than 1 character into a char).

     

    2) Although it is allowed, perfectly valid, and not the cause of the error you're seeing - I would recommend avoiding naming a class "Type". Even if it is in your own namespace, using the same class name as a common framework class is guaranteed to cause confusion at some point. (System.Type vs MyOwnNamespace.Type)
    Friday, October 10, 2008 6:11 AM
    Answerer

All replies

  • Two issues:

     

    1)         [Column(DbType = "Char(8) NOT NULL", CanBeNull = false)]
            public char Type_Desc { get; set; }

     

    ...should be "public string Type_Desc" since the underlying column in the db is a char(8)... I think this is the cause of the error you're seeing. (trying to stick more than 1 character into a char).

     

    2) Although it is allowed, perfectly valid, and not the cause of the error you're seeing - I would recommend avoiding naming a class "Type". Even if it is in your own namespace, using the same class name as a common framework class is guaranteed to cause confusion at some point. (System.Type vs MyOwnNamespace.Type)
    Friday, October 10, 2008 6:11 AM
    Answerer
  • KristoferA: thanks - I was looking in the wrong place and missed an obvious typo! Correcting it allows the query to run as designed.

    I understand your comment regarding the naming of the class 'Type' and will avoid it in future.
    Saturday, October 11, 2008 1:45 AM