Database connection not established in published asp.net website - c#

I have developed a web application in asp.net , and have published that website in iis 7.
I have given full access to the published folder and it works perfectly.When i browse my published application the login page has been loaded initially. If i fill the details and tried to login there connection fails.SQL connection has noot been opened
webConfig i have
<add name="SQLSERVER2008ConString" connectionString="Data Source=(local);Initial Catalog=REPOSITY;Integrated Security=True" providerName="System.Data.SqlClient"/>
and for opening connection
SqlConnection con = new SqlConnection(conString);
con.Open();
here the connection is not getting opened.But it works perfectly in my development environment.But not working after i published the code.
this how i connect my sql in local
Note:
Development environment is my system.
Used SQL Server 2008 and that is also in my system.
Published the website from my system only.
thanks

use this code
String conString= System.Configuration.ConfigurationManager.ConnectionStrings["SQLSERVER2008ConString"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
con.Open();

Are you sure your SQL Local Login Name is (local) ? Check the name once. But If Yes, then try this: <add name="SQLSERVER2008ConString" connectionString="Data Source=(local);Initial Catalog=REPOSITY;Integrated Security=True" User Id = [ENTER YOUR WINDOWS LOGIN USERNAME];Password = [ENTER YOUR WINDOWS LOGIN PASSWORD]"/>

Related

How to create connection for mdf files

I am creating a Windows application with a local database.
Now I need to define a connection string for that local database in C#.
What is the connection string for local .mdf files in Winforms C#?
Do the following steps:
In your Server Explorer window right click on the database that
you've created and select Properties.
in the Properties window copy the value of the connection string
property and paste it to your application.
It should be something like this:
Data Source=(localdb)\v11.0;Initial Catalog=yourDB;Integrated Security=True
Just you need to add one extra \ to it to work in c#:
string address = "Data Source=(localdb)\\v11.0;Initial Catalog=yourDB;Integrated Security=True";
You could also have a look at
The Connection Strings Reference.
Here you go
<add name="YourDataBase" connectionstring="Data Source=.\SQLEXPRESS; AttachDbFilename=Path\YourDataFile.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True" />
Also you can visit connectionstring.Com as well for more.
Use SqlServeCe as a namespace and then give the following codings
using System.Data.SqlServerCe;
SqlCeConnection conn = new SqlCeConnection(#"Data Source=D:\PROJECT\data\db.mdf;");

Named Pipes error 40 in windows server 2003

I have write one .Net application in C# I use these strings to connect
str = "Data Source=200.1xx.2yy.1zz ;Initial Catalog =minofom;uid =sa;pwd = abuelita";
and the same string for app.config:
<add name="MiniFinan.Properties.Settings.MiniFimConnectionString"
connectionString="Data Source=200.1xx.2yy.1zz ;Initial Catalog =minofom;uid=sa;pwd = abuelita" providerName="System.Data.SqlClient" />
All the forms open fine, when I'm out of the local network, the app and the reports work perfect, but when I install the app in the server or any machine of the local network the app show me this error:
“Named Pipes Provider, error: 40 - Could not open a connection to SQL Server”
The local server is a domain controller, I use this string for local area network:
str="Data Source=192.168.2.254 ;Initial Catalog =MinoFom;uid =sa;pwd = abuelita"
All the forms work, but when I call to any report not connect, I use the same string for the app.config. I have tested the same app in other Server 2003 (not Domain Controller) and the reports works, have any way of write the string for domain, any idea of how to fix this?
I have checked SQL Browser is online, the protocols : Shared Memory/ Named Pipes/TCPIP are installed and work. I think is something with the domain.
I have solved using two strings one for the forms :
str = #"Data Source=TORRE ;Initial Catalog =minofom;uid =sa;pwd = abuelita";
and other in the app.config for the reports :
connectionString="Data Source=MyDomain\TORRE;Initial Catalog=MinoFom;uid =sa;pwd = abuelita"
Its posible save more than one string in app.config , enjoy

how to connect a remote SQLserver using widows authentication from a .net application, if possible

currently I am using this connection string inside the app.config file of the application
add name="LightSailEntities" connectionString="metadata=res://*/LightSailEntities.csdl|res://*/LightSailEntities.ssdl|res://*/LightSailEntities.msl;provider=System.Data.SqlClient;provider connection string='data source=abc.xyz.com;initial catalog=LightSail;user id=LightSail; password=yourpasswordhere;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient"
The domain of .Net application and the domain of client, using .Net application, is different from domain of SQL server. I mentioned "using widows authentication" only because of, I have the access of the server machine(means I can use Remote Desktop Connection) on which the SQL server is installed.
For Windows Auth you don't need to set the User Id and Password but you do need to include 'Integrated Security=SSPI;'
Try:
add name="LightSailEntities" connectionString="metadata=res://*/LightSailEntities.csdl|res://*/LightSailEntities.ssdl|res://*/LightSailEntities.msl;provider=System.Data.SqlClient;provider connection string='data source=dev.shopcube.com;initial catalog=LightSail;Integrated Security=SSPI;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient"
There's a bit more info here:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(VS.71).aspx
You have to change ConnectionString to use Integrated Security=SSPI insetad of user and password
add name="LightSailEntities"
connectionString="metadata=res://*/LightSailEntities.csdl|res://*/LightSailEntities.ssdl|res://*/LightSailEntities.msl;
provider=System.Data.SqlClient;
provider connection string='data source=dev.shopcube.com;initial catalog=LightSail;Integrated Security=SSPI;MultipleActiveResultSets=True;App=EntityFramework'"
providerName="System.Data.EntityClient"
After that, look at the Identity set for the Application Pool of you application.
That user must be authorized to access your DB using Security\Logins inside Object Explorer pan of Management Studio.
Youo can use following code:
SqlConnection conn = new SqlConnection(Configuration.DBConn);
or if you use Linq2SQL:
DBContext ctx = new DBContext(Configuration.DBConn);
where in Configuration class DBConn string contains connection string to sql ie:
Data Source=XYZ\\DEV;Initial Catalog=YOURDB;Integrated Security=True;Connect Timeout=600;connection lifetime=600
Integrated Security=True tells that you want to use windows auth.

Getting a sql server error when tryin to access the wesite using the remote URL rather than the localhost

Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. Please make sure the user has a local user profile on the computer. The connection will be closed.
This is the error that I am getting. If I try to access the web site using the localhost it works fine. But, when I am usinfg it through the remote URL it throws this error.
I feel there is some thing wrong woth my web.config file.
The connection string I am using
In the C# code.....
SqlConnection conn = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Thesis_Database.mdf;Integrated Security=True;User Instance=True");
Please Help....
SqlDataReader rdr = null;
This is related to impersonation.
The user hitting your page is required to have a local user account on the machine, and since you have "User Instance=True", a copy of the master db will be copied to:
C:\Documents and Settings\<Username>\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS
And according to the error it seems the user you are accessing the web page with does not have an account.
Could it be that you are not running impersonation on the webserver, so it's actually trying to mount the db instance as the AppPool user?
I've had to add new line to my connection string in web.config file to fix the problem:
> <connectionStrings> add this to your
> web.confog file. <remove
> name="LocalSqlServer"/> <add
> name="LocalSqlServer"
> connectionString="Data
> Source=.\SQLExpress;Integrated
> Security=True;User
> Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf"/>
Then the IIS application pool I used an acount that has a local user profile (local system is an example) but this could make a security hole in your system.
go to iis manager.go toApplication Pool tab. on each and every Application pool Instance Click Advandced Settins and change the User Profie to "Local System". This should solve the issue.

How do I connect to an .mdf (Microsoft SQL Server Database File) in a simple web project?

Specifically, in VS 2008, I want to connect to a data source that you can have by right-clicking on the automatically-generated App_Data folder (an .mdf "database"). Seems easy, and it is once you know how.
A great resource I always keep around is connectionstrings.com.
It's really handy for finding these connection strings when you can't find an example.
Particularly this page applied to your problem
Attach a database file on connect to a local SQL Server Express instance
Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
So here's the answer from MSDN:
Choos[e] "Add New Data Source" from the
Data menu.[And follow the connection wizard]
Very easy, except that I have no Data menu. If you don't have a Data menu, do the following:
Click on Tools >> Connect to Database...
Select "Microsoft SQL Server Database File", take the default Data provider, and click OK
On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.
Test the connection. It'll be good. If you want to add the string to the web.config, click the Advanced button, and copy the Data Source line (at the bottom of the dialog box), and paste it into a connection string in the appropriate place in the web.config file. You will have to add the "AttachDbFilename" attribute and value. Example:
The raw text from the Advanced panel:
Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True
The actual entry in the web.config:
<add name="SomeDataBase" connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=C:\Development\blahBlah\App_Data\SomeDataFile.mdf;
Integrated Security=True; Connect Timeout=30; User Instance=True" />
Just one more -- i've always kept a udl file on my desktop to easily create and test connection strings. If you've never done it before - create a new text file and name it to connection.udl (the ext is the only important part). Open the file, start on the Provider tab and work your way through. Once you're happy with the connection rename the file giving it a .txt extension. Open the file and copy the string - it's relatively easy and lets you test the connection before using it.
<add name="Your Database" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Expanse.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
In your Login.aspx.cs (the code behind file for your login page in the submit button click event) add
string constr = #"Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\myData.mdf; Integrated Security=True; Connect Timeout=30;";
using (SqlConnection conn = new SqlConnection(constr))
string constr = ConfigurationManager.ConnectionStrings["myData"].ToString();
using (SqlConnection conn = new SqlConnection(constr))
{
sqlQuery=" Your Query here"
SqlCommand com = new SqlCommand(sqlQuery, conn);
com.Connection.Open();
string strOutput = (string)com.ExecuteScalar();
}

Categories