I was trying to create registration form but when I run my project am getting this error message
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
My code:
using System;
using System.Data;
using System.Data.SqlClient;
namespace Registration{
private void Register_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SHAB
SQLEXPRESS;InitialCatalog=Phonebook;Integrated Security=True");
SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[registration]([username[fullname],[Password]) VALUES('" + username.Text+"', '"+fullname.Text+"', '"+password.Text+"')");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Congradulation You have been Registered");
}
The error appears on the line con.open();
.
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Additional information:
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
Your connection string must be in app.config file
import configuration from
Add DB Connection
You are missing a comma between
[username]
and[fullname]
. Try like:Also it is very important to use sql Parameters
I think that you just have omitted the username and the password in your connection string. Try the following code.
First Check SQL Server Service is running. after that check Data Source value in your Connection String, if you are using SQLExpress then it may be
SqlConnection con = new SqlConnection("Data Source=SHAB\SQLEXPRESS;InitialCatalog=Phonebook;Integrated Security=True");
As the error indicates, your connection unable to reach SQL Server, make sure you are able to do
telnet SHAB 1433
If
Telnet
fails:If the custom port NOT configured
1433
and1434 (TCP and UDP)
- This must be done at the server where SQL Service is runningDataSource=SHAB\\SQLEXPRESS
. You can dotelnet SHAB 1433
to verifyIf the custom port configured
DataSource = SHAB,<custom port>
. However, you can dotelnet SHAB <custom port>
to verifySQL Server Configuration manager: