Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the belwo code i want to generalize 'typeof' that's why i have designed to pass parameter 'typ' to 'typeof' operator . I don't have the idea of 'typ' variable but it's not working !!! So, how i can generalized the variable to be passed to 'typeof' operator and what is it's variable type !!!! My code :

C#
public static string BuildWhereCondition(Type typ, string columnName, string columnValue)
{
    string whereCondition = string.Empty;
    PropertyInfo columnInfo = typeof(typ).GetProperty(columnName);
    if (columnInfo != null)
    {
       if (columnInfo.PropertyType == typeof(int))
       {
           whereCondition = columnName + "=" + columnValue;
       }
       else if (columnInfo.PropertyType == typeof(String))
       {
           whereCondition = columnName + " LIKE '%" + columnValue + "%'";
       }
       else if (columnInfo.PropertyType == typeof(DateTime))
       {
           whereCondition = columnName + "='" + columnValue + "'";
       }
    }
    return whereCondition;
}

Thanks in advance .
Posted
Updated 29-Mar-12 21:52pm
v2
Comments
V. 30-Mar-12 3:52am    
added "pre" tags.

The typeof(typ) is Type. Try this:
if(typ.Equals(System.Int)) {
   // ...
}


However, concatenating sql command strings is a major risk in software. It would be better to add parameters to the SqlCommand. Check the link below:
http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/AddparameterstotheSqlCommand.htm[^]

Good luck!
 
Share this answer
 
v2
read this

typeof(c#)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900