Prompt user for connection string

M

McGeeky

Hi.

I am using SQLDataSources with the connection string hardcoded in the
web.config file under the connectionStrings element. However, I want users
to be able to enter a username and password on a login page which I want to
subsequently use in the connection string. Is that possible?

Thanks.
 
M

Mark Rae

I am using SQLDataSources with the connection string hardcoded in the
web.config file under the connectionStrings element. However, I want users
to be able to enter a username and password on a login page which I want
to
subsequently use in the connection string. Is that possible?

Yes - store the username and password (encrypted, if possible) in the
Session variable, and then just use the String.Replace() method on the
existing connection string.
 
E

Eliyahu Goldin

Yes, you need to provide some gui for entering the credentials, make the
connection string out of the user input and save it in a session variable
where it will be available for all other pages. You may consider using the
Login control.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
M

McGeeky

Thanks Mark and Eliyahu.

When replacing the existing connection string, will that apply to that
user's session only or is it a global change?
 
M

Mark Rae

When replacing the existing connection string, will that apply to that
user's session only or is it a global change?

It won't be a global change unless you actually write the values back to the
web.config!

Obviously, this would be utter lunacy, as it would keep getting overwritten
every time somebody logged in... :)

Also, I have a feeling this would cause your app to cycle (not 100% sure
about this), so that really is the very last thing you want to do!

You could store the modified connection string in a Session variable and
then fetch it from there every time you need it, rather then from
web.config...
 
M

McGeeky

Hi Mark,

That doesn't sound like a great idea! I suppose what I mean is this;
currently when dropping a SqlDataSource control on to my web page I specify
the connection string to use which is defined in the web.config file. Will I
still be able to use this technique when creating a custom connection
string?

Thanks.
 
M

Mark Rae

when dropping a SqlDataSource control on to my web page

Oh right... Sorry, I have no idea - I never go anywhere near those
controls...

However, if you are able to specify a value for it, surely you are able to
specify another value for it, or is it all done at design time...?
 
M

McGeeky

Hi Mark,

I think I can set the connection string of a SqlDataSource in the Page_Load
method. This should do the trick. Only issue with this is it means some
initialization code at the start of every page. Shame because I liked the
configuration only approach.
 
M

Mark Rae

I think I can set the connection string of a SqlDataSource in the
Page_Load
method. This should do the trick. Only issue with this is it means some
initialization code at the start of every page.

Create a page base class and have all your pages inherit from it...
Shame because I liked the configuration only approach.

Each to their own... I find it immensely restrictive...
 
J

Juan T. Llibre

Sure, it's possible.

Make sure you're protected regarding SQL injection attacks, though.



Juan
====
 
H

Hans Kesting

Hi.
I am using SQLDataSources with the connection string hardcoded in the
web.config file under the connectionStrings element. However, I want
users to be able to enter a username and password on a login page
which I want to subsequently use in the connection string. Is that
possible?

Thanks.

One disadvantage of this: as every user has his own connection string, you
don't benefit as much from connection pooling. This means your app will use
a higher number of database connections than really needed.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top