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?