Search code examples
c#mysqlgettype

How can i get the system type of a value i get from a form/string?


i have never tried something like this so i need your help.

i have the following code:

    public int AddChannel(NameValueCollection FormValues)
    {
        string Keys = string.Join(",",FormValues.AllKeys);
        string Values = string.Join(",", FormValues.AllKeys.Select(key => String.Format("\"{0}\"", HttpContext.Current.Server.HtmlEncode(FormValues[key]))));

        return InsertQuery("INSERT INTO channels (" + Keys + ") VALUES (" + Values + ");");
    }

while processing, some fields are supposed to be integers otherwise, the mysql query fails. how can i check and convert KeyValue into the correct type?


Solution

  • In short, none. The data coming in from a http post is text. You could try and infer that 123 is probably a number, but that isn't safe, as "123" is a perfectly valid string. Personally, I'd hope that the code knows the schema ahead of time, or can access the schema. And from the schema, apply the correct conversions (and then add the values via parameters of the appropriate types).