Re:索引和长度必须引用string.Parameter中的位置:length [英] Re: Index and length must refer to a location within the string.Parameter name: length

查看:89
本文介绍了Re:索引和长度必须引用string.Parameter中的位置:length的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

code:

protected void Page_Load(object sender, EventArgs e)
    {
        string markers = GetMarkers();
        Literal1.Text = @"
     <script type='text/javascript'>
     function initialize() {
 
     var mapOptions = {
     center: new google.maps.LatLng(28.3213, 77.5435),
     zoom: 2,
     mapTypeId: google.maps.MapTypeId.ROADMAP
     };
 
     var myMap = new google.maps.Map(document.getElementById('mapArea'),
     mapOptions);"
        + markers +
        @"}
     </script>";
    }







 public double ConvertDegree(string input)
   {
       double sd = 0.0;
       double min = 0.0;
       double sec = 0.0;
       double deg = 0.0;

       string direction = input.Substring(0, 1);  // Error: Index and length must refer to a location within the string.Parameter name: length
       string sign = "";
       if ((direction.ToUpper() == "S") || (direction.ToUpper() == "W"))
       {
           sign = "-";
       }

       string[] arr = input.Split(new char[] { ' ' });
       min = Convert.ToDouble(arr[2]);
       string[] arr1 = arr[3].Split(new char[] { '.' });
       sec = Convert.ToDouble(arr1[0]);
       deg = Convert.ToDouble(arr[1]);
       min = min / ((double)60);
       sec = sec / ((double)3600);
       sd = deg + min + sec;
       if (!(string.IsNullOrEmpty(sign)))
       {
           sd = sd * (-1);
       }
       sd = Math.Round(sd, 6);
       string sdnew = Convert.ToString(sd);
       string sdnew1 = "";
       sdnew1 = string.Format("{0:0.000000}", sd);

       double manu = Convert.ToDouble(sdnew1.ToString());
       return manu;
   }

   protected string GetMarkers()
   {
       string markers = "";
       using (SqlConnection con = new
       SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HostelConnectionString"].ConnectionString))
       {
           SqlCommand cmd = new SqlCommand("select hi.Hostelcd, hi.Lattitude,hi.Longitude,h.HostelName from HostelDetails h left outer join Hostelinformation hi on h.Hostelcd = hi.Hostelcd order by hi.Hostelcd desc", con);
           con.Open();
           SqlDataReader reader = cmd.ExecuteReader();
           int i = 0;

           while (reader.Read())
           {
               i++;


               markers +=
               @"var marker" + i.ToString() + @" = new google.maps.Marker({
             position: new google.maps.LatLng(" + ConvertDegree(reader["Lattitude"].ToString()) + ", " +
               ConvertDegree(reader["Longitude"].ToString()) + ")," +
               @"map: myMap,
             title:'" + reader["HostelName"].ToString() + "'});";
           }
       }
       return markers;
   }





在我的数据库中,纬度和经度存储为例如:N 16 53 55 E 77 14 27



请为此运行时错误提供解决方案。



In my database lattitude and longitude are stored as Eg: N 16 53 55 E 77 14 27

Pls give the solution for this runtime error.

推荐答案

使用..



i认为输入的参数值为空或空字符串..

试试这个..

Use..

i think your parameter value for input gets null or empty string..
try this..
if(String.IsNullOrEmpty(input))
{
input="hello";
}
string direction = input.Substring(0, 1);


检查您的数据。

由于错误发生在尝试从字符串开头提取单个字符的substring命令,似乎至少有一个从数据库返回的值是空字符串:或数据库列中的NULL,在经度中或纬度 - 我们无法分辨哪一个。



但是你为什么要将它存储为字符串数据呢?要做任何有用的事情你需要它作为一个双 - 所以为什么不在你进行数据输入时将其转换为,然后只是读出来?在您真正需要时节省大量时间和精力......
Check your data.
Since the error is occurring on a substring command which tried to extract a single character starting at the beginning of the string, it would appear that at least one of the values returned from your DB is an empty string: "" or a NULL in your DB column, either in the Longitude or Latitude - we can't tell which.

But why are you storing this as string data anyway? To do anything useful with it you need it as a double - so why not convert it to that when you do the data entry, and then just read it out? Saves a lot of time and effort when you actually need it...


这篇关于Re:索引和长度必须引用string.Parameter中的位置:length的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆