468,290 Members | 2,107 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

passing exception messages from a dll to a client

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
Mar 31 '06 #1
5 1232
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

Mar 31 '06 #2
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.

"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


Apr 3 '06 #3
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


Apr 3 '06 #4
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?

Apr 3 '06 #5
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

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Sridhar" <Sr*****@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.com...
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.

"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


Apr 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Catherine Jones | last post: by
7 posts views Thread by Ken Allen | last post: by
1 post views Thread by CKane | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.