Please help i have two issues my GridView becomes empty on enabling checkbox on form load and also GridView view returns error when i use the DropDownList code of on rowdatabound.
here is the error returned below.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Web.UI.WebControls.ListItemCollection.FindByValue(...) returned Nothing.
Public Class Newsearchdata
Inherits System.Web.UI.Page
Dim conString As String = ConfigurationManager.ConnectionStrings("STOREConnectionString1").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim cmd As New SqlCommand("SELECT Name,class as Classr,class,stream, House, ACNOS,admno,sex,STDTYPE,Autofield FROM STUDENT")
Stockbooks.DataSource = Me.ExecuteQuery(cmd, "SELECT")
Stockbooks.DataBind()
Stockbooks.UseAccessibleHeader = True
Stockbooks.HeaderRow.TableSection = TableRowSection.TableHeader
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
'If e.Row.RowType = DataControlRowType.DataRow AndAlso Stockbooks.EditIndex = e.Row.RowIndex Then
' If e.Row.RowType = DataControlRowType.DataRow AndAlso Stockbooks.EditIndex = e.Row.RowIndex Then
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim cell As TableCell = e.Row.Cells(1)
Dim quantity As String = cell.Text
'Find the DropDownList in the Row
Dim ddlCountries As DropDownList = CType(e.Row.FindControl("ddlclasses"), DropDownList)
ddlCountries.DataSource = GetData("SELECT DISTINCT(Class)as classr FROM Classes")
ddlCountries.DataTextField = "Classr"
ddlCountries.DataValueField = "Classr"
ddlCountries.DataBind()
'Add Default Item in the DropDownList
ddlCountries.Items.Insert(0, New ListItem("Please select"))
'Select the Country of Customer in DropDownList
Dim country As String = CType(e.Row.FindControl("Lblclasses"), Label).Text
If country = "" Then
Exit Sub
Else
ddlCountries.Items.FindByValue(country).Selected = True
End If
End If
End Sub
End Class