Hi,
Thanks for the reply. I am getting problem when the function returns
"Nothing" and I am trying to assign that Nothing to the variable. Here is the
sample that I am using
Function GetData(pConn as SqlConnection, spName as string, params as
SqlParameter()) as dataset
try
Dim objSqlCmd As SqlCommand
Dim objSqlAdapter As SqlDataAdapter
Dim objDataSet As DataSet
Dim objSqlParam As SqlParameter
Dim strError As String
Dim paramValues As DataTable
Dim paramRow As DataRow
Dim colName As String
Dim colNum As Integer
Try
'Initialize Variables
objSqlCmd = New SqlCommand(storedProcedureName, pConnection)
objSqlAdapter = New SqlDataAdapter(objSqlCmd)
objSqlCmd.CommandType = CommandType.StoredProcedure
'Create a new table for the Output Parameter Values
paramValues = New DataTable("OutputParamValues")
If Not IsNothing(parameters) Then
If parameters.Length > 0 Then
For i As Integer = 0 To parameters.Length - 1
objSqlParam = New
SqlParameter(parameters(i).ParameterName, parameters(i).SqlDbType,
parameters(i).Size)
'If parameter type is output then add column to the
Output Parameter Table
If parameters(i).Direction =
ParameterDirection.Output Then
objSqlParam.Direction = ParameterDirection.Output
colName =
parameters(i).ParameterName.Replace("@", String.Empty)
paramValues.Columns.Add(colName)
Else
objSqlParam.Value = parameters(i).Value
End If
objSqlCmd.Parameters.Add(objSqlParam)
Next
End If
End If
objDataSet = New DataSet
objSqlAdapter.Fill(objDataSet, "Results")
'Check if the Stored Procedure contains any Output Parameters
'If so, add the output parameters to a table and add this table
to the dataset
If paramValues.Columns.Count > 0 Then
paramRow = paramValues.NewRow
colNum = 0
For i As Integer = 0 To objSqlCmd.Parameters.Count - 1
If objSqlCmd.Parameters(i).Direction =
ParameterDirection.Output Then
paramRow(colNum) = objSqlCmd.Parameters(i).Value
colNum = colNum + 1
End If
Next
paramValues.Rows.Add(paramRow)
objDataSet.Tables.Add(paramValues)
End If
Catch ex As Exception
strError = CreateExceptionEmailMessage(ex.Source, ex.Message)
Logger.LogException(ex.Source, ex.Message, ex.StackTrace)
StatusEmailer.SendException(ExceptionEmail, strError)
Throw
Finally
If Not IsNothing(objSqlAdapter) Then
objSqlAdapter.Dispose()
objSqlAdapter = Nothing
End If
If Not IsNothing(objSqlCmd) Then
objSqlCmd.Dispose()
objSqlCmd = Nothing
End If
If Not IsNothing(objSqlParam) Then
objSqlParam = Nothing
End If
End Try
Return objDataSet
end function
sub Test()
objDataSet = GetData(objSqlConn, storedProcedureName, objSqlParams)
'Here I am getting exception since the function is returning nothing and I
am trying to assign Nothing to the variable
end sub
Thanks,
Sridhar
"Karl Seguin [MVP]" wrote:
The "object reference not set to an instance of an object" is a valid
exception.
I'm ready to guess there's a bug in the data access layer.
Probably something like:
}
finally
{
dr.Close();
}
but dr is nothing/null so THAT exception is overwriting the sproc not found
one. The solution is to fix your code :)
Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Sridhar" <Sr*****@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com... Hi,
I have created a web application. I have a data access dll to interact
with database. I added this dll as a reference to the web application. Now
let's say if I am calling a function in data access and if it throws
exception (like cannot find stored procedure), how do I retrieve that
exception message back in the web form? In the client, I am able to catch
the
exception, but the message I am getting is "Object reference not set to an
instance of an object" not "Cannot find stored procedure". How do i
retrieve
the exact message?
Thanks,
Sridhar