0

im trying to make simple registration form i want to insert data from textboxes into a DataSet Table here is my WebForm code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Configuration;
using System.Data.SqlClient;


namespace IknowyourbrainWebSite
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);


        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("insert into tbl values('"+TxtUserName.Text+"','"+TxtPassword.Text+"','"+TxtRePassword.Text+"')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            TxtUserName.Text = "";
            TxtPassword.Text = "";
            TxtRePassword.Text = "";


        }
    }
}

when i start it i got an error :

Server Error in '/' Application.
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) 
 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.Data.SqlClient.SqlException: 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 Error: 

Line 20:         protected void Page_Load(object sender, EventArgs e)
Line 21:         {
Line 22:             con.Open();
Line 23:         }
Line 24: 


 Source File:  c:\Users\Fluksikarton\Documents\Visual Studio 2012\Projects\IknowyourbrainWebSite\IknowyourbrainWebSite\WebForm1.aspx.cs    Line:  22 

Stack Trace: 

[SqlException (0x80131904): 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)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295167
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5307115
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +920
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +225
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +37
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions) +558
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions) +67
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1052
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +167
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +143
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +83
   System.Data.SqlClient.SqlConnection.Open() +96
   IknowyourbrainWebSite.WebForm1.Page_Load(Object sender, EventArgs e) in c:\Users\Fluksikarton\Documents\Visual Studio 2012\Projects\IknowyourbrainWebSite\IknowyourbrainWebSite\WebForm1.aspx.cs:22
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
enter code here

Is it possible that this error is caused by the fact that my public IP is not static i mean is dynamic , as far as i know it changes everytime i restart my router or modem or whatever or the problem is somewhere in my codes/options

Here is my connecton string code in web.config:

<configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=|DataDirectory|\RegistrationDataBase.sdf"
            providerName="System.Data.SqlServerCe.4.0" />
    </connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>


</configuration>
Boris
  • 1
  • 5

2 Answers2

0

This error is most often triggered if your connection string isn't set up correctly. Have you verified that it is correct, in that, is the server name and instance correct, are the credentials correct and is your server set up to receive remote connections (assuming it's not on the same box as your code)?

DiskJunky
  • 4,750
  • 3
  • 37
  • 66
  • i dont know what you are really asking me to do can you guide me sorry im new in this SQL connections – Boris Mar 13 '13 at 20:01
  • @Boris, can you post your connection string? You will find it in your App.config or Web.config file at the root of your project in a section called `` – DiskJunky Mar 13 '13 at 20:04
  • @Boris, ok, and does the "RegistrationDatabase.sdf" file exist in your application's App_Data folder? – DiskJunky Mar 13 '13 at 20:19
  • y in my App_Data folder i have RegistratonDataBase.sdf file – Boris Mar 13 '13 at 20:24
  • DiskJunky if you want im willing to show you with TeamViewer my project i really want to start my things working – Boris Mar 13 '13 at 20:27
  • @Boris, I think it'd be trickier to set up than trying to solve the issue at hand. Essentially the code cannot find the database. You need to make sure that every step along the way is working. E.g., does the database exist? Does it exist in the place the code is looking for it? Are the username/password correct? etc. – DiskJunky Mar 13 '13 at 20:48
  • I miss something really stupid im sure, please contact me in skype(if you have one)skype name fokijoro93 really im so sad because simple things does not work im simply following a tutorial in youtube – Boris Mar 13 '13 at 21:17
0

The rror is normally a bad connection string. Please check it.

Why don't you rewrite Button2_Click like this.

protected void Button2_Click(object sender, EventArgs e)
{
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into tbl values('"+TxtUserName.Text+"','" + TxtPassword.Text+"','"+TxtRePassword.Text+"')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        TxtUserName.Text = "";
        TxtPassword.Text = "";
        TxtRePassword.Text = "";

}

Some good starting places:

How to check if connection string is valid?

or

http://www.connectionstrings.com/

Read the actual error you are getting

[SqlException (0x80131904): 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)]

Community
  • 1
  • 1
Ian G
  • 29,468
  • 21
  • 78
  • 92