JS List
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(se nder 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.Ro ws(e.RowIn dex).FindC ontrol("lb lEmpID"), Label).Text)
intEmpID = CInt(strEmpID)
Dim ckTfr As Boolean
ckTfr = DirectCast(gvEmployees.Row s(e.RowInd ex).FindCo ntrol("Tra nsfers"), CheckBox).Checked
Dim ckPromo As Boolean
ckPromo = DirectCast(gvEmployees.Row s(e.RowInd ex).FindCo ntrol("chk Promotions "), 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(d s, strSQL)
ds.Dispose()
gvEmployees.EditIndex = -1
BindData()
End Sub
Any ideas?
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
<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(se
gvEmployees.EditIndex = -1
Dim strChange, strFrom, strTo As String
Dim strEmpID, strSQL As String
Dim intEmpID, recordUpdated As Integer
strEmpID = (DirectCast(gvEmployees.Ro
intEmpID = CInt(strEmpID)
Dim ckTfr As Boolean
ckTfr = DirectCast(gvEmployees.Row
Dim ckPromo As Boolean
ckPromo = DirectCast(gvEmployees.Row
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(d
ds.Dispose()
gvEmployees.EditIndex = -1
BindData()
End Sub
Any ideas?
Where are you declaring ds as a DataSet?
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
Public ds As DataSet
Public dt As DataTable
Public dr As DataRow
Public thisOrder As String
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The answer was in the front page - edit item and display item weren't the same name.