Friday, March 16, 2012

Object reference not set to an instance of an object.

Hi All,

Can anyone please point out what is causing this error!

I am trying to call a function then read its dataset

rgs

Tony

Server Error in '/' Application.
------------------------

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.

<code/
Dim dataSet As System.Data.DataSet

Sub Button1_Click(sender As Object, e As EventArgs)

dim ida as string = textbox1.text

call MyQueryMethod(ida)

dim text as string = dataset.tables(0).rows(0).item(0)

response.write(text)

End Sub

Function MyQueryMethod(ByVal iD As Integer) As System.Data.DataSet
Dim connectionString As String = "server=xxxx.xxx.xxx; user id=xxxxx.150'; password=xxxxxx; database=xxxx"& _
"xxxxxx.150'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [tblReg].[UserName] FROM [tblReg] WHERE ([tblReg].[ID] = @dotnet.itags.org.ID)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_iD As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_iD.ParameterName = "@dotnet.itags.org.ID"
dbParam_iD.Value = iD
dbParam_iD.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_iD)

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)

Return dataSet

End Function

</code>

call MyQueryMethod(ida)

dim text as string = dataset.tables(0).rows(0).item(0)

Looks like you want to change that to
dim text as string = MyQueryMethod(ida).tables(0).rows(0).item(0)

0 comments:

Post a Comment