Click here to Skip to main content
16,004,686 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
This is the exception that i get every time when i click the register button
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). The number of line in which i get error is line 107 (con.Open).
The code is below:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;
using System.Data;
using System.Text.RegularExpressions;
namespace Sup
{
   /// <summary>
   /// Interaction logic for Signup.xaml
   /// </summary>
   public partial class Signup : Window
   {
      public Signup()
      {
          InitializeComponent();
      }
      private void Reset()
      {
         textBoxAddress.Text = "";
         textBoxEmail.Text = "";
         textBoxFirstName.Text = "";
         textBoxLastName.Text = "";
         passwordBox1.Password = "";
         passwordBoxConfirm.Password = "";
      }
      private void registerButton_Click(object sender, RoutedEventArgs e)
      {
         if (textBoxEmail.Text.Length == 0)
         {
            MessageBox.Show("Enter an email.");
            textBoxEmail.Focus();
         }

         else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
         {
            MessageBox.Show("Enter a valid email.");
            textBoxEmail.Select(0, textBoxEmail.Text.Length);
            textBoxEmail.Focus();
         }
         else
         {
            string firstname = textBoxFirstName.Text;
            string lastname = textBoxLastName.Text;
            string email = textBoxEmail.Text;
            string password = passwordBox1.Password;

            if (passwordBox1.Password.Length == 0)
            {
               MessageBox.Show("Enter password.");
               passwordBox1.Focus();
            }
            else if (passwordBoxConfirm.Password.Length == 0)
            {
               MessageBox.Show("Enter Confirm password.");
               passwordBoxConfirm.Focus();
            }
            else if (passwordBox1.Password != passwordBoxConfirm.Password)
            {
               MessageBox.Show("Confirm password must be same as password.");
               passwordBoxConfirm.Focus();
            }
            else
            {
               string address = textBoxAddress.Text;
               SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=logins;Integrated Security=SSPI");

               con.Open();

               SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con);

               cmd.CommandType = CommandType.Text;
               cmd.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("You have Registered successfully.");
               Reset();
            }
         }
      }
   }
}


[edit]indexation reduced[/edit]
Posted
Updated 18-Feb-14 0:46am
v3
Comments
Sampath Lokuge 18-Feb-14 5:55am    
Is this a local sql server or remote one ?
Heyrbiar 18-Feb-14 6:32am    
local
Sampath Lokuge 18-Feb-14 6:50am    
Sql express or compact or sql or what ?

Almost certainly, it's your connection string - it looks odd to me!

Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) 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.

And don't "hard-code" connections strings: it means when you release the software, you have to re-compile it for each server....
 
Share this answer
 
Comments
Heyrbiar 18-Feb-14 6:36am    
i tried that but same exception
OriginalGriff 18-Feb-14 7:28am    
What exact string did it give you?
Basically, when you failed to connect to your SQL Server, the issue could be:
1) Network issue,
2) SQL Server configuration issue.
3) Firewall issue,
4) Client driver issue,
5) Application configuration issue.
6) Authentication and logon issue.

Based on the error message you didn't allow the remote connection to that machine so change the options in settings.

Check these
Steps to troubleshoot SQL connectivity issues[^]
SQL Server 2005 Connectivity Issue Troubleshoot - Part I[^]
Troubleshoot Connectivity Issue in SQL Server 2005 - Part II[^]
Troubleshoot Connectivity Issue in SQL Server 2005 - Part III[^]

BTW check this bunch also for future errors
 
Share this answer
 
provided connection string is wrong

1. use machine name insted of (local)
 
Share this answer
 
Comments
Heyrbiar 18-Feb-14 6:03am    
how would i use the machine name can you please give me an example
Check your server is ON because your application doesn't get the server
 
Share this answer
 
v2
You just check your connectionstring.

SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=logins;Integrated Security=SSPI");

for Data Source you can use your machine ip or you can use '.' dot.
 
Share this answer
 
The below code has a problem

SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=logins;Integrated Security=SSPI");

Please provide the user name and password to set up the connection.
 
Share this answer
 
Comments
Heyrbiar 18-Feb-14 10:08am    
what would be my username because when i set up this sql server i only was having option of writing password not username
As stated, but not sure if confirmed yet, your connection string cannot connect to the database. So, either the connection string is wrong or your server is not running.

See http://www.connectionstrings.com/[^]
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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