0
我想要指定gridview控件內的下拉列表的數據源。但是當我執行下面的代碼時,我得到NullReferenceException。將數據集分配爲下拉列表的數據源時出現NullReferenceException
Protected Sub grvStudent_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Try
Dim Connection As SqlConnection = New SqlConnection(ConnectionString)
Dim Query As String = "select Course from Courses"
Dim Command As SqlCommand
Command = New SqlCommand(Query, Connection)
Dim Da As New SqlDataAdapter(Command)
Dim Ds As New DataSet()
Connection.Close()
Dim ddlCourse = DirectCast(e.Row.FindControl("ddlCourse"), DropDownList)
Da.Fill(Ds)
ddlCourse.DataSource = Ds //Exception is here
ddlCourse.DataTextField = "Course"
ddlCourse.DataValueField = "Id"
ddlCourse.DataBind()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
End Sub
我試過了。但是同樣的例外。我在課程領域有5個項目,我會得到5個錯誤消息框。 – sujeesh
您是否檢查了我在答案中添加的鏈接,其中有與您的問題相關的完整示例。 – Meherzad
嘗試ddlCourse.DataSource = Ds.Tables [0];對於例外行 – Meherzad