Link to home
Start Free TrialLog in
Avatar of JS List
JS ListFlag for United States of America

asked on

Updating SQL from Gridview Checkbox Object reference not set to an instance of an object

Hello,
I know this is a common question.  I have a gridview and 2 cells have a checkbox.  I'm updating the data to a SQL table.  It comes up with error:
Object reference not set to an instance of an object.

I was updating this to Access and now the table is being moved to SQL and it's erroring out.

Using asp.net vb
aspx page:
<asp:GridView ID="gvEmployees" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="EmpID" DataSourceID=""  AllowPaging="False">
<Columns>

<asp:TemplateField HeaderText="Transfer" SortExpression="Transfers">
<EditItemTemplate><div align="center">
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# IIf(Eval("Transfers") Is DBNull.Value, "False", Eval("Transfers")) %>' /></div>
</EditItemTemplate>
<ItemTemplate>
<div align="center">
<asp:CheckBox ID="Transfers" runat="server" Checked='<%# IIf(Eval("Transfers") Is DBNull.Value, "False", Eval("Transfers")) %>'
      Enabled="false" /></div>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Promo" SortExpression="Promotions">
<EditItemTemplate><div align="center">
<asp:CheckBox ID="chkPromotions" runat="server" Checked='<%# IIf(Eval("Promotions") Is DBNull.Value, "False", Eval("Promotions")) %>' /></div>
</EditItemTemplate>
<ItemTemplate><div align="center">
<asp:CheckBox ID="chkPromotions" runat="server" Checked='<%# IIf(Eval("Promotions") Is DBNull.Value, "False", Eval("Promotions")) %>'
      Enabled="false" /></div>
</ItemTemplate>
</asp:TemplateField>            

<asp:CommandField ShowEditButton="True" ButtonType="Button" />
 </Columns>
</asp:GridView>

aspx.vb page
Imports System.Data
Imports System.Data.SqlClient

Imports DataSetClassIntranetSQL  (This gets the connection string)


Protected Sub gvEmployees_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles gvEmployees.RowUpdating
gvEmployees.EditIndex = -1

Dim strChange, strFrom, strTo As String
Dim strEmpID, strSQL As String
Dim intEmpID, recordUpdated As Integer

strEmpID = (DirectCast(gvEmployees.Rows(e.RowIndex).FindControl("lblEmpID"), Label).Text)
intEmpID = CInt(strEmpID)

Dim ckTfr As Boolean
ckTfr = DirectCast(gvEmployees.Rows(e.RowIndex).FindControl("Transfers"), CheckBox).Checked

Dim ckPromo As Boolean
ckPromo = DirectCast(gvEmployees.Rows(e.RowIndex).FindControl("chkPromotions"), CheckBox).Checked

strSQL = "SELECT empID, Promotions, Transfers FROM Employees WHERE empID = " & intEmpID & " "

Try
ds = GetDataSet(strSQL)

Catch ex As Exception
lbl1.Text = "Unable to retrieve ds: " & ex.ToString

End Try

With ds.Tables(0).Rows(0)
.Item("Promotions") = ckPromo
.Item("Transfers") = ckTfr

End With

recordUpdated = EmployeePhone.UpdateData(ds, strSQL)
ds.Dispose()
gvEmployees.EditIndex = -1

BindData()
End Sub

Any ideas?
Avatar of virtuadept
virtuadept
Flag of United States of America image

Where are you declaring ds as a DataSet?
Avatar of JS List

ASKER

Sorry - it's at the top of the class

Public ds As DataSet
    Public dt As DataTable
    Public dr As DataRow
    Public thisOrder As String
ASKER CERTIFIED SOLUTION
Avatar of JS List
JS List
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JS List

ASKER

The answer was in the front page - edit item and display item weren't the same name.