开发者

How to get the datatype of value entered by User

Eg:TableName.ColumnName==Value.

I have the datatype of the columnName but the val开发者_StackOverflow社区ue is entered by the user, which can be int/string/datetime or any value.

I need to check the datatype of columnName and value.The code should work for both windows and web application


Unless the user tells you what the data type is, you will only be able to determine it by trying to parse it to different types, and even then you are not guaranteed to get it right.

Using the different TryParse methods that are defined on the int, double, DateTime and other types in the BCL you can see if the passed in string is parseable to one of more of these types.

At this point you will need to decide which type you want to use... Since you already know the datatype of the column, you can test to see if you can parse to that type and if not, reject the input.

In all cases, the value will be a valid string (as entered by the user), as this is the type of the Text property of all controls.


if ( Value is string)
  //code to handle
else if (value is int)
//code to handle

etc.


Unless you come up with the way how to recognize the entered value is int, string or datetime, there is no other way. As I understand user enters the string always. I would use RegEx maybe??? if you are quite clear how date-times / int / string differ from each-other.


Rather than giving an answer on how to solve the specific issue at hand, it seems it could be avoided altogether by making a better design decision on how users input values into that field.

You say columName has a specific datatype but you are allowing the user to input any value in the presentation layer? This seems to have a code smell.

Can you not enforce some form of validation at the UI layer. I can't think of any instances where I would allow a user to input any value into field that should conform to a specific datatype. I'd validate the input based on the chosen columnName type.


Your question is not very clear, but I'm guessing that your talking about casting a variable to specific type.

In your case, say TableName.ColumnName is an integer, you would do something like this:

string valueByUser = "23"; // Wherever this comes from.

int number = 0;

int.TryParse(valueByUser, out number);

TableName.ColumnName = number;

Since you will know what the data type of ColumnName is, you can use the appropriate TryParse to conver the value e.g.

decimal.TryParse()
DateTime.TryParse()


This is what I use:

 private string ParseString(string str)
        {

            bool boolValue;
            Int32 intValue;
            Int64 bigintValue;
            decimal doubleValue;
            DateTime dateValue;

            // Place checks higher in if-else statement to give higher priority to type.

            if (bool.TryParse(str, out boolValue))
                return "System_Boolean";
            else if (Int32.TryParse(str, out intValue))
                return "System_Int32";
            else if (Int64.TryParse(str, out bigintValue))
                return "System_Int64";
            else if (decimal.TryParse(str, out doubleValue))
                return "System_Decimal";
            else if (DateTime.TryParse(str, out dateValue))
                return "System_DateTime";
            else return "System_String";

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜