找不到第0列

编程入门 行业动态 更新时间:2024-10-19 14:47:30
本文介绍了找不到第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列

本文发布于:2023-07-04 17:13:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!