我不知道我的代码有什么问题它总是停在行[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列
发布评论