请问谁能解决错误. [英] can any one solve error please.

查看:55
本文介绍了请问谁能解决错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共 字符串 [] GetCompletionList( int 计数)
    {
      //  DB cn = new DB(); 
        conn();
        DataTable dt =  DataTable();
        DataSet ds =  DataSet();
       
        尝试
        {
          
            字符串 text = "  + prefixText + " ;
            cmd =  SqlCommand("  +文字+  '",等等);
            da =  SqlDataAdapter(cmd);
            ds =  DataSet();
            da.Fill(ds);


          //  ds = cn.getDataSet(从reg中选择*,其中名字如'" +文本+'", reg"); 
        }
        捕获
        {
        }
        最终
        {
          

        }
        dt = ds.Tables [ 0 ];

        // 然后返回字符串列表(txtItems)作为结果
        List< string> txtItems = 列表< string>();
        字符串 dbValues;
        字符串 img;
        字符串电子邮件;
         foreach (数据行 in  dt.Rows中的行)
        {
            // 来自数据库的字符串(dbValues)

            dbValues = row [" ].ToString();
            
            //  dbValues = dbValues.ToLower(); 
            //  txtItems.Add(dbValues,); 

            img = row [" ].ToString();
           img = img.Remove( 0  2 );
            
            //  txtItems.Add(img); 
           
            
            txtItems.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dbValues.ToString(),img.ToString()));


           //  txtItems.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dbValues.ToString())); 
        }

        返回 txtItems.ToArray();

    }

   
    
} 




这是一个错误

 dt = ds.table [ 0 ]; 



Error msg : Cannot find table 0.

谁能解决........... !!

解决方案

首先检查您的数据库是否返回结果.最有可能数据库返回null
其次
为防止应用崩溃,您可以通过这种方式获取数据集...


 If(ds!= && ds.tables.count >   0 )
{
dt = ds.Tables [ 0 ]
} 


它似乎不返回任何表.

请确保您的连接正确,表存在并且返回表.
填充数据集时放置一个断点,并观察适配器和数据集内的值

尝试rhis代码:

 SqlDataAdapter sdaAdapter =  SqlDataAdapter();
 sdaAdapter.SelectCommand =  SqlCommand("  +文本+  '",等等);
 sdaAdapter.SelectCommand.CommandType = CommandType.Text;
DataTable dtTable =  DataTable();
sdaAdapter.Fill(dtTable);


public string[] GetCompletionList(string prefixText, int count)
    {
      //  DB cn = new DB();
        conn();
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
       
        try
        {
          
            string text = "%" + prefixText + "%";
            cmd = new SqlCommand("select * from product where name like '" + text + "'", con);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);


          //  ds = cn.getDataSet("select * from reg where Firstname like '" + text + "'", "reg");
        }
        catch
        {
        }
        finally
        {
          

        }
        dt = ds.Tables[0];

        //Then return List of string(txtItems) as result
        List<string> txtItems = new List<string>();
        String dbValues;
        String img;
        String email;
        foreach (DataRow row in dt.Rows)
        {
            //String From DataBase(dbValues)

            dbValues = row["Email"].ToString();
            
            //dbValues = dbValues.ToLower();
            //txtItems.Add(dbValues,);

            img = row["image"].ToString();
           img = img.Remove(0, 2);
            
            //txtItems.Add(img);
           
            
            txtItems.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dbValues.ToString(), img.ToString()));


           // txtItems.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dbValues.ToString()));
        }

        return txtItems.ToArray();

    }

   
    
}




an error is this

dt=ds.table[0];



Error msg : Cannot find table 0.

can any one solve it ...........!

解决方案

first of all check whether your DB is returning the results or not. its most likely that database is returning null
secondly
to prevent app crashing , you may get the dataset this way...


If(ds != null && ds.tables.count > 0)
{
dt = ds.Tables[0]
}


It looks like its not returning any tables.

Please make sure that your connection is proper, table exists and it returns table.
Put a breakpoint when filling into dataset and watch the values inside your adapter and dataset


Try rhis code :

  SqlDataAdapter sdaAdapter = new SqlDataAdapter();
 sdaAdapter.SelectCommand = new SqlCommand("select * from product where name like '" + text + "'", con);
 sdaAdapter.SelectCommand.CommandType =CommandType.Text;
DataTable dtTable = new DataTable();
sdaAdapter.Fill(dtTable);


这篇关于请问谁能解决错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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