Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have added database to my project from server explorer -> Data connections -> Add connection. It creates database without any error but when I try to connect to the database, It gives very long error -
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I can't understand what is the error about. Please help me to solve this.

Following is the code. I am creating a table in the connected database.
C#
namespace OnlineReportSystem.Admin
{
    public partial class AdminRagistration : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\Users\\Avani\\Documents\\\\\\mynewdb.mdf;Integrated Security=True;Connect Timeout=30;");
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnNext1_Click(object sender, EventArgs e)
        {
           // Response.Write(TabContainer1.ActiveTabIndex.ToString());
            TabContainer1.ActiveTab = TabContainer1.Tabs[TabContainer1.ActiveTabIndex + 1];
        }

        protected void btnCreateSchema_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Create table " + txtSchema.Text + ";";
            cmd.CommandType = CommandType.Text;
            try
            {
                using (con)
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Response.Write("successfull");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }
    }
}
Posted
Updated 10-Jun-15 18:29pm
v3
Comments
PIEBALDconsult 10-Jun-15 11:47am    
Check your connection string.
Dolly Nimavat 10-Jun-15 12:25pm    
I have copied connection string from properties panel.

Double check it.
Look at the connection you established in VS via Server explorer and make sure it works.
If it does, highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file. If the connection string works directly in VS, then it'll work in your app as well, provided it's unchanged.
 
Share this answer
 
Comments
Dolly Nimavat 10-Jun-15 12:23pm    
I have done this. I have copied connection string from properties panel.
OriginalGriff 10-Jun-15 13:59pm    
Show us the code where you are using the connections string: there has to be something wrong with the *way* you are using it then.
What does the debugger say the string looks like when the code is running?
Dolly Nimavat 11-Jun-15 0:27am    
I have updated the question. Hope it will help you to understand better.
there's some problem with your SQL connection, which you are making..
Try this Fresh again.. After you choose your data Source and click continue on the Add connection dialog box you filling the Server name and select the Database name . On the bottom left there is a Test connection. Click on this Test Connection button to check if you can connect to Database. If not..

You need check the following .
a. Go to Services and Make sure SQL Server Service is running
b. If a named instance, make sure SQL Server browser service is running
c. Make sure SQL Server is configured to allow remote connections
d. Examine the SQL Server error log for messages confirming that SQL is listening on the expected network interfaces and ports
e. Test server connectivity with PING from the client machine
f. Test port connectivity using TELNET or PowerShell to the server and port (from step 4) from the client machine.
For example
a. TELNET <server-name> 1433
b. PowerShell: 1433 | % { echo ((new-object Net.Sockets.TcpClient).Connect("YourServerName",$_)) "server listening on TCP port $_" }

g. Check firewall settings if step 5 or 6 connectivity test fails

hope this helps
 
Share this answer
 
Comments
Dolly Nimavat 11-Jun-15 0:26am    
I don't know the server name. So I add new database using add connection instead of using 'create new sql server database'. In 'ADD CONNECTOIN' server name is not asked. As I add new databse instead of existing one, I can't test connection.
Hussain Patel 11-Jun-15 0:31am    
Pleas use this link for more details on connecting to database.. This might help you
https://msdn.microsoft.com/en-us/library/ms233763.aspx

Hope this helps..
Dolly Nimavat 11-Jun-15 0:32am    
Will it be installation problem? I have installed VS 2013. So all sql server commponents are those installed automatically in this setup. There was no error in installation.
Dolly Nimavat 11-Jun-15 0:44am    
On google I found the answer that says to do appropriate settings in sql server configuration manager. But i didn't find the Microsoft SQL server 2012 option in my start menu. Does it mean that sql server is not installed in my PC?
Hussain Patel 12-Jun-15 1:08am    
what is your VS Edition. Can you check in Control panel - Add remove programs as well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900