-2

Hi i am trying to connect to database with following code. But i am getting an error message saying

Cannot open database "CPL" requested by the login. The login failed. Login failed for user 'Dell-PC\Dell'.

It was working fine before.

C# Code

 protected void Page_Load(object sender, EventArgs e)
    {
        string CurrentDate = DateTime.Today.ToShortDateString();
        string CS = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            DataTable dtMatchDetails = new DataTable();
            string query = "Select MatchTeam1,MatchTeam2 from tblSchedule Where MatchDate =" + CurrentDate;
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dtMatchDetails);
            con.Close();
            da.Dispose();
        }

Connection String in Web.Config

<connectionStrings>
<add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;User Instance=true;database = CPL" 
     providerName="System.Data.SqlClient=" />
</connectionStrings>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
slash
  • 21
  • 5
  • Are you sure your connection string is right? – Soner Gönül Jan 10 '15 at 17:24
  • @SonerGönül Yes it was working fine before. – slash Jan 10 '15 at 17:25
  • What can cause the failure? Have you upgraded the SQL Server or something else related with this between successful and unsuccessful attempts of connect? Try connect without `User Instance=true`. Also you have extra symbol `=` in your provider name. – Hamlet Hakobyan Jan 10 '15 at 18:11
  • @HamletHakobyan I removed User instance = true and it worked. Thanks a lot. What is the possible reason of happening this? – slash Jan 10 '15 at 18:41

1 Answers1

0

This means that your connection string is ok. User and password are ok.

You need to grant access to the specific database.

For example, after logging in sql server as administrator:

USE CPL
EXEC sp_addrolemember 'db_datareader', '(your user name)'
user_0
  • 3,173
  • 20
  • 33