Vb.net ado indexwasofof range [英] Vb.net ado indexwasoutof range

查看:105
本文介绍了Vb.net ado indexwasofof range的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将我的数据集显示在Datagridview上,但这让我感到困扰。



你怎么解释这个?任何修复?



I want display my dataset onto Datagridview but this bothers me.

How could you explain this? Any fix?

Private Sub dgvOutput()

       With dgvDisplay


           .Columns(1).HeaderCell.Value = "ID"
           .Columns(1).Width = 30
           .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
           .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

           .Columns(1).HeaderCell.Value = "Place"
           .Columns(1).Width = 100
           .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

           .Columns(2).HeaderCell.Value = "Facility"
           .Columns(2).Width = 100
           .Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

           .Columns(3).HeaderCell.Value = "Date Reserved"
           .Columns(3).Width = 200
           .Columns(3).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

           .Columns(4).HeaderCell.Value = "Time Start"
           .Columns(4).Width = 100
           .Columns(4).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

           .Columns(5).HeaderCell.Value = "Time End"
           .Columns(5).Width = 100
           .Columns(5).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

           .Columns(6).HeaderCell.Value = "Requestor"
           .Columns(6).Width = 80
           .Columns(6).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

           .Columns(7).HeaderCell.Value = "Price"
           .Columns(7).Width = 70
           .Columns(7).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

           .Columns(8).HeaderCell.Value = "Note"
           .Columns(8).Width = 180
           .Columns(8).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter


       End With

   End Sub

   Private Sub FillTable()
       Dim adapter As New OleDbDataAdapter
       Dim dt As New DataTable

       adapter.SelectCommand = command
       adapter.Fill(dt)
       dgvDisplay.DataSource = dt
       dgvOutput()
       adapter.Dispose()
       command.Dispose()
       conn.Close()

   End Sub





我尝试了什么:



我试图更改列和它的索引,但仍然没有运气



我应该需要de clare列数?



或任何特定代码。



如果你想检查我的整个代码



您可以给我发消息:[已删除电子邮件]



约项目文件大小:1.5mb



What I have tried:

I've tried to change columns and its indexes but still no luck

should I need to declare columns count?

or any specific codes.

if you want to check my whole code

you may message me: [Email removed]

approx project file size: 1.5mb

推荐答案

Quote:

Vb.net ado indexwasofof range

Vb.net ado indexwasoutof range

2种可能性:< br $>
- dgvDisplay.DataSource = dt 未能返回预期的对象。

-列数少于预期。

在所有情况下,检查你有你期望的。



使用调试器来查看错误。



调试器允许你逐行跟踪执行,检查变量,你会发现有一点它会停止你所期望的。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器这里是为了向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

如果代码没有达到预期的效果,那么您就接近于错误。

2 possibilities:
- either dgvDisplay.DataSource = dt failed to return the object expected.
-either there is fewer columns than expected.
in all cases, check that you have what you expect.

Use the debugger to see what is wrong.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


问题是,这将非常依赖于我们无法访问的数据 - 因此我们无法准确说明您收到错误的原因,甚至是哪里。



正在发生的是你正在访问一个不存在的列 - 而且因为我们无法运行你的代码,我们不知道有多少列你应该有。我怀疑索引从零开始,而不是一个 - 所以你可能有八列,需要将它们称为0到7,而不是1到8.
The problem is that this is going to be very dependant on your data, which we don't have any access to - so we can't tell exactly why you get the error, or even exactly where.

What's happening is that you are accessing a column which doesn't exist - and because we can't run your code, we have no idea how many columns you should have. I suspect it's that indexes start at zero, not one - so probably you have eight columns, and need to refer to them as 0 to 7, rather than 1 to 8.


解决了问题



只需要声明这段代码



Dim命令As OleDBConnection = Nothing



并将其用于一些SQL语句



Dim命令为新OleDBCommand





谢谢大家:)感谢您的时间和工作:)
Problem Solved

Just need to declare this code

Dim command As OleDBConnection = Nothing

and use this onto some SQL statements

Dim command as New OleDBCommand


Thanks Guys :) Appreciate your time and efffort :)


这篇关于Vb.net ado indexwasofof range的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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