locked
Cannot connect to a database RRS feed

  • Question

  • Hi all,

    I am trying to connect to the database but it gives the error cannot connect to database.Login failed for user....

    This is my connection string in code behind :

     [string constring = "Data Source= localhost; Initial catalog= Database.mdf; Integrated Security= True";
                SqlConnection con = new SqlConnection(constring);]

    This is my connection string in web.config:

    <connectionStrings>
        <add name="Database" connectionString="Data Source= localhost;Initial catalog=Database.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    looking forward for answers,

    Thanks in advance!

    Monday, June 15, 2015 6:55 AM

Answers

  • Hello - I doubt over the connection string you are using:

    <connectionStrings>
        <add name="Database" connectionString="Data Source= localhost;Initial catalog=Database.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    In the above, you need to replace Database.mdf to just actual DatabaseName (For example master or your actual database ). Secondly you do not have specify provider name explicitly that is not required. Ideally your connection string should look like this:

    If using Windows Authentication:

    Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

    If using SQL Server Authentication:

    Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

    Hope this helps


    Good Luck!
    Please Mark This As Answer if it solved your issue.
    Please Vote This As Helpful if it helps to solve your issue

    Monday, June 15, 2015 7:04 AM

All replies

  • Hello - I doubt over the connection string you are using:

    <connectionStrings>
        <add name="Database" connectionString="Data Source= localhost;Initial catalog=Database.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    In the above, you need to replace Database.mdf to just actual DatabaseName (For example master or your actual database ). Secondly you do not have specify provider name explicitly that is not required. Ideally your connection string should look like this:

    If using Windows Authentication:

    Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

    If using SQL Server Authentication:

    Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

    Hope this helps


    Good Luck!
    Please Mark This As Answer if it solved your issue.
    Please Vote This As Helpful if it helps to solve your issue

    Monday, June 15, 2015 7:04 AM
  • Hi try this in your connection string

    Data Source=myServerAddress(local/IP/Name) ;Initial Catalog=myDataBase;Integrated Security=SSPI;
    User ID
    =myDomain\myUsername;Password=myPassword;

    Monday, June 15, 2015 7:07 AM
  • Thanks but it still gives the error in this line:

    con.Open();

    An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

    Monday, June 15, 2015 10:20 AM
  • Have you modified your Connection string as suggested? What's the error message of the inner exception?

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Monday, June 15, 2015 10:25 AM
    Answerer
  • Yes I did thank you...I used the connection string and then I put the code inside the try catch. It doesn't display the error anymore. But When I am entering values to the textboxes and click a button the page just refreshes instead of doing the action specified in a code
    • Edited by Alu Nonny Friday, June 19, 2015 9:40 PM
    Friday, June 19, 2015 9:40 PM
  • instead of doing the action specified in a code

    Then you may be have a bug in your code, but this is not longer a SQL Server related issue.

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Monday, June 22, 2015 7:23 AM
    Answerer