如何将后期绑定语法转换为vb.net中的早期绑定语法?

时间:2011-09-05 00:18:38

标签: vb.net binding error-handling

我有这段代码:

 Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        ' retrieving the administration table.
        con.Open()
        DataAdapter1.SelectCommand = sqladmin
        DataAdapter1.Fill(ds, "stratos")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "stratos"
        con.Close()
    Catch myerror As MySqlException
        MessageBox.Show("Error Retrieving Administration Table: " & myerror.Message)
    End Try


    Try
        ' retrieving the projects list.
        con.Open()
        DataAdapter2.SelectCommand = sqlprojects
        DataAdapter2.Fill(ds2, "projects")
        ListBox1.Items.Clear()

        For Each DataRow In ds2.Tables("projects").Rows

            ' ##### THE ERROR OCCURS ON THE LINE BELOW: ##### '

            ListBox1.Items.Add(DataRow("project_name"))
        Next
        con.Close()

    Catch myerror As MySqlException
        MessageBox.Show("Error Retrieving Projects List: " & myerror.Message)
    End Try

我发现以下错误:

  

错误1:Option Strict On禁止后期绑定。

我在运行Windows 7操作系统的网关笔记本电脑上使用visual basic 2010 express 我该如何解决这个错误?

2 个答案:

答案 0 :(得分:3)

您需要将带有错误的循环更改为以下内容:

For Each dr as DataRow In ds2.Tables("projects").Rows 

  ' ##### THE ERROR OCCURS ON THE LINE BELOW: ##### ' 

  ListBox1.Items.Add(Convert.ToString(dr("project_name"))) 
Next 

答案 1 :(得分:2)

这通常意味着你对一个变量进行了Dim,或者声明了一个没有类型的函数。 For Each循环中使用的DataRow可能是罪魁祸首。另一个是DatRow中的项目是object类型;你应该施放它们或以其他方式将它们转换成特定的类型。你想要这个:

For Each dr As DataRow in d2.Tables("projects").Rows
   '...

   ListBox1.Items.Add(dr("project_name").ToString())
Next dr