passing exception messages from a dll to a client

G

Guest

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
 
K

Karl Seguin [MVP]

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
 
G

Guest

Hi,

Thanks for the reply. I am filling the dataset using the following code

public function getData() as dataset
try
Dim ds as New dataset
Dim da as New SqlDataAdapter(objSqlCmd)

da.Fill(ds, "test")
catch ex as exception
Dim msg as string = ex.Message
finally

end try
return ds
end function

sub test()
try
Dim ds as dataset
ds = getData()
catch ex as exception
Dim msg as string = ex.message
finally

end try
end sub

If the function getData() is returning nothing, then I am getting the
exception "object reference not set to an instance of an object" since I am
trying to assign nothing to the dataset. How do i fix that?

Thanks,
Sridhar.
 
G

Guest

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
 
G

Gozirra

I don't think the problem is what you think it is. It's completely
valid to set objDataSet = nothing. Therefore, it should be completely
valid to do so on return from a function. Just to be sure, I've tried
in some small samples and never received an exception. What makes you
believe that the error is in this assignment and not happening within
the function itself?
 
K

Karl Seguin [MVP]

If you didn't swallow exceptions, you'd be a step ahead of the game

Does catching the Exception in getData and setting a message actually HANDLE
it? or does it simply cover it up? Can your code actually continue working
if getData fails? or is it better to call it a lost cause and get a
meanigful error message that you can use to fix your code?

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top