Click here to Skip to main content
15,914,074 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am having problem updating in gridview with vb.net, the C# version worked fine but the VB is throwing this error: Object reference not set to an instance of an object.

below is my code:
VB
 Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

        Dim row As GridViewRow = DirectCast(GridView1.Rows(e.RowIndex), GridViewRow)
        Dim idlbl As Label = DirectCast(row.FindControl("IdLabel"), Label)
        Dim budgetYearTextBox As TextBox = DirectCast(row.FindControl("budgetYearTextBox"), TextBox)
        Dim budgetAmountTextBox As TextBox = DirectCast(row.FindControl("budgetAmountTextBox"), TextBox)

        Dim sqltext As String = "Update Budget Set BudgetYear =" & budgetYearTextBox.Text & ", YearBudgetAmount =" & budgetAmountTextBox.Text & " where AgentID =" & idlbl.Text & ""
        Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString)
            Using cmd As SqlCommand = New SqlCommand()
                With cmd
                    .CommandText = sqltext
                    .CommandType = CommandType.Text
                    .Connection = con
                End With
                Try
                    con.Open()
                    cmd.ExecuteNonQuery()
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
        GridView1.EditIndex = -1
        populategrid()
    End Sub
<pre>
Posted
Updated 23-Sep-10 0:26am
v2
Comments
anshudutta 23-Sep-10 5:43am    
Which line are you exactly getting the error?

1 solution

There are quite a few place where you could get this error. Since you have not specified where, let me point out the most probable place.

If error is at this line:
SQL
Dim sqltext As String = "Update Budget Set BudgetYear =" & budgetYearTextBox.Text & ", YearBudgetAmount =" & budgetAmountTextBox.Text & " where AgentID =" & idlbl.Text & ""

Reason might be the FindControls in the above 3 lines did not return any of the control (Label or Textbox) and thus when you try to access it value, it breaks! Make sure they are not null and proper controls.

Another place could while you use connection string. Do look it.

BTW, a simple use of DEBUGGER should tell the line and issue easily. Try it.
 
Share this answer
 
Comments
Adesina Mark 23-Sep-10 6:49am    
You are right thats where i had the error, what can be the possible cause of it please
Sandeep Mewara 23-Sep-10 11:36am    
Have a null check... what else!!!
Further, if you are expecting a control, make sure the ID names are right and not a typo and they do exist where you are trying to find it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900