0

I have MS sql database in folder on my computer and i am trying to connect to that database from asp.net(c#) application but it is throwing errors. Any help please.

My Web.config

<connectionStrings>
<add 
<add name="con" connectionString="Server=kuldeep;Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\jambtjh.mdf;
Initial Catalog=jambtjh;Integrated Security=True; ID=sa;Password=123;" providerName="System.Data.SqlClient" />
<connectionStrings>

c# Code-

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["con2"].ConnectionString);
    SqlCommand cmd,cmd1;
    SqlDataReader dr,dr1;
    con.Open();
    string str = "select * from testtable";
    cmd = new SqlCommand(str, con);
}
10
  • SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ConnectionString); SqlCommand cmd,cmd1; Commented Mar 30, 2017 at 11:03
  • what errors,can you please paste them Commented Mar 30, 2017 at 11:04
  • Server Error in '/' Application. Keyword not supported: 'id'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentException: Keyword not supported: 'id'. Commented Mar 30, 2017 at 11:08
  • Thanks for the quick one i have shared the error @ TheGameiswar Commented Mar 30, 2017 at 11:09
  • I can't see where you're using id in ths code you have posted. Commented Mar 30, 2017 at 11:11

2 Answers 2

2

If you're using the *.mdf file in the App_Data folder of an ASP.NET app you can use this.

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;Integrated Security=True;MultipleActiveResultSets=True;

No authentication is required.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks@Vandita when using this (a) <add name="con" connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|jambtjh.mdf;Database=dbname; Trusted_Connection=Yes;" providerName="System.Data.SqlClient" /> .............The error is ---
erroro--System.Data.SqlClient.SqlException occurred HResult=0x80131904 Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Source=.Net SqlClient Data Provider StackTrace: <Cannot evaluate the exception stack trace>
This time another error for the ---- <add name="con" connectionString="Data Source=localhost;Initial Catalog=omnsin_gdb;User ID=omnsin_omnsin;Password=baby@1977" providerName="System.Data.SqlClient"/> <add name="updateKRA" connectionString="Data Source=kuldeep;Initial Catalog=jambtjh;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
error - System.Data.SqlClient.SqlException occurred HResult=0x80131904 Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Source=.Net SqlClient Data Provider StackTrace: <Cannot evaluate the exception stack trace>
@kuldeepsingh have you kept your .mdf file in App_Data folder of Application?
|
0

Thanks you @Vandita for you help. You are right id and password is not required. I have added few things to your answer and it is working for me. Just sharing the line here :

<add name="con" connectionString="Data Source=(LocalDb)\MSSQLLocalDB   AttachDbFilename=|DataDirectory|\jambtjh.mdf;
        Initial Catalog=jambtjh;Integrated Security=True" providerName="System.Data.SqlClient" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.