Reputation: 221
I want to establish the database connection for receiving the data from the Textfields and store that data in database records. For that, up to now I have tried:
I have created the .mdf
database files and in that I have created the table with name as Table1
and I have placed the two textfields and submit button, with the following code :
data.aspx
<b>Username:<asp:TextBox ID="TextBox1" runat="server" BackColor="AliceBlue">
</asp:TextBox><br/>
<b>Lastname:<asp:TextBox ID="TextBox2" runat="server" BackColor="AliceBlue">
</asp:TextBox><br/>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="SubmitBtn_Click"/>
and the code file is as follows: data.aspx.cs
using System.Web.Configuration;
using System.Data.SqlClient;
protected void SubmitBtn_Click(object sender, EventArgs e)
{
string connectionStrings = "Data Source=|SQLEXPRESS;Integrated
Security=True; Connect Timeout=30;User Instance=True;";
using (SqlConnection sqlConnection = new SqlConnection(connectionStrings))
{
string insertStatement = "INSERT INTO Table1(column1,column2)
VALUES (@col1, @col2)";
SqlCommand sqlCommand = new SqlCommand(insertStatement, sqlConnection);
sqlCommand.Parameters.AddWithValue("@col1", TextBox1.Text);
sqlCommand.Parameters.AddWithValue("@col2", TextBox2.Text);
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
try
{
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
}
finally
{
sqlConnection.Close();
}
}
}
and I have also the configuration files as follows to establish connection the code for that is as follows: web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
but for that code I am getting this error after clicking the submit 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)
and also the exception:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Can any one help me?
Upvotes: 1
Views: 2116
Reputation: 8227
The exception is thrown because your SQL Server instance is not actually reachable, or your SQL Server instance is not started, or there's a firewall blocking the request for the default 1433 port, to reach remotely the SQL Server instance.
Read the documentation page: http://msdn.microsoft.com/en-us/library/ms177440.aspx
Remember to check your connection string. The connection string you wrote:
"Data Source=|SQLEXPRESS;Integrated
Security=True; Connect Timeout=30;User Instance=True;"
I think it's not correct. If you're connecting to your local 2nd SQL instance, named SQLEXPRESS, this is the correct connection string:
"Data Source=.\SQLEXPRESS;Integrated
Security=True; Connect Timeout=30;User Instance=True;"
Furthermore, you're using the setting Integrated Security=True;
. This means that you're trying to connect using your Windows credentials. Are you sure you have the rights to connect with your Windows credentials?
If not, remove this setting and add your username and password, like in this connection string:
"Data Source=.\SQLEXPRESS;User Id=sa; Password=sa123; Connect Timeout=30;"
Read this interesting question about the use of Integrated Security
setting.
Also, your SQLEXPRESS instance seems to be a user instance. Is this instance enabled?
Upvotes: 0
Reputation: 4314
Check if you sql server instance is running. Goto Start->SQL Server Configuration Manager
And check if your sql server instance has been started.
Or try changing your connection string to
string connectionStrings = "Data Source=.\SQLEXPRESS;
Initial Catalog=databaseName Integrated Security=True;";
Upvotes: 3