Hi All,

First time I am posting, so please forgive me if I miss out any information while asking you my question. I have a .net web app that I am building in 2.0 framework. I have a code-behind file that calls a method in a business logic class. The method in the class talks to the database through a database access layer. Through the database access layer I create a tableadapter through SPs in the database. I need to return a datatable to the code-behind class for the front layer. However I do not know how to handle returning an empty datatable in case I encounter that.

thanks much in advance

<System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, False)> _
Public Function MenuControl(ByVal Username As String, ByVal Password As String) As EFAST.tbl_UsersDataTable
' Create a new datatable instance for the dataset from the getdataby method
Dim datatableUserLogin As EFAST.tbl_UsersDataTable = Adapter.GetDataByUsername(Username)
If datatableUserLogin.Count = 0 Then
' no matching record found, return 0 this is where I have the problem
Return Nothing 
End If
Dim datarowUserLogin As EFAST.tbl_UsersRow = datatableUserLogin(0)
If some business logic Then
' matching record found, return userid
Return datatableUserLogin
End If
End Function

Recommended Answers

All 4 Replies

Could you not test in a logic statement if the row count is not > 0 than ignore?

dude i didnt understand u well.
but i will assume that u r asking how to return empty datatable.

i dont remember i did that since i always use dataset. in my case i return ds without testing and in the code behind i make the login test according to
if (ds.tables(0).rows.count<> 0) then
...
else
..
end if

i hope that answers ur question

sam

dude i didnt understand u well.
but i will assume that u r asking how to return empty datatable.

i dont remember i did that since i always use dataset. in my case i return ds without testing and in the code behind i make the login test according to
if (ds.tables(0).rows.count<> 0) then
...
else
..
end if

i hope that answers ur question

sam

Thanks Sam, and your assumption is correct, I am trying to see how to return an empty datatable, From what I have read I cannot do that and instead I should throw out an exception for the code behind to handle in a try catch expression. My code compiles but I still get a warning, so not sure what to do. Can you please explain to me how you did as a dataset ...

thanks
-Raj

ok dude

what i did is i have a function return dataset (ds) inside a data access layer.

now in the code behind as i write above i make the login condition for the number of the rows in the table number 0 inside in the ds.

if (ds.tables(0).rows.count<> 0) then
not empty ds

else
empty ds
end if

if the rows greater than 0 then the ds isnt empty else its empty.

i dont use try and catch.

sam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.