在vb.net中通过gridview更新数据库

时间:2011-08-23 14:38:38

标签: vb.net ms-access

这是我的更新活动代码

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
    Connection.Open()
    Dim idd As Integer
    idd = GridView1.DataKeys(e.RowIndex).Value
    Dim empname As TextBox
    Dim a As String
    empname = GridView1.Rows(e.RowIndex).FindControl("empName")
    a = Trim(empname.Text.ToString)
    Dim Command As New OleDbCommand("update employeeDetails set empName='" & a & "' where ID=" & Trim(idd) & " ", Connection)
    Command.ExecuteNonQuery()
    Connection.Close()
    GridView1.EditIndex = -1
    Response.Redirect("listOfAssociates.aspx")
End Sub

我的gridview代码是:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
        <Columns>
            <asp:TemplateField HeaderText="S NO"></asp:TemplateField>
            <asp:BoundField HeaderText="Emp Id" DataField="empNumber"/>
            <asp:BoundField HeaderText="Emp Name" DataField="empName"/>
            <asp:BoundField HeaderText="Emp Email Id" DataField="empEmailId"/>
            <asp:BoundField HeaderText="Role" DataField="empRole"/>
            <asp:BoundField HeaderText="Project Won" DataField="empProjectNumber"/>
            <asp:BoundField HeaderText="Project Name" DataField="empProjectName"/>
            <asp:BoundField HeaderText="Start Date" DataField="empProjectStartDate"/>
            <asp:BoundField HeaderText="End Date" DataField="empProjectEndDate"/>
            <asp:BoundField HeaderText="Supervisor Id" DataField="empProjectSupervisorId"/>
            <asp:BoundField HeaderText="Allocation %" DataField="empProjectAP"/>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />

        </Columns>
    </asp:GridView>

它正在删除和获取正常但在尝试更新时显示nullreferenceexception未被此行中的用户代码处理:

a = Trim(empname.Text.ToString)

任何人都可以帮助我。提前谢谢

使用ms访问作为数据库

1 个答案:

答案 0 :(得分:0)

试试这个......

'intRowIndex = Integer Variable of Row you wish to retrieve values from.
a = Trim(gridview1.rows(intRowIndex).Cells(2).Text)