GridView更新问题 [英] GridView Updating Problems

查看:88
本文介绍了GridView更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在使用vb.net在gridview中更新时遇到问题,C#版本工作正常,但是VB抛出此错误:对象引用未设置为对象的实例.

以下是我的代码:

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:

 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>

推荐答案

在很多地方都可能出现此错误.由于您尚未指定位置,因此让我指出最可能的位置.

如果在此行出现错误:
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:
Dim sqltext As String = "Update Budget Set BudgetYear =" & budgetYearTextBox.Text & ", YearBudgetAmount =" & budgetAmountTextBox.Text & " where AgentID =" & idlbl.Text & ""


原因可能是上述三行中的FindControls没有返回任何控件(标签或文本框),因此当您尝试访问它的值时,它会中断!确保它们不是null和正确的控件.

当您使用连接字符串时,另一个地方可以.看看吧.

顺便说一句,简单地使用DEBUGGER应该可以告诉线路和问题.试试吧.


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.


这篇关于GridView更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆