title: Unable to access SQL Server database in asp.net
body: <p>I am trying to access an online SQL Server database using asp.net</p>
  
  <p>I have used following lines in the code,</p>
  
  <pre><code>string connectionString = "Data Source=xx.xx.20x.82;Initial Catalog=mydbname;User ID=myusername;Password=mypass";
  
  string insertSql = "INSERT INTO Users (FirstName, LastName, UserName, Password, Email, Address, Gender, Phone, Pincode) VALUES (@FirstName, @LastName, @UserName, @Password, @Email, @Address, @Gender, @Phone, @Pincode)";
  //Create SQL connection
  SqlConnection con = new SqlConnection(connectionString);
  
  //Create SQL Command And Sql Parameters
  SqlCommand cmd = new SqlCommand(insertSql);
  cmd.Connection = con;
  cmd.CommandType = CommandType.Text;
  cmd.CommandText = insertSql; 
  
  SqlParameter firstName = new SqlParameter("@FirstName", SqlDbType.VarChar,50);
  firstName.Value = txtFirstName.Text.ToString();
  cmd.Parameters.Add(firstName);
  
  SqlParameter lastName = new SqlParameter("@LastName", SqlDbType.VarChar,50);
  lastName.Value = txtLastName.Text.ToString();
  cmd.Parameters.Add(lastName);
  
  SqlParameter Phone = new SqlParameter("@Phone", SqlDbType.VarChar, 12);
  Phone.Value = txtPhone.Text.ToString();
  cmd.Parameters.Add(Phone);
  
  SqlParameter Pincode = new SqlParameter("@Pincode", SqlDbType.VarChar, 10);
  Pincode.Value = txtPin.Text.ToString();
  cmd.Parameters.Add(Pincode);
  
  SqlParameter userName = new SqlParameter("@UserName", SqlDbType.VarChar, 10);
  userName.Value = txtUserName.Text.ToString();
  cmd.Parameters.Add(userName);
  
  SqlParameter pwd = new SqlParameter("@Password", SqlDbType.VarChar, 10);
  pwd.Value = txtPwd.Text.ToString();
  cmd.Parameters.Add(pwd);
  
  SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
  email.Value = txtEmailID.Text.ToString();
  cmd.Parameters.Add(email);
  
  SqlParameter address = new SqlParameter("@Address", SqlDbType.VarChar, 100);
  address.Value = txtAdress.Text.ToString();
  cmd.Parameters.Add(address);
  
  SqlParameter gender = new SqlParameter("@Gender", SqlDbType.VarChar,10);
  gender.Value = rdoGender.SelectedItem.ToString();
  cmd.Parameters.Add(gender);
  
  try
  {
          con.Open();
          cmd.ExecuteNonQuery();
          lblMsg.Text = "User Registration successful";
          ClearControls(Page);
  }
  catch (SqlException ex)
  {
          string errorMessage = "Error in registring user";
          errorMessage += ex.Message;
          throw new Exception(errorMessage);
  }
  </code></pre>
  
  <p>and my <code>web.config</code> file looke like this:</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="ConnectionString" 
           connectionString=" Server=whsql-v04.prod.mesa1.secureserver.net; Database=mydbname; User ID=myusername; Password=mypass; Trusted_Connection=False" 
           providerName="System.Data.SqlClient" /&gt;
      &lt;remove name="LocalSqlServer"/&gt;
      &lt;add name="LocalSqlServer" 
           connectionString=" Server=whsql-v04.prod.mesa1.secureserver.net; Database=mydbname; User ID=myusername; Password=mypass; Trusted_Connection=False" 
           providerName="System.Data.SqlClient" /&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>When I run the code am getting the error...</p>
  
  <p>What mistake have I made?</p>
  
  <p>Please help me...</p>
  
  <p>Error message...</p>
  
  <pre><code>Server Error in '/UserRegistration' Application.
  
  Error in registring userA 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)
  
  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.Exception: Error in registring userA 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)
  
  Source Error: 
  
  
  Line 80:             string errorMessage = "Error in registring user";
  Line 81:             errorMessage += ex.Message;
  Line 82:             throw new Exception(errorMessage);
  Line 83: 
  Line 84:         }
  
  Source File: e:\UserRegistration\Default.aspx.cs    Line: 82 
  
  Stack Trace: 
  
  
  [Exception: Error in registring userA 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)]
  _Default.btnSave_Click(Object sender, EventArgs e) in e:\UserRegistration\Default.aspx.cs:82
  System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
  System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
  System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
  eventArgument) +10
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  +13
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean 
  includeStagesAfterAsyncPoint) +1565
  
  Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET 
  Version:2.0.50727.4927
  </code></pre>