今天在使用 AutoCompleteExtender 自动完成控件时发现一个问题,也许这是微软的一个 bug 吧。如果返回的数组是纯数字,那么将变成 undefined或 null。public string[] GetUsers(string prefixText, int count) //兩個參數分別是輸入文本跟提示項數
    {
    string[] items = new string[count];
    SqlConnection myCon = new SqlConnection("Data Source=**;Initial Catalog=**;User ID=**;Password=**"); //數據庫連接
        myCon.Open();  //打開數據庫連接 select * from authors
       SqlCommand myCmd = new SqlCommand("select top " + count + " * from authors where ziplike '" + prefixText + "%'", myCon);
        SqlDataReader myDR = myCmd.ExecuteReader();
        string auid;
        int i = 0;
        while (myDR.Read())
        {
            auid = myDR["zip"].ToString().Trim();
            items[i++]=(auid);
        }
        myCon.Close();                                      //關閉數據庫連接
        return items;
    }
由於查到的郵編(ZIP)是純數字就出現了undefined的現象
我不清楚为什么数字字符串会变成 undefined 和 null,那又要如何解決這一問題
跪求高手們不吝賜教,100分相送

解决方案 »

  1.   

    出現null的原因我知道了,主要是undefined的現象
      

  2.   

    null 是这个地方auid = myDR["zip"].ToString().Trim();  tostring以前先判断一下吧
    return items; 返回的这个里面是纯数字数组就会出现楼住说的情况吗?
      

  3.   

    myDR["zip"]的值为null是不允许.tostring()的,
      

  4.   

    SqlCommand myCmd = new SqlCommand("select top " + count + " * from authors where ziplike  '" + prefixText + "% '", myCon); 
    此句不对应为:
    SqlCommand myCmd = new SqlCommand("select top " + count + " * from authors where zip like  '" + prefixText + "% '", myCon); 
    zip与like 间应空格
      

  5.   

    現象是這樣的:假設zip字段有如下記錄
    123455
    123458
    123uie
    1iwi58
    那麼當我在與AutoCompleteExtender綁定的TxexBox中輸入"1"(不含引號)時彈出的自動完成下拉列表的數据就成了下面這樣的四個:
    undefined
    undefined
    123uie
    1iwi58
    而沒有出現我想要的結果
    123455
    123458
    123uie
    1iwi58
      

  6.   

    為null的原因是因為在數據表中查不到所要求的count個,假設count設定為5,以上面的為例輸入1 后就會出現:
    undefined 
    undefined 
    123uie 
    1iwi58 
    null
    因為數據表中只有四條符合以1開頭的要求,而count強制要求5個所以就有null的現象
      

  7.   

    楼主你的问题不是难,而是奇怪.按理说,zip Like '1%'都会把你第一个字符为"1"的记录取出来,但你现在取到的却是undefined,你看一下,在Sql查询分析器里执行:
    select top 5 * from authors where zip like '1%'如果也如你上面所说,就要检查你zip具体内容了.如果正确,就检查你取数据的语法.  
      

  8.   

    在查詢分析器里有,設定斷點時auid的值也是正確的,但一到界面上就變成了undefined,苦惱啊
      

  9.   

    是不是类型 需要转换一下 where convert(ziplike,varchar(200)) like '..........  
      

  10.   

    select top 5 convert(varchar(10),zip) as zip from test where convert(varchar(10),zip) like '1%'
    我改成這樣也不行,問題依然如故
      

  11.   


    SqlCommand myCmd = new SqlCommand("select top " + count + " * from authors where ziplike   '" + prefixText + "%  '", myCon);  
    此句不对应为: 
    SqlCommand myCmd = new SqlCommand("select top " + count + " * from authors where zip like   '" + prefixText + "%  '", myCon);  
    zip与like 间应空格==========================================
    SqlCommand myCmd = new SqlCommand("select top " + count + " * from authors where zip like   '%" + prefixText + "%  '", myCon); 
     
      

  12.   

    那里是我拷貝是出錯了,我的代碼里是分開的,現在是能查到數据,但若查到的數据為純數字時就會出現undefined,媽的csdn怎麼不能貼圖咯
      

  13.   

    1、Count是用来拼写在sql语句中的,不是用来声明数组的。建议你使用ArrayList来保存你需要的结果。这样能够避免产生null。或者使用DataTable获取结果,然后根据DataTable.Rows.Count声明数组。
    2、我没用过AutoCompleteExtender,是不是有些格式化的内容。
      

  14.   

    我再貼一次代碼好了:
    using System;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Collections.Generic;               //System.Collections.Generic是List的命名空間
    using System.Data.SqlClient;
    using System.Web.Script.Services;[WebService(Namespace = "AjaxControlToolkit")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]                          //用於調用web service標簽
    public class WebService : System.Web.Services.WebService
    {
        public WebService() { }
        [WebMethod]
        public string[] GetUsers(string prefixText, int count)    //兩個參數分別是輸入文本跟提示項數
        {
            List<string> items = new List<string>(count);    
            SqlConnection myCon = new SqlConnection("Data Source=**;Initial Catalog=**;User ID=**;Password=**");//數據庫連接
            myCon.Open();                                          //打開數據庫連接
            SqlCommand myCmd = new SqlCommand("select top " + count + " convert(varchar(10),zip) as zip from test where convert(varchar(10),zip) like '" + prefixText + "%'", myCon);
            //SqlCommand myCmd = new SqlCommand("select top " + count + " * from test where zip like '9%'", myCon);
            SqlDataReader myDR = myCmd.ExecuteReader();
            string auid;
            while (myDR.Read())
            {
                auid = myDR["zip"].ToString().Trim();
                items.Add(auid);
            }
            myCon.Close();                                      //關閉數據庫連接
            return items.ToArray();
        }}
      

  15.   

    List <string > items = new List <string >(count);   
    这句话改为
    List <string > items = new List <string >();   
      

  16.   

    試了一下沒用,還是原樣的,只要zip字段之值為純數字就有問題
    改為:
    auid = myDR["zip"].ToString().Trim()+"-"; //或其它字符(空格不行)
    則正常,只是每一個后面都帶個尾巴,
    123455- 
    123458-
    123uie- 
    1iwi58- 
      

  17.   

    应该是类型转换问题,数组初始化后未值引起
            while (myDR.Read()) 
            { 
                auid = myDR["zip"].ToString().Trim(); 
                items[i++]=(auid); 
            } 
    改成 items[i++] = " "+auid;
    试试
      

  18.   

    还有一种解决方案是给item数组预赋初值
      

  19.   

    在前面加空格試過了,不行,如果是預賦初值那得到的結果就會出現我不想要的數据
    煩哪,看到www.google.cn里面輸入時就會發現它即使輸入的為純數字也不會發生這種問題
      

  20.   

    1、
    楼主,你的代码看不出有什么问题,测试也没问题.问题应该出在环境上.
    [WebService(Namespace = "AjaxControlToolkit")] 
    你的这句是什么意思???你把该句改成默认的:[WebService(Namespace = "http://tempuri.org/")]
    试试!!!2、
    另外,既然你返回的是字符串数组,直接声明一个字符串数组即可,感觉没必要用泛型:
    string[] items=new string[count];然后赋值的时候:
    SqlDataReader myDR = myCmd.ExecuteReader(); 
            int i=0;
            while (myDR.Read()) 
            { 
                items[i] = myDR["zip"].ToString().Trim(); 
                i++;
            } return items;
      

  21.   

    呀,又能回复了,我刚才给你发了站内消息你试一下,在数字的两边加个单引号把
     items.Add(auid); 
    改成
     items.Add("'" + auid + "'"); 
    看看告诉我一下结果
      

  22.   

    解決了,非常感謝happycharles 的幫助,十分感謝,本來100分應該都給你,可csdn加分要求超過五天,加不了分
    只能分一點給其它也出過力的。
    正確的代碼再貼一次:
    using System;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Collections.Generic;               //System.Collections.Generic是List的命名空間
    using System.Data.SqlClient;
    using System.Web.Script.Services;[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]                          //用於調用web service標簽
    public class WebService : System.Web.Services.WebService
    {
        public WebService() { }
        [WebMethod]
        public string[] GetUsers(string prefixText, int count)      //兩個參數分別是輸入文本跟提示項數
        {
            List<string> items = new List<string>(count); 
            SqlConnection myCon = new SqlConnection("Data Source=**;Initial Catalog=**;User ID=**;Password=**"); 
            myCon.Open();                                               //打開數據庫連接
            SqlCommand myCmd = new SqlCommand("select top " + count + " convert(varchar(10),zip) as zip from test where convert(varchar(10),zip) like '" + prefixText + "%'", myCon);
            SqlDataReader myDR = myCmd.ExecuteReader();
            string auid;
            //int i = 0;
            while (myDR.Read())
            {
                auid = myDR["zip"].ToString().Trim();
                auid = "'" + auid + "'";
                items.Add(auid);
            }
            myCon.Close();                                      //關閉數據庫連接
             return items.ToArray();
          }
    }