Showing posts with label connection. Show all posts
Showing posts with label connection. Show all posts

Thursday, March 29, 2012

Set Class Instance to Nothing During Instantiation?

I'm creating a class (using VB.NET) that must read data from a
database table during instantiation. If the data cannot be read
(connection problem, etc.), I want the instantiation to fail such that
the following "If" statement will evaluate to True:

Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...

What code in the class's constructor (Sub New) would make that happen?You can't. You're constructor can throw an exception...which how you
should do it anyways. if that isn't satisfactory, you can wrap your
constructor call in a static property of your class:

dim objGribble as cGribble = cGribble.CreateInstance()
if objGriggle is nothing then..

where CreateInstance() looks something like:

Public shared Property CreateInstance() as CGribble
get
try
return new cGribble()
catch ex as YourCustomException 'for the love of god don't
swallow this exception
return null
end try
end get
end property

karl

"Jeff Carver" <jeff_carver@.hotmail.com> wrote in message
news:b62a6c2a.0408240258.302ab414@.posting.google.c om...
> I'm creating a class (using VB.NET) that must read data from a
> database table during instantiation. If the data cannot be read
> (connection problem, etc.), I want the instantiation to fail such that
> the following "If" statement will evaluate to True:
> Dim objGribble as New cGribble()
> If objGribble Is Nothing Then ...
> What code in the class's constructor (Sub New) would make that happen?
Or, here's another method. Do a Web Search on "Factory Pattern"

You can implement it kinda like this

Public Class MyClass
Private Sub New()
' You can not directly instantiate this class
End Sub

Public Shared Function GetInstance(param1 as string, param2 as integer,
...) as MyClass
Dim result as new MyClass 'Since its private, the constructor can be
called inside the class
''' Code to possibly load from database
If CanLoadFromDB then
Return result
Else
Return Nothing
End If
End Function

End Class

Instead of :
Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...

You'll end up with:
Dim objGribble as cGribble = cGribble.GetInstance()
If objGribble Is Nothing Then ...

Now this will work. If you're totally set about this type of approach, then
go for it, dude! However, if you use the approach that Carl mentioned, you
do get one major benefit. All you know from this approach is that the class
can not be created. Was it because of a database error? Was it because of
a division Error? What about Solar Flares? If you throw an exception
inside of the constructor, you can catch it and inform the user about the
error, as it was thrown, as opposed to the assumption that it was error "X".

"Jeff Carver" <jeff_carver@.hotmail.com> wrote in message
news:b62a6c2a.0408240258.302ab414@.posting.google.c om...
> I'm creating a class (using VB.NET) that must read data from a
> database table during instantiation. If the data cannot be read
> (connection problem, etc.), I want the instantiation to fail such that
> the following "If" statement will evaluate to True:
> Dim objGribble as New cGribble()
> If objGribble Is Nothing Then ...
> What code in the class's constructor (Sub New) would make that happen?
Thanks, Karl and David. These are very interesting approaches, and
they're definitely going into my snippet file! I'm not sure yet which
I'll use for this current project, but I imagine I'll be using them
both in different situations in the future.

Set Class Instance to Nothing During Instantiation?

I'm creating a class (using VB.NET) that must read data from a
database table during instantiation. If the data cannot be read
(connection problem, etc.), I want the instantiation to fail such that
the following "If" statement will evaluate to True:
Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...
What code in the class's constructor (Sub New) would make that happen?You can't. You're constructor can throw an exception...which how you
should do it anyways. if that isn't satisfactory, you can wrap your
constructor call in a static property of your class:
dim objGribble as cGribble = cGribble.CreateInstance()
if objGriggle is nothing then..
where CreateInstance() looks something like:
Public shared Property CreateInstance() as CGribble
get
try
return new cGribble()
catch ex as YourCustomException 'for the love of god don't
swallow this exception
return null
end try
end get
end property
karl
"Jeff Carver" <jeff_carver@.hotmail.com> wrote in message
news:b62a6c2a.0408240258.302ab414@.posting.google.com...
> I'm creating a class (using VB.NET) that must read data from a
> database table during instantiation. If the data cannot be read
> (connection problem, etc.), I want the instantiation to fail such that
> the following "If" statement will evaluate to True:
> Dim objGribble as New cGribble()
> If objGribble Is Nothing Then ...
> What code in the class's constructor (Sub New) would make that happen?
Or, here's another method. Do a Web Search on "Factory Pattern"
You can implement it kinda like this
Public Class MyClass
Private Sub New()
' You can not directly instantiate this class
End Sub
Public Shared Function GetInstance(param1 as string, param2 as integer,
...) as MyClass
Dim result as new MyClass 'Since its private, the constructor can be
called inside the class
''' Code to possibly load from database
If CanLoadFromDB then
Return result
Else
Return Nothing
End If
End Function
End Class
Instead of :
Dim objGribble as New cGribble()
If objGribble Is Nothing Then ...
You'll end up with:
Dim objGribble as cGribble = cGribble.GetInstance()
If objGribble Is Nothing Then ...
Now this will work. If you're totally set about this type of approach, then
go for it, dude! However, if you use the approach that Carl mentioned, you
do get one major benefit. All you know from this approach is that the class
can not be created. Was it because of a database error? Was it because of
a division Error? What about Solar Flares? If you throw an exception
inside of the constructor, you can catch it and inform the user about the
error, as it was thrown, as opposed to the assumption that it was error "X".
"Jeff Carver" <jeff_carver@.hotmail.com> wrote in message
news:b62a6c2a.0408240258.302ab414@.posting.google.com...
> I'm creating a class (using VB.NET) that must read data from a
> database table during instantiation. If the data cannot be read
> (connection problem, etc.), I want the instantiation to fail such that
> the following "If" statement will evaluate to True:
> Dim objGribble as New cGribble()
> If objGribble Is Nothing Then ...
> What code in the class's constructor (Sub New) would make that happen?
Thanks, Karl and David. These are very interesting approaches, and
they're definitely going into my snippet file! I'm not sure yet which
I'll use for this current project, but I imagine I'll be using them
both in different situations in the future.

Set connection string of table adapter dynamically

Hello .
I am stucked with the following problem and hope you can help me :
- My ASP.NET 2.0 application has one master database .
- This master database has a Databases table with list of children
databases .
- All children databases have the same schema ( only the data is
different ) .
My application connects to the Master database first (
connection String of this database should be read from the C:\WINDOWS
\XXX.INI file).
Login web page will present drop-down list of sub-databases ( which
will be
retrieved from the Master database ) and then will connect to
specified sub-database and will display data
from this database in a GridView .
I would like to be able to bind this GridView to the Data Source
Objects , to edit all columns during design time , and to set the
connection string of corresponding table adapter programmatically
during run-time .
How can I do this ?
I found several references to the connection string ( for example ,
via appSettings) , but I got an error message that this is read only
parameter .
Please advice .
Thanks in advance for your help ,
Orit Chanukov .Keep it in a session variable.
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Orit" <oritc123@.gmail.com> wrote in message
news:1183138531.120152.59480@.a26g2000pre.googlegroups.com...
> Hello .
> I am stucked with the following problem and hope you can help me :
> - My ASP.NET 2.0 application has one master database .
> - This master database has a Databases table with list of children
> databases .
> - All children databases have the same schema ( only the data is
> different ) .
> My application connects to the Master database first (
> connection String of this database should be read from the C:\WINDOWS
> \XXX.INI file).
> Login web page will present drop-down list of sub-databases ( which
> will be
> retrieved from the Master database ) and then will connect to
> specified sub-database and will display data
> from this database in a GridView .
> I would like to be able to bind this GridView to the Data Source
> Objects , to edit all columns during design time , and to set the
> connection string of corresponding table adapter programmatically
> during run-time .
> How can I do this ?
> I found several references to the connection string ( for example ,
> via appSettings) , but I got an error message that this is read only
> parameter .
> Please advice .
> Thanks in advance for your help ,
> Orit Chanukov .
>

Set connection string of table adapter dynamically

Hello .

I am stucked with the following problem and hope you can help me :

-My ASP.NET 2.0 application has one master database .
-This master database has a Databases table with list of children
databases .
-All children databases have the same schema ( only the data is
different ) .
My application connects to the Master database first (
connection String of this database should be read from the C:\WINDOWS
\XXX.INI file).
Login web page will present drop-down list of sub-databases ( which
will be
retrieved from the Master database ) and then will connect to
specified sub-database and will display data
from this database in a GridView .
I would like to be able to bind this GridView to the Data Source
Objects , to edit all columns during design time , and to set the
connection string of corresponding table adapter programmatically
during run-time .
How can I do this ?
I found several references to the connection string ( for example ,
via appSettings) , but I got an error message that this is read only
parameter .
Please advice .
Thanks in advance for your help ,
Orit Chanukov .Keep it in a session variable.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Orit" <oritc123@.gmail.comwrote in message
news:1183138531.120152.59480@.a26g2000pre.googlegro ups.com...

Quote:

Originally Posted by

Hello .
>
I am stucked with the following problem and hope you can help me :
>
- My ASP.NET 2.0 application has one master database .
- This master database has a Databases table with list of children
databases .
- All children databases have the same schema ( only the data is
different ) .
My application connects to the Master database first (
connection String of this database should be read from the C:\WINDOWS
\XXX.INI file).
Login web page will present drop-down list of sub-databases ( which
will be
retrieved from the Master database ) and then will connect to
specified sub-database and will display data
from this database in a GridView .
I would like to be able to bind this GridView to the Data Source
Objects , to edit all columns during design time , and to set the
connection string of corresponding table adapter programmatically
during run-time .
How can I do this ?
I found several references to the connection string ( for example ,
via appSettings) , but I got an error message that this is read only
parameter .
Please advice .
Thanks in advance for your help ,
Orit Chanukov .
>

Monday, March 26, 2012

set default schema on connection string

Hi,
We have a aps.net 2.0 web application and sqlserver 2005.
We keep the connection string in the web.config file.
We need to set the for the web application...
so we do not have to hard code the default schema in every call.
Is it possible to set default schema in the connection string?
Thanks
NalakaHi,
can't you set default schema for the SQL Server user which you use for the
ASP.NET application?
Sample from Books Online
USE AdventureWorks;
ALTER USER Abolrous WITH DEFAULT_SCHEMA = Purchasing;
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Nalaka" <nalaka12@.nospam.nospam> wrote in message
news:uGEIEY5IGHA.2708@.tk2msftngp13.phx.gbl...
> Hi,
> We have a aps.net 2.0 web application and sqlserver 2005.
> We keep the connection string in the web.config file.
> We need to set the for the web application...
> so we do not have to hard code the default schema in every call.
> Is it possible to set default schema in the connection string?
>
> Thanks
> Nalaka
>

set default schema on connection string

Hi,
We have a aps.net 2.0 web application and sqlserver 2005.
We keep the connection string in the web.config file.

We need to set the for the web application...
so we do not have to hard code the default schema in every call.

Is it possible to set default schema in the connection string?

Thanks
NalakaHi,

can't you set default schema for the SQL Server user which you use for the
ASP.NET application?

Sample from Books Online

USE AdventureWorks;
ALTER USER Abolrous WITH DEFAULT_SCHEMA = Purchasing;

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Nalaka" <nalaka12@.nospam.nospam> wrote in message
news:uGEIEY5IGHA.2708@.tk2msftngp13.phx.gbl...
> Hi,
> We have a aps.net 2.0 web application and sqlserver 2005.
> We keep the connection string in the web.config file.
> We need to set the for the web application...
> so we do not have to hard code the default schema in every call.
> Is it possible to set default schema in the connection string?
>
> Thanks
> Nalaka

Thursday, March 22, 2012

Set Maximum Pool Size in web.config

I would like to allow more connection to SQL servers from the web
application.

Do you know what is the code to set maximum pool size in web.config file?

ThanksA sample connection string for SQL Server:
"Data Source=(local);Initial Catalog=pubs;User ID=sa;Password=;Max Pool Size=75;Min Pool Size=5;"

Tiendq,
tien@.tienonsoftware.com
You can set that in SQL Connection String...e.g

"Server=(local); ........; Database=Northwind; Max Pool Size=45; Min Pool Size=10

When a connection is opened and a pool is created, multiple connections are added to the pool to bring the connection count to the configured minimum level. Connections can be subsequently added to the pool up to the configured maximum pool count. When the maximum count is reached, new requests to open a connection are queued for a configurable duration.

Avneesh Kuma
avneesh@.helloprojects.com
Default value of Max Pool size is 100. You can set it to a higher number also so far as performance of the server is not a issue..

A Detailed article for the same can be found at
http://techrepublic.com.com/5100-6329_11-5034285-2.htm

Regard
Avneesh
Is there any issue if I set the maximum to be 1000?

"Do Quyet Tien" <tien@.tienonsoftware.com> wrote in message
news:47D18900-BF7C-4141-9753-452ED3D57EA0@.microsoft.com...
> A sample connection string for SQL Server:
> "Data Source=(local);Initial Catalog=pubs;User ID=sa;Password=;Max Pool
Size=75;Min Pool Size=5;"
> Tiendq,
> tien@.tienonsoftware.com