By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,742 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,742 IT Pros & Developers. It's quick & easy.

Problems putting database layer in class library. Please help!

P: n/a
Question about putting the data layer in a separate class library.

I keep reading that the data layer should be separated from the
presentation layer and put in its own class library. I am trying to
do this but running into a problem. I'm hoping someone can point me
in the right direction. I first create a class library (all code in
C#) and within this class library create a very simple Sql Server
Express database (.mdb) file. It's columns are [index] and miles with
index being the key value. I then generate a dataset by picking Add
New Item->DataSet. I drag my one table onto the dataset designer and
end up with a file called SimpleRunningDataSetDesigner.cs that has
(among other things) the following:

[System.ComponentModel.DataObjectMethodAttribute(Sy stem.ComponentModel.DataObjectMethodType.Fill,
true)]
public virtual int Fill(SimpleRunningDataSet.SimpleRunningDataTable
dataTable) {
this.Adapter.SelectCommand = this.CommandCollection[0];
if ((this.ClearBeforeFill == true)) {
dataTable.Clear();
}
int returnValue = this.Adapter.Fill(dataTable);
return returnValue;
}

Of course, there is much more code and I'd be happy to post any/all of
it. Anyhow, based on previous work where I had only 1 layer, this
looked promising.

I compiled the class library and then created a web application (file
based system). I added the class library as a reference and put an
objectdatasource on the main form. I chose the only dataadapter
available:
DataLayer.SimpleRunningDataSetTableAdapters.Simple RunningTableAdapter

I put a GridView on the page and used the objectdatasource mentioned
above.
But when I run the web page, I get the following error:
An attempt to attach an auto-named database for file C:\Documents and
Settings\Andrew\My Documents\Visual Studio 2005\WebSites
\SimpleThreeTieredArchitecture\WebApp\App_Data\App _Data\VerySimple.mdf
failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share.

Has anyone seen this before?

To verify the problem has something to do with using a separate class
library I copied the .mdb file to a brand new web app project and
created a dataset within that web app project using the copied .mdb
file. I again used an objectdatasource and gridview as above. When I
do everything in one webapp project I have no problem and the page
displays.

A couple of possibly relevant points: I notice that in the class
library when I create the dataset I get an .xsd file. However, it
appears to be just a design page and not an .xsd code page like you
get when you do the same exercise in the web app. Also, I get files
under the .xsd file called SimpleRunningDataSet.cs, *.Designer.cs,
*.xsc, and *.xss. When I do this exercise in the web app project, I
only get the .xsd file (design and code) and a .xss file. Also, I'm
not using IIS. I pick 'file system'for the web apps.

Can anyone point me to an article, book or tutorial explaining this
behavior? I'm sure it's something simple I need to fix but I've been
knocking my head against a wall for some time on this one. I have
seen mention of the error message above in the newsgroups but no
solution. My research leads me to suspect that it might be some sort
of permissions problem. Perhaps the WebApp does not have permission
to read the database that's in the class library layer? I did try
changing the permissions of every folder in sight to include the user
ASPNET. No luck. I also saw suggestions that the connectionString in
the class library might be wrong. To this end I changed it from:
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|
DataDirectory|\App_Data\VerySimple.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True"

to

C:\Documents and Settings\Andrew\My Documents\Visual Studio
2005\WebSites\SimpleThreeTieredArchitecture\DataLa yer\App_Data
\VerySimple.mdf

It made no difference.

If you've read this far, many thanks. Again, any comments, ideas,
flames, or suggestions for more appropriate newsgroups would be most
welcome. Eventually, I'd like to use the built in ASP.NET security
model (ASPNET.mdb) to check username, passwords, roles, etc. I've
already done this when everything is in one big webapp project, but I
suspect there will be problems putting that in the class library too!

Thanks,
Dave.

May 11 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.