找不到第0列 [英] Cannot find column 0

查看:98
本文介绍了找不到第0列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我的代码有什么问题它总是停在行[0] =选择IMO代码;它说无法找到第0列请帮助我们这里是我的代码



i don't know what's wrong in my code it always stop in row[0]="Select IMO Code"; and it says Cannot Find Column 0 please help me guys here's my code

private void LoadIMOcode()
        {
            CacheConnection conn = new CacheConnection(cacheconn);
            try
            {
                conn.Open();
                CacheDataAdapter da = new CacheDataAdapter("select STRING(Description,' (',HCode,')') as Description,HCode from CDTAB.HazardousCodes order by Description", conn);
                DataTable dt = new DataTable();
                DataRow row = dt.NewRow();
                row[0] = "Select IMO Code";
                row[1] = "";
                dt.Rows.InsertAt(row, 0);
                cmbIMOcode.DataSource = dt;
                cmbIMOcode.DisplayMember = "Description";
                cmbIMOcode.ValueMember = "HCode";
                cmbIMOcode.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }





我的尝试:



i尝试用不同的方式改变它我知道但它总是停在那里



What I have tried:

i tried change it in different ways i know but it always stop there

推荐答案

DataTable对象缺少列。这里是一个在插入行之前添加列的示例。



The DataTable object is missing column. Here an example to add column before insert row.

DataTable dt = new DataTable();
		dt.Columns.Add("Description", typeof(string));
		dt.Columns.Add("Code", typeof(string));


你似乎错过了调用执行查询并将数据加载到表中:

You seem to be missing the call to execute the query and load the data into the table:
DataTable dt = new DataTable();
da.Fill(dt); // <-- ADD THIS LINE
DataRow row = dt.NewRow();



这将自动为您创建表格列。



注意:永远不要这样做:


This will automatically create the table columns for you.

NB: Never do this:

catch (Exception ex)
{
    throw ex;
}

你刚刚抛弃了异常的堆栈跟踪,几乎无法确定哪一行抛出异常。



如果你真的必须重新抛出异常,只需使用 throw;

You've just thrown away the stack trace of the exception, making it almost impossible to work out which line threw the exception.

If you really must re-throw an exception, just use throw;:

catch (Exception ex)
{
    throw;
}

但是因为你没有做任何事情,你已经抓住了,所以首先没有必要抓住它。只需删除 try..catch 块。



[C#]在C#中正确地重新抛出异常(或.NET将军) - Tyler Fang的博客 [ ^ ]

But since you're not doing anything with the exception you've caught, there's no need to catch it in the first place. Just remove the try..catch block.

[C#] Properly Re-throwing Exceptions in C# (or .NET to be General) – Tyler Fang's Blog[^]


这篇关于找不到第0列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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