locked
Failed to convert parameter value from a String to a IEnumerable`1. RRS feed

  • Question

  • User1888523642 posted

    Hi,

    I am saving coordinates to MS SQL database with the following code:

                SqlParameter param12 = new SqlParameter("@gLatitude", SqlDbType.Udt);
                param12.UdtTypeName = "geography";
                param12.Value = txtLatitude.Text;
                cmd.Parameters.Add(param12);
                param12.SqlDbType = SqlDbType.Structured;

                SqlParameter param13 = new SqlParameter("@gLongitude", SqlDbType.Udt);
                param13.UdtTypeName = "geography";
                param13.Value = txtLongitude.Text;
                cmd.Parameters.Add(param13);
                param13.SqlDbType = SqlDbType.Structured;

    When I submit the form I get the error:  Failed to convert parameter value from a String to a IEnumerable`1. Could you please help me how I can convert textbox to IEnumerable?

    Thanks

    Thursday, June 4, 2015 2:39 PM

Answers

  • User281315223 posted

    What do your actual parameter values look like (e.g. what is the value in your Latitude and Longitude textboxes)? You might be able to use the DbGeography.PointFromText() method as mentioned in Rick Strahls' blog post here, which might look something like this :

    SqlParameter param12 = new SqlParameter("@gLatitude", SqlDbType.Udt);
    param12.UdtTypeName = "geography";
    param12.Value = DbGeography.PointFromText(txtLatitude.Text);
    param12.SqlDbType = SqlDbType.Structured;
    cmd.Parameters.Add(param12);
    
    SqlParameter param13 = new SqlParameter("@gLongitude", SqlDbType.Udt);
    param13.UdtTypeName = "geography";
    param13.Value = DbGeography.PointFromText(txtLongitude.Text);
    param13.SqlDbType = SqlDbType.Structured;
    cmd.Parameters.Add(param13);
                

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 3:54 PM
  • User-1910946339 posted

    A 'geography' instance is not a single number, at a minimum it represents a point on the surface of a sphere.  Try googling 'SQLServer geography' to see lots of examples of how to convert lat/long to geography.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 7:29 PM
  • User1888523642 posted

    Thank you for your replies Rion and Paul. Rion, txtLatitude has the value: 52.2327122 and txtLongitude: -2.2697221. The post you shared looks fantastic. I haven't worked in Entity Framework before but hopefully it won't be much trouble. Going to read the post now. thanks again! 

    Paul, thanks for pointing that out. I'm currently only saving one coordinate. I'll test by adding both coordinates in one column. 

    This post looks helpful as well: http://gis.stackexchange.com/questions/29236/insert-geography-data-into-sql-server-2008

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 5, 2015 1:13 AM

All replies

  • User281315223 posted

    What do your actual parameter values look like (e.g. what is the value in your Latitude and Longitude textboxes)? You might be able to use the DbGeography.PointFromText() method as mentioned in Rick Strahls' blog post here, which might look something like this :

    SqlParameter param12 = new SqlParameter("@gLatitude", SqlDbType.Udt);
    param12.UdtTypeName = "geography";
    param12.Value = DbGeography.PointFromText(txtLatitude.Text);
    param12.SqlDbType = SqlDbType.Structured;
    cmd.Parameters.Add(param12);
    
    SqlParameter param13 = new SqlParameter("@gLongitude", SqlDbType.Udt);
    param13.UdtTypeName = "geography";
    param13.Value = DbGeography.PointFromText(txtLongitude.Text);
    param13.SqlDbType = SqlDbType.Structured;
    cmd.Parameters.Add(param13);
                

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 3:54 PM
  • User-1910946339 posted

    A 'geography' instance is not a single number, at a minimum it represents a point on the surface of a sphere.  Try googling 'SQLServer geography' to see lots of examples of how to convert lat/long to geography.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 7:29 PM
  • User1888523642 posted

    Thank you for your replies Rion and Paul. Rion, txtLatitude has the value: 52.2327122 and txtLongitude: -2.2697221. The post you shared looks fantastic. I haven't worked in Entity Framework before but hopefully it won't be much trouble. Going to read the post now. thanks again! 

    Paul, thanks for pointing that out. I'm currently only saving one coordinate. I'll test by adding both coordinates in one column. 

    This post looks helpful as well: http://gis.stackexchange.com/questions/29236/insert-geography-data-into-sql-server-2008

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 5, 2015 1:13 AM