索引超出范围例外vb.net
问题描述:
我目前正在一个项目,我需要插入/搜索/添加到/从M.S access
datatable
到textboxes
。索引超出范围例外vb.net
搜索是基于id
并试图将数据插入到文本框,我得到一个错误
指数超出范围的异常:手机号码”
cn.Open()
comand.Connection = cn
comand.CommandText = "SELECT * from Address_table where [email protected]"
comand.Parameters.Add("@id", OleDbType.Numeric).Value = TextBox1.Text
Dim dr As OleDbDataReader = comand.ExecuteReader()
Do While dr.Read() = True
TextBox2.Text = dr("Name")
TextBox3.Text = dr("House Name")
TextBox4.Text = dr("City")
TextBox5.Text = dr("Mobile Number".ToString)
TextBox6.Text = dr("PinCode".ToString)
TextBox7.Text = dr("Email")
Loop
If dr.Read() = False Then
MessageBox.Show("enter a valid id")
End If
dr.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
cn.Close()
任何帮助表示赞赏。提前谢谢
答
This:
TextBox5.Text = dr("Mobile Number".ToString)
应该是这样的:
TextBox5.Text = dr("Mobile Number").ToString
真的,我想到的是列名也并不完全正确。不知怎的,你可能需要逃离这个空间。
考虑到“Mobile Number”是一个字符串loteral,“Mobile Number”.ToString没有太大意义。你确定有一个专门命名为“移动号码”的列 – Plutonix
在'SELECT'语句中包含列,并使用括号'[]'作为包含空格的名称。 '选择[手机号码] FROM ...'。 – Fabio
您不能按照您的方式执行'ToString',您可以访问'DataRow'。例如它应该是:**'TextBox5.Text = dr(“Mobile Number”)。ToString' **另外你可能想要检查它是否是'DBNull.Value',否则当你做'ToString'时它会失败,因为你不能在'DBNull.Value'上做'ToString' – Codexer