System.NullReferenceException Error when finding control in Row Updating event of ASP.Net GridView

Last Reply on Jul 06, 2012 09:26 AM By Mudassar

Posted on Jul 06, 2012 09:23 AM

i got an error here in below line of code  :

Dim tempID As String = DirectCast(grdMaster.Rows(e.RowIndex).FindControl("tempID"), Label).Text

ERROR IS : System.NullReferenceException: Object reference not set to an instance of an object.

 

 

my code is :

 

Protected Sub grdMaster_rowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdMaster.RowUpdating
        Dim conStr As String = ConfigurationManager.ConnectionStrings("conStr").ConnectionString
        Dim con1 As SqlConnection = New SqlConnection(conStr)

        Dim tempID As String = DirectCast(grdMaster.Rows(e.RowIndex).FindControl("tempID"), Label).Text
        Dim grpName As String = DirectCast(grdMaster.Rows(e.RowIndex).FindControl("group"), TextBox).Text
        Dim usrName As String = DirectCast(grdMaster.Rows(e.RowIndex).FindControl("user"), TextBox).Text

        Dim str As String = "update MMM_REF_GROUP_USER set grpId=@groupId, usrId=@userId where tempID=@tempId "

        Dim cmd As SqlCommand = New SqlCommand(str, con1)
        cmd.Parameters.Add("@tempId", SqlDbType.Int).Value = tempID

        cmd.Parameters.Add("@grpId", SqlDbType.NVarChar).Value = grpName

        cmd.Parameters.Add("@usrId", SqlDbType.NVarChar).Value = usrName
        grdMaster.EditIndex = -1

        grdMaster.DataSource = GetDataItem(cmd)
        grdMaster.DataBind()


    End Sub

Posted on Jul 06, 2012 09:26 AM

Your Label is not under EditItemTemplate hence you will not be able to find it in Row_Updating event

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html