Object reference not set to an instance of an object

  • Thread starter Thread starter Herb Stull
  • Start date Start date
H

Herb Stull

I'm getting this "Object reference not set to an instance of an object"
error when trying to post updates to a Dataset back to the database.

Would really appreciate any help from those of you who know what you're
doing. The error pops up in the Button1_Click SUB when the
SqlDataAdapter1.Update is executed.

Thank you, Herb

----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack() Then

SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter

SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand

SqlConnection = New System.Data.SqlClient.SqlConnection

DataSet11 = New DSProfiles.DataSet1

CType(DataSet11, System.ComponentModel.ISupportInitialize).BeginInit()

SqlDataAdapter2 = New System.Data.SqlClient.SqlDataAdapter

SqlSelectCommand2 = New System.Data.SqlClient.SqlCommand

'

'SqlDataAdapter1

'

SqlDataAdapter1.SelectCommand = SqlSelectCommand1

SqlDataAdapter1.TableMappings.AddRange(New
System.Data.Common.DataTableMapping() {New
System.Data.Common.DataTableMapping("Table", "ap_TSSUA_GetProfileInfo", New
System.Data.Common.DataColumnMapping() {New
System.Data.Common.DataColumnMapping("DSPUAKey", "DSPUAKey"), New
System.Data.Common.DataColumnMapping("DSPLastName", "DSPLastName"), New
System.Data.Common.DataColumnMapping("DSPFirstName", "DSPFirstName"), New
System.Data.Common.DataColumnMapping("DSPEmail", "DSPEmail"), New
System.Data.Common.DataColumnMapping("DSPPhone", "DSPPhone"), New
System.Data.Common.DataColumnMapping("DSPPosition", "DSPPosition"), New
System.Data.Common.DataColumnMapping("DSPLastAccessDate",
"DSPLastAccessDate"), New System.Data.Common.DataColumnMapping("DTStamp",
"DTStamp")})})

'SqlDataAdapter2

'

SqlDataAdapter2.SelectCommand = SqlSelectCommand2

SqlDataAdapter2.TableMappings.AddRange(New
System.Data.Common.DataTableMapping() {New
System.Data.Common.DataTableMapping("Table", "TSS_SystemMessages", New
System.Data.Common.DataColumnMapping() {New
System.Data.Common.DataColumnMapping("SystemMessage", "SystemMessage")})})

'

'SqlSelectCommand1

'

SqlSelectCommand1.CommandText = "[ap_TSSUA_GetProfileInfo]"

SqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure

SqlSelectCommand1.Connection = SqlConnection

SqlSelectCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RETURN_VALUE",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
False, CType(0, Byte), CType(0, Byte), "",
System.Data.DataRowVersion.Current, Nothing))

SqlSelectCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@UAKey", System.Data.SqlDbType.Int, 4))

SqlSelectCommand1.Parameters("@UAKey").Value = Session.Item("UAKey")

'

'SqlSelectCommand2

'

SqlSelectCommand2.CommandText = "[ap_TSSSystemMessages_Retrieve]"

SqlSelectCommand2.CommandType = System.Data.CommandType.StoredProcedure

SqlSelectCommand2.Connection = SqlConnection

SqlSelectCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RETURN_VALUE",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
False, CType(0, Byte), CType(0, Byte), "",
System.Data.DataRowVersion.Current, Nothing))

'

'SqlConnection

'

SqlConnection.ConnectionString = DSProfilesConfiguration.ConnectionString

'

'DataSet11

'

DataSet11.DataSetName = "DataSet1"

DataSet11.Locale = New System.Globalization.CultureInfo("en-US")

CType(DataSet11, System.ComponentModel.ISupportInitialize).EndInit()

SqlConnection.Open()

SqlDataAdapter1.Fill(DataSet11, "ap_TSSUA_GetProfileInfo")

SqlDataAdapter2.Fill(DataSet11, "TSS_SystemMessages")

SqlConnection.Close()

SqlSelectCommand1.Dispose()

SqlSelectCommand2.Dispose()

' Pulls value out of a table in the Dataset and sets the label's text value
to it.

Dim dtblMsg As DataTable, drow As DataRow

dtblMsg = DataSet11.Tables("TSS_SystemMessages")

drow = dtblMsg.Rows(0)

Label1.Text = drow("SystemMessage")

' Bind the form fields to values in the SQLDataAdapter1 dataset.

txtFirstName.DataBind()

txtLastName.DataBind()

txtEmail.DataBind()

txtPhone.DataBind()

txtPosition.DataBind()

txtLastAccessDate.DataBind()

End If

End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' Establish a process for updating records



'SqlUpdateCommand1

'

SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand

SqlUpdateCommand1.CommandText = "[ap_TSSUA_UpdateProfile]"

SqlUpdateCommand1.CommandType = System.Data.CommandType.StoredProcedure

SqlUpdateCommand1.Connection = SqlConnection

With SqlUpdateCommand1.Parameters

..Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
False, CType(0, Byte), CType(0, Byte), "",
System.Data.DataRowVersion.Current, Nothing))

..Add(New System.Data.SqlClient.SqlParameter("@UAKey",
System.Data.SqlDbType.Int, 4, Session.Item("UAKey")))

..Add(New System.Data.SqlClient.SqlParameter("@DSPLastName",
System.Data.SqlDbType.VarChar, 50, "DSPLastName"))

..Add(New System.Data.SqlClient.SqlParameter("@DSPFirstName",
System.Data.SqlDbType.VarChar, 50, "DSPFirstName"))

..Add(New System.Data.SqlClient.SqlParameter("@DSPEmail",
System.Data.SqlDbType.VarChar, 50, "DSPEmail"))

..Add(New System.Data.SqlClient.SqlParameter("@DSPPhone",
System.Data.SqlDbType.VarChar, 15, "DSPPhone"))

..Add(New System.Data.SqlClient.SqlParameter("@DSPPosition",
System.Data.SqlDbType.SmallInt, 2, "DSPPosition"))

..Add(New System.Data.SqlClient.SqlParameter("@DTStamp",
System.Data.SqlDbType.Timestamp, 8, "DTStamp"))

End With

'

SqlDataAdapter1.UpdateCommand = SqlUpdateCommand1

Try

SqlConnection.Open()

SqlDataAdapter1.Update(DataSet11, "ap_TSSUA_GetProfileInfo")

Catch ex As System.Data.SqlClient.SqlException

If ex.Number = 101 Then

Response.Write("Record was updated by someone else.")

End If

Finally

SqlConnection.Close()

SqlUpdateCommand1.Dispose()

End Try

End Sub
 
Herb,

I don't see your declaratin for Button1_click variables. Do you have Option
Strict On and Option Explicit On?

The problem is probably that DataSet11 is Nothing. But it was so hard to
read this code, I couldn't find the Else of the Page_load to determine how
you were retrieving the DataSet11.

Paste code into Notepad, then copy it into newsgroup messages directly from
Notepad. Then it will be formatted properly and people are more likely to be
able to help you.

Kathleen

Herb Stull said:
I'm getting this "Object reference not set to an instance of an object"
error when trying to post updates to a Dataset back to the database.

Would really appreciate any help from those of you who know what you're
doing. The error pops up in the Button1_Click SUB when the
SqlDataAdapter1.Update is executed.

Thank you, Herb

-------------------------------------------------------------------------- --
--------------------------------------------------------------------------
--
----------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack() Then

SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter

SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand

SqlConnection = New System.Data.SqlClient.SqlConnection

DataSet11 = New DSProfiles.DataSet1

CType(DataSet11, System.ComponentModel.ISupportInitialize).BeginInit()

SqlDataAdapter2 = New System.Data.SqlClient.SqlDataAdapter

SqlSelectCommand2 = New System.Data.SqlClient.SqlCommand

'

'SqlDataAdapter1

'

SqlDataAdapter1.SelectCommand = SqlSelectCommand1

SqlDataAdapter1.TableMappings.AddRange(New
System.Data.Common.DataTableMapping() {New
System.Data.Common.DataTableMapping("Table", "ap_TSSUA_GetProfileInfo", New
System.Data.Common.DataColumnMapping() {New
System.Data.Common.DataColumnMapping("DSPUAKey", "DSPUAKey"), New
System.Data.Common.DataColumnMapping("DSPLastName", "DSPLastName"), New
System.Data.Common.DataColumnMapping("DSPFirstName", "DSPFirstName"), New
System.Data.Common.DataColumnMapping("DSPEmail", "DSPEmail"), New
System.Data.Common.DataColumnMapping("DSPPhone", "DSPPhone"), New
System.Data.Common.DataColumnMapping("DSPPosition", "DSPPosition"), New
System.Data.Common.DataColumnMapping("DSPLastAccessDate",
"DSPLastAccessDate"), New System.Data.Common.DataColumnMapping("DTStamp",
"DTStamp")})})

'SqlDataAdapter2

'

SqlDataAdapter2.SelectCommand = SqlSelectCommand2

SqlDataAdapter2.TableMappings.AddRange(New
System.Data.Common.DataTableMapping() {New
System.Data.Common.DataTableMapping("Table", "TSS_SystemMessages", New
System.Data.Common.DataColumnMapping() {New
System.Data.Common.DataColumnMapping("SystemMessage", "SystemMessage")})})

'

'SqlSelectCommand1

'

SqlSelectCommand1.CommandText = "[ap_TSSUA_GetProfileInfo]"

SqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure

SqlSelectCommand1.Connection = SqlConnection

SqlSelectCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RETURN_VALUE",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
False, CType(0, Byte), CType(0, Byte), "",
System.Data.DataRowVersion.Current, Nothing))

SqlSelectCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@UAKey", System.Data.SqlDbType.Int, 4))

SqlSelectCommand1.Parameters("@UAKey").Value = Session.Item("UAKey")

'

'SqlSelectCommand2

'

SqlSelectCommand2.CommandText = "[ap_TSSSystemMessages_Retrieve]"

SqlSelectCommand2.CommandType = System.Data.CommandType.StoredProcedure

SqlSelectCommand2.Connection = SqlConnection

SqlSelectCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@RETURN_VALUE",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
False, CType(0, Byte), CType(0, Byte), "",
System.Data.DataRowVersion.Current, Nothing))

'

'SqlConnection

'

SqlConnection.ConnectionString = DSProfilesConfiguration.ConnectionString

'

'DataSet11

'

DataSet11.DataSetName = "DataSet1"

DataSet11.Locale = New System.Globalization.CultureInfo("en-US")

CType(DataSet11, System.ComponentModel.ISupportInitialize).EndInit()

SqlConnection.Open()

SqlDataAdapter1.Fill(DataSet11, "ap_TSSUA_GetProfileInfo")

SqlDataAdapter2.Fill(DataSet11, "TSS_SystemMessages")

SqlConnection.Close()

SqlSelectCommand1.Dispose()

SqlSelectCommand2.Dispose()

' Pulls value out of a table in the Dataset and sets the label's text value
to it.

Dim dtblMsg As DataTable, drow As DataRow

dtblMsg = DataSet11.Tables("TSS_SystemMessages")

drow = dtblMsg.Rows(0)

Label1.Text = drow("SystemMessage")

' Bind the form fields to values in the SQLDataAdapter1 dataset.

txtFirstName.DataBind()

txtLastName.DataBind()

txtEmail.DataBind()

txtPhone.DataBind()

txtPosition.DataBind()

txtLastAccessDate.DataBind()

End If

End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' Establish a process for updating records



'SqlUpdateCommand1

'

SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand

SqlUpdateCommand1.CommandText = "[ap_TSSUA_UpdateProfile]"

SqlUpdateCommand1.CommandType = System.Data.CommandType.StoredProcedure

SqlUpdateCommand1.Connection = SqlConnection

With SqlUpdateCommand1.Parameters

.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue,
False, CType(0, Byte), CType(0, Byte), "",
System.Data.DataRowVersion.Current, Nothing))

.Add(New System.Data.SqlClient.SqlParameter("@UAKey",
System.Data.SqlDbType.Int, 4, Session.Item("UAKey")))

.Add(New System.Data.SqlClient.SqlParameter("@DSPLastName",
System.Data.SqlDbType.VarChar, 50, "DSPLastName"))

.Add(New System.Data.SqlClient.SqlParameter("@DSPFirstName",
System.Data.SqlDbType.VarChar, 50, "DSPFirstName"))

.Add(New System.Data.SqlClient.SqlParameter("@DSPEmail",
System.Data.SqlDbType.VarChar, 50, "DSPEmail"))

.Add(New System.Data.SqlClient.SqlParameter("@DSPPhone",
System.Data.SqlDbType.VarChar, 15, "DSPPhone"))

.Add(New System.Data.SqlClient.SqlParameter("@DSPPosition",
System.Data.SqlDbType.SmallInt, 2, "DSPPosition"))

.Add(New System.Data.SqlClient.SqlParameter("@DTStamp",
System.Data.SqlDbType.Timestamp, 8, "DTStamp"))

End With

'

SqlDataAdapter1.UpdateCommand = SqlUpdateCommand1

Try

SqlConnection.Open()

SqlDataAdapter1.Update(DataSet11, "ap_TSSUA_GetProfileInfo")

Catch ex As System.Data.SqlClient.SqlException

If ex.Number = 101 Then

Response.Write("Record was updated by someone else.")

End If

Finally

SqlConnection.Close()

SqlUpdateCommand1.Dispose()

End Try

End Sub
 
Back
Top