Please help with error: Object reference not set to an instance of an object 
Author Message
 Please help with error: Object reference not set to an instance of an object

I have an application that uses a dataset to delete
selected rows from a table at once. A pointer goes through
the table and searches for the correct rows to delete.
That pointer is called dr. I am able to successfully build
and view the page in a browser. However, when I select
rows and then hit the delete button, i get the error at
the bottom of this message. I hope someone can help me. I
attached the source file for reference. I am developing in
Visual Studio .net using VB. Thank you.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and where
it originated in the code.

Exception Details: System.NullReferenceException: Object
reference not set to an instance of an object.

Source Error:

Line 134: dr =
Me.DsRSTest1.RSTestTable.FindByDepartmentCode
(DepartmentCode) Line 135: ' ... and delete it. Line 136:
dr.Delete() Line 137: End If Line 138: i += 1

  RSVBcode.txt
9K Download


Tue, 01 Feb 2005 22:47:45 GMT  
 Please help with error: Object reference not set to an instance of an object
Would help with full error message... where is it blowing out?
Quote:

> Public Class WebForm1
>     Inherits System.Web.UI.Page
>     Protected WithEvents SqlDataAdapter2 As
> System.Data.SqlClient.SqlDataAdapter
>     Protected WithEvents SqlSelectCommand2 As
> System.Data.SqlClient.SqlCommand
>     Protected WithEvents SqlInsertCommand2 As
> System.Data.SqlClient.SqlCommand
>     Protected WithEvents SqlUpdateCommand2 As
> System.Data.SqlClient.SqlCommand
>     Protected WithEvents SqlDeleteCommand2 As
> System.Data.SqlClient.SqlCommand
>     Protected WithEvents SqlConnection1 As
> System.Data.SqlClient.SqlConnection
>     Protected WithEvents DsRSTest1 As RSApplication.dsRSTest
>     Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
>     Protected WithEvents btnDelete1 As System.Web.UI.WebControls.Button

> #Region " Web Form Designer Generated Code "

>     'This call is required by the Web Form Designer.
>     <System.Diagnostics.De{*filter*}StepThrough()> Private Sub
> InitializeComponent()
>         Me.SqlDataAdapter2 = New
> System.Data.SqlClient.SqlDataAdapter()
>         Me.SqlDeleteCommand2 = New System.Data.SqlClient.SqlCommand()
>         Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
>         Me.SqlInsertCommand2 = New System.Data.SqlClient.SqlCommand()
>         Me.SqlSelectCommand2 = New System.Data.SqlClient.SqlCommand()
>         Me.SqlUpdateCommand2 = New System.Data.SqlClient.SqlCommand()
>         Me.DsRSTest1 = New RSApplication.dsRSTest()
>         CType(Me.DsRSTest1,
> System.ComponentModel.ISupportInitialize).BeginInit()
>         '
>         'SqlDataAdapter2
>         '
>         Me.SqlDataAdapter2.DeleteCommand = Me.SqlDeleteCommand2
>         Me.SqlDataAdapter2.InsertCommand = Me.SqlInsertCommand2
>         Me.SqlDataAdapter2.SelectCommand = Me.SqlSelectCommand2
>         Me.SqlDataAdapter2.TableMappings.AddRange(New
> System.Data.Common.DataTableMapping() {New
> System.Data.Common.DataTableMapping("Table", "RSTestTable", New
> System.Data.Common.DataColumnMapping() {New
> System.Data.Common.DataColumnMapping("DepartmentCode",
> "DepartmentCode"), New
> System.Data.Common.DataColumnMapping("DepartmentDescr",
> "DepartmentDescr"), New
> System.Data.Common.DataColumnMapping("DepartCodeDescr",
> "DepartCodeDescr")})})
>         Me.SqlDataAdapter2.UpdateCommand = Me.SqlUpdateCommand2
>         '
>         'SqlDeleteCommand2
>         '
>         Me.SqlDeleteCommand2.CommandText = "DELETE FROM RSTestTable



>         "ND DepartCodeDescr IS NULL) AND (DepartmentDescr =


>         Me.SqlDeleteCommand2.Connection = Me.SqlConnection1
>         Me.SqlDeleteCommand2.Parameters.Add(New

> System.Data.SqlDbType.VarChar, 15, System.Data.ParameterDirection.Input,
> False, CType(0, Byte), CType(0, Byte), "DepartmentCode",
> System.Data.DataRowVersion.Original, Nothing))
>         Me.SqlDeleteCommand2.Parameters.Add(New

> System.Data.SqlDbType.NVarChar, 50,
> System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0,
> Byte), "DepartCodeDescr", System.Data.DataRowVersion.Original, Nothing))
>         Me.SqlDeleteCommand2.Parameters.Add(New

> System.Data.SqlDbType.VarChar, 45, System.Data.ParameterDirection.Input,
> False, CType(0, Byte), CType(0, Byte), "DepartmentDescr",
> System.Data.DataRowVersion.Original, Nothing))
>         '
>         'SqlConnection1
>         '
>         Me.SqlConnection1.ConnectionString = "data
> source=CFF133A;initial catalog=Employees;persist security
> info=False;user id" &  
>         "=sa;workstation id=WS TEMP730;packet size=4096"
>         '
>         'SqlInsertCommand2
>         '
>         Me.SqlInsertCommand2.CommandText = "INSERT INTO
> RSTestTable(DepartmentCode, DepartmentDescr, DepartCodeDescr) VALUES " &


> DepartmentCode, De" &  
>         "partmentDescr, DepartCodeDescr FROM RSTestTable WHERE

>         "entCode)"
>         Me.SqlInsertCommand2.Connection = Me.SqlConnection1
>         Me.SqlInsertCommand2.Parameters.Add(New

> System.Data.SqlDbType.VarChar, 15, "DepartmentCode"))
>         Me.SqlInsertCommand2.Parameters.Add(New

> System.Data.SqlDbType.VarChar, 45, "DepartmentDescr"))
>         Me.SqlInsertCommand2.Parameters.Add(New

> System.Data.SqlDbType.NVarChar, 50, "DepartCodeDescr"))
>         '
>         'SqlSelectCommand2
>         '
>         Me.SqlSelectCommand2.CommandText = "SELECT DepartmentCode,
> DepartmentDescr, DepartCodeDescr FROM RSTestTable"
>         Me.SqlSelectCommand2.Connection = Me.SqlConnection1
>         '
>         'SqlUpdateCommand2
>         '
>         Me.SqlUpdateCommand2.CommandText = "UPDATE RSTestTable SET



>         " DepartmentCode) AND (DepartCodeDescr =

>         "epartCodeDescr IS NULL AND DepartCodeDescr IS NULL) AND


> DepartmentDescr IS" &  
>         " NULL); SELECT DepartmentCode, DepartmentDescr, DepartCodeDescr
> FROM RSTestTable" &  

>         Me.SqlUpdateCommand2.Connection = Me.SqlConnection1
>         Me.SqlUpdateCommand2.Parameters.Add(New

> System.Data.SqlDbType.VarChar, 15, "DepartmentCode"))
>         Me.SqlUpdateCommand2.Parameters.Add(New

> System.Data.SqlDbType.VarChar, 45, "DepartmentDescr"))
>         Me.SqlUpdateCommand2.Parameters.Add(New

> System.Data.SqlDbType.NVarChar, 50, "DepartCodeDescr"))
>         Me.SqlUpdateCommand2.Parameters.Add(New

> System.Data.SqlDbType.VarChar, 15, System.Data.ParameterDirection.Input,
> False, CType(0, Byte), CType(0, Byte), "DepartmentCode",
> System.Data.DataRowVersion.Original, Nothing))
>         Me.SqlUpdateCommand2.Parameters.Add(New

> System.Data.SqlDbType.NVarChar, 50,
> System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0,
> Byte), "DepartCodeDescr", System.Data.DataRowVersion.Original, Nothing))
>         Me.SqlUpdateCommand2.Parameters.Add(New

> System.Data.SqlDbType.VarChar, 45, System.Data.ParameterDirection.Input,
> False, CType(0, Byte), CType(0, Byte), "DepartmentDescr",
> System.Data.DataRowVersion.Original, Nothing))
>         '
>         'DsRSTest1
>         '
>         Me.DsRSTest1.DataSetName = "dsRSTest"
>         Me.DsRSTest1.Locale = New
> System.Globalization.CultureInfo("en-US")
>         Me.DsRSTest1.Namespace = " http://www.*-*-*.com/ ;
>         CType(Me.DsRSTest1,
> System.ComponentModel.ISupportInitialize).EndInit()

>     End Sub

>     Private Sub Page Init(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Init
>         'CODEGEN: This method call is required by the Web Form Designer
>         'Do not modify it using the code editor.
>         InitializeComponent()
>     End Sub

> #End Region

>     Private Sub Page Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>         SqlDataAdapter2.Fill(DsRSTest1)

>         If Not IsPostBack Then
>             DataGrid1.DataBind()
>         End If

>     End Sub

>     Private Sub DataGrid1 SelectedIndexChanged(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles
> DataGrid1.SelectedIndexChanged

>     End Sub

>     Public Sub Delete()

>     End Sub

>     Private Sub btnDelete1 Click(ByVal sender As System.Object,  
>            ByVal e As System.EventArgs) Handles btnDelete1.Click
>         ' Walk the grid looking for selected rows
>         Dim i As Integer = 0
>         Dim cb As CheckBox
>         Dim dgi As DataGridItem
>         Dim DepartmentCode As Integer
>         Dim dr As dsRSTest.RSTestTableRow
>         For Each dgi In DataGrid1.Items
>             cb = CType(dgi.Cells(0).Controls(1), CheckBox)
>             If cb.Checked Then
>                 ' Determine the key of the selected record ...
>                 DepartmentCode = CType(DataGrid1.DataKeys(i), Integer)
>                 ' ... get a pointer to the corresponding dataset record
> ...
>                 dr =
> Me.DsRSTest1.RSTestTable.FindByDepartmentCode(DepartmentCode)
>                 ' ... and delete it.
>                 dr.Delete()
>             End If
>             i += 1
>         Next
>         Me.SqlDataAdapter2.Update(DsRSTest1)
>         Me.SqlDataAdapter2.Fill(DsRSTest1)
>         DataGrid1.DataBind()
>     End Sub

> End Class
> --



Thu, 10 Feb 2005 08:34:35 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Error when showing form: Object reference not set to an instance of an object

2. How to fix Error: Object reference not set to an instance of an object

3. Error: Object reference not set to an instance of an object

4. Multiple Forms Question: Object reference not set to an instance of an object

5. Object reference not set to an instance of an object

6. Object Reference Not Set to an Instance of an Object

7. Object reference not set to the instance of an object

8. Object reference not set to an instance of an object

9. Object reference not set to an instance of an object

10. Object reference not set to an instance of an object

11. New Twist on: Object reference not set to an instance of an object

12. Object reference not set to an instance of an object

 

 
Powered by phpBB® Forum Software