Click to See Complete Forum and Search --> : VB.NET Data Access Layer


bhushan1980
November 17th, 2009, 01:46 PM
Hi all,
I am primarily used to work in C#(about 75% times). But I also have to work with VB.NET quite often for some justified reasons.
It seems that Visual Basic has a different way of working...or atleast VS 2008. You need not mention the namespace that you are working in as we can in C#. Let us say we have a project named MyProj, then the default top level namespace for that project is "namespace MyProj{....}". So, in C#, we can write all the code in namespace MyProj{...} braces. But, if we do the same in VB.NET as Namespace MyProj.....End Namespace, then it creates a namespace with the same name inside MyProj! So to solve this problem, what I did was, I just did not include the default top level namespace for that project at all. Likewise, there are a lot of things in VB.NET that are automatically done for us.

My Q is regarding the DAL article at:

http://www.codeproject.com/KB/database/CustomBindableObject.aspx


Since the code is in C#, it is summarized as under:



// The first part of the typed data set and the table adapter for Customers and Orders table
// is in CustomerOrderTDS.designer.cs file and the design is in CustomerOrderTDS.xsd

// This is the other part of the
using DCAF.DataLayer.CustomerOrderTDSTableAdapters;

namespace DCAF.DataLayer
{
partial class CustomerOrderTDS
{
public static CustomerOrderTDS GetCustomerOrders()
{
CustomersTableAdapter custAdapter = new CustomersTableAdapter();
OrdersTableAdapter ordAdapter = new OrdersTableAdapter();
CustomerOrderTDS ds = new CustomerOrderTDS();
custAdapter.Fill(ds.Customers);
ordAdapter.Fill(ds.Orders);
return ds;
}
}
}

//......

// This is the service class that connects the DAL classes to the Business Objects
using System;
using System.Collections.Generic;
using System.Text;
using DCAF.DataLayer.CustomerOrderTDSTableAdapters;

namespace DCAF.DataLayer
{
public class CustomerOrderService
{
#region "Storage"
private CustomersTableAdapter m_customersAdapter = null;
protected CustomersTableAdapter CustomersAdapter
{
get
{
if (m_customersAdapter == null)
{
m_customersAdapter = new CustomersTableAdapter();
}
return m_customersAdapter;
}
}
private OrdersTableAdapter m_ordersAdapter = null;
protected OrdersTableAdapter OrdersAdapter
{
get
{
if (m_ordersAdapter == null)
{
m_ordersAdapter = new OrdersTableAdapter();
}
return m_ordersAdapter;
}
}
#endregion "Storage"

#region "Public Interface
public CustomerOrderTDS.CustomersDataTable GetCustomers()
{ return CustomersAdapter.GetData(); }
public CustomerOrderTDS.OrdersDataTable GetOrders()
{ return OrdersAdapter.GetData(); }
public CustomerOrderTDS GetCustomerOrders()
{ return CustomerOrderTDS.GetCustomerOrders(); }
#endregion "Public Interface"
}
}




The Corresponding VB.NET code is as under...


' The first part of the typed data set and the table adapter for Customers and Orders table
' is in CustomerOrderTDS.designer.cs file and the design is in CustomerOrderTDS.xsd
' This is the other part of the
Imports DCAF.DataLayer.CustomerOrderTDSTableAdapters

Namespace DCAF.DataLayer
Partial Class CustomerOrderTDS
Public Shared Function GetCustomerOrders() As CustomerOrderTDS
Dim custAdapter As New CustomersTableAdapter()
Dim ordAdapter As New OrdersTableAdapter()
Dim ds As New CustomerOrderTDS()
custAdapter.Fill(ds.Customers)
ordAdapter.Fill(ds.Orders)
Return ds
End Function
End Class
End Namespace

'...

' This is the service class that connects the DAL classes to the Business Objects
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports DCAF.DataLayer.CustomerOrderTDSTableAdapters

Namespace DCAF.DataLayer
Public Class CustomerOrderService
#Region "Storage"

Private m_customersAdapter As CustomersTableAdapter = Nothing
Protected ReadOnly Property CustomersAdapter() As CustomersTableAdapter
Get
If m_customersAdapter Is Nothing Then
m_customersAdapter = New CustomersTableAdapter() 'Error here...
End If
Return m_customersAdapter
End Get
End Property

Private m_ordersAdapter As OrdersTableAdapter = Nothing
Protected ReadOnly Property OrdersAdapter() As OrdersTableAdapter
Get
If m_ordersAdapter Is Nothing Then
m_ordersAdapter = New OrdersTableAdapter() 'Error here...
End If
Return m_ordersAdapter
End Get
End Property

#End Region

#Region "Public Interface

Public Function GetCustomers() As CustomerOrderTDS.CustomersDataTable
Return CustomersAdapter.GetData()
End Function

Public Function GetOrders() As CustomerOrderTDS.OrdersDataTable
Return OrdersAdapter.GetData()
End Function

Public Function GetCustomerOrders() As CustomerOrderTDS
Return CustomerOrderTDS.GetCustomerOrders()
End Function

#End Region
End Class

End Namespace



The C# application was developed in VS 2005 and I got it migrated to VS 2008.

While the code in C# works fine, the one in VB.NET code has underlines indicating the error (I have commented the 2 lines):

"value of type DCAF.DataLayer.CustomerOrderTDSTableAdapters.CustomersTableAdapter cannot be converted to 'i-dimensional array of DCAF.DataLayer.CustomerOrderTDSTableAdapters.CustomersTableAdapter'

and similar error for Orders data adapter

"value of type DCAF.DataLayer.CustomerOrderTDSTableAdapters.OrdersTableAdapter cannot be converted to 'i-dimensional array of DCAF.DataLayer.CustomerOrderTDSTableAdapters.OrdersTableAdapter'


Is it dependent on the contents of the data table, that the object returned by table adapter will vary accordingly?
Is there another way to deal with this problem? I doubt that I am doing something wrong in syntax? Or is it wrong in C# as well?

Also, I do not have a working sample yet; as I am saying this based on the underlines I am getting in the IDE.

Thanks & regards,
Bhushan

Craig Gemmill
November 17th, 2009, 02:53 PM
Are you absolutely certain that those are the lines that are throwing the error, and that the code you pasted is the same code that is causing your issue?

I only ask because your code is perfectly valid and should not be causing those errors.

I downloaded the sample C# code and re-wrote the exact example you have in vb.net, but cannot reproduce the issue.

The only thing I didn't do and I don't know if you did or not. I did not covert the rest of the project to vb.net. I simply referenced the C# project from my VB.NET project.

bhushan1980
November 17th, 2009, 08:40 PM
Well...First of all thank you vert much for the reply
My problem is, I am working on a similar sample. I should have told you that. But due to some rules of Non disclosure agreement, I cannot post any code here...that is why I did some analogy. If you say, that the code working is fine, then I would go ahead with it. Because, it is just the intellisense error in the form of a underlining & a tool-tip when you hover over the class and not compiler error. As evident from the past, these errors go away when you finish the application. However, the application is a lot bigger than what the codeproject link has and I was trying to save myself some debugging by getting rid of this early.
Having said that, let us consider that portion of CustomerOrderService again:

' m_customersAdapter is an instance and I am having problem instantiating it...
' m_OrdersAdapter is also having the same issue
'I was asking, if there is another syntax for declaring the same. I am C# mostly (about 75%)
'But I have worked with VB.NET without much problems before
'The public region is reproduced here...
....
....
Public Function GetCustomers() As CustomerOrderTDS.CustomersDataTable
Return CustomersAdapter.GetData()
End Function

Public Function GetOrders() As CustomerOrderTDS.OrdersDataTable
Return OrdersAdapter.GetData()
End Function

Public Function GetCustomerOrders() As CustomerOrderTDS
Return CustomerOrderTDS.GetCustomerOrders()
End Function

'I cannot use the "." operator on the properties...
'Ofcourse I can work without intellisense, but it would be a nightmare for debugging


Since you are more acquainted with VB.NET, I would like to ask you the possibilities here...which I will have to eventually deal with while debugging

Thanks & regards,
Bhushan

dglienna
November 17th, 2009, 11:04 PM
Intellisense works because of this line:

using DCAF.DataLayer.CustomerOrderTDSTableAdapters;

in VB.Net

Imports DCAF.DataLayer.CustomerOrderTDSTableAdapters

does the same thing. it gives access to protected methods

bhushan1980
November 17th, 2009, 11:45 PM
I know...
What I was suggesting was ...it was not working and there is something definitely wrong
And I have posted the tool-tip error message I am seeing on hovering over the underlined member
I want to know the situations where in I can get it....? In the meantime, I will also try and find out what the issue is and post it here if I happen to find it.
Thanks and regards,
Bhushan

bhushan1980
November 17th, 2009, 11:48 PM
In the code that I have posted, I am not able to see the GetData() method and GetCustomerOrders() method after I put a "." after the protected properties in the public methods.
I attribute this error to the underlines and the tool-tip error message I am getting there...
I hope that clears the problem just a little further...
Thanks,
Bhushan

Craig Gemmill
November 18th, 2009, 12:16 AM
Yeah, it's all a guessing game without understanding more about how you've coded this thing.

This code will produce the 1-dimensional array ERROR, as it's trying to assign a non-array variable to an array.


Dim thisIsAnArray() As Object
Dim thisIsNotAnArray As Object


thisIsAnArray = thisIsNotAnArray

bhushan1980
November 18th, 2009, 07:59 AM
Hi,

You won't find a more dumb person than me right now! Sorry for all your time who attended this thread.

At the same time, thanks to Craig Gemill. Your last post did the trick for me.

The problem was the right sort of definition. I am really livid with myself.
On reading your thread, of which I already had an idea but did not know where to start and this is where CG has always chipped in.

I have always wondered, how to decide what constructors do we put a parenthesis and not. If I had a slightest idea, I would not have run into this problem.

I am reproducing the code from previous posts in this thread in VB.NET here:

Imports DCAF.DataLayer.CustomerOrderTDSTableAdapters

Namespace DCAF.DataLayer
Partial Class CustomerOrderTDS
Public Shared Function GetCustomerOrders() As CustomerOrderTDS
Dim custAdapter As New CustomersTableAdapter() 'Parenthesis ...right or wrong?
Dim ordAdapter As New OrdersTableAdapter() 'Parenthesis ...right or wrong?
Dim ds As New CustomerOrderTDS() 'Parenthesis ...right or wrong?
custAdapter.Fill(ds.Customers)
ordAdapter.Fill(ds.Orders)
Return ds
End Function
End Class
End Namespace

'...

' This is the service class that connects the DAL classes to the Business Objects
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports DCAF.DataLayer.CustomerOrderTDSTableAdapters

Namespace DCAF.DataLayer
Public Class CustomerOrderService
#Region "Storage"

'In the following properties, I forgot to mention the return type!!!
'There was nothing like "As ....type"
'As a result, every property was treated like having a return type Object
'That is why my method won't show up

Private m_customersAdapter As CustomersTableAdapter = Nothing
Protected ReadOnly Property CustomersAdapter() As CustomersTableAdapter
Get
If m_customersAdapter Is Nothing Then
m_customersAdapter = New CustomersTableAdapter 'Removed () here...???
End If
Return m_customersAdapter
End Get
End Property

Private m_ordersAdapter As OrdersTableAdapter = Nothing
Protected ReadOnly Property OrdersAdapter() As OrdersTableAdapter
Get
If m_ordersAdapter Is Nothing Then
m_ordersAdapter = New OrdersTableAdapter 'Removed () here...???
End If
Return m_ordersAdapter
End Get
End Property

#End Region

#Region "Public Interface

Public Function GetCustomers() As CustomerOrderTDS.CustomersDataTable
Return CustomersAdapter.GetData()
End Function

Public Function GetOrders() As CustomerOrderTDS.OrdersDataTable
Return OrdersAdapter.GetData()
End Function

Public Function GetCustomerOrders() As CustomerOrderTDS
Return CustomerOrderTDS.GetCustomerOrders()
End Function

#End Region
End Class

End Namespace


After doing the changes, I can see everything working. The sample is not yet complete. But I am sure, it will work fine. Please correct me if I am still doing something wrong....

But I sinerely hope that Craig's post came to my rescue. Thisisanarray and thisisnotanarray!!! 2 objects defined...it cannot get any simpler

Too much of work can make you do dumb things as these...is what I have learned here.

Thanks again,
Bhushan