0
votes

enter image description here

It's my first time and I checked it in the internet and I followed every step but I have a problem here in SqlConnection I think its says that it cannot find the SQL Server and don't know why.

I'm trying to make a simple Employees data (with a photo and date) and I want to save it to SQL Server. At first, I created a new SQL Server and put (localdb)\MSSQLocalDB as the Server Name, and EmployeeDatabase" as the new database name.

I got the Connection String in the properties of the database and this is what I got

"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=USER_TABLE;Integrated Security=True;Pooling=False"

I get this error when using this connection string:

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)'

Code:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace EmployeeDataSystem
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Insert_button_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=USER_TABLE;Integrated Security=True;Pooling=False");
            con.Open();

            SqlCommand cmd = new SqlCommand("INSERT INTO Personnel(Id,EmployeeName,EmployeeBirthdate,EmployeeSalary,EmployeeAddress,EmployeeMobile,EmployeeHiredate,EmployeePhoto) VALUES (@Id,@EmployeeName,@EmployeeBirthdate,@EmployeeSalary,@EmployeeAddress,@EmployeeMobile,@EmployeeHiredate,@EmployeePhoto)", con);
            cmd.Parameters.AddWithValue("@Id", int.Parse(Id_number.Text));
            cmd.Parameters.AddWithValue("@EmployeeName", int.Parse(Employee_name.Text));
            cmd.Parameters.AddWithValue("@EmployeeBirthdate", dateTimePicker1.Value);
            cmd.Parameters.AddWithValue("@EmployeeSalary", int.Parse(Salary_textbox.Text));
            cmd.Parameters.AddWithValue("@EmployeeAddress", int.Parse(Address_textbox.Text));
            cmd.Parameters.AddWithValue("@EmployeeMobile", int.Parse(Mobile_textbox.Text));
            cmd.Parameters.AddWithValue("@EmployeeHiredate", dateTimePicker2.Value);
            cmd.Parameters.AddWithValue("@EmployeePhoto", pictureBox1.Image);
            cmd.ExecuteNonQuery();
            con.Close();

            Id_number.Text = "";
            Employee_name.Text = "";
            //dateTimePicker1.Value = "";
            Salary_textbox.Text = "";
            Address_textbox.Text = "";
            Mobile_textbox.Text = "";
            //dateTimePicker2.Value = "";
            //pictureBox1.Image = "";

            MessageBox.Show("Successfullly Inserted!");
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            PictureBox p = sender as PictureBox;

            if(p != null)
            {
                open.Filter = "(*.jpg;*.jpeg;*.bmp;)|*.jpg; *.jpeg; *.bmp;";

                if(open.ShowDialog() == DialogResult.OK)
                {
                    p.Image = Image.FromFile(open.FileName);
                }
            }
        }
    }
}
5
Your photo is hardly readable. Please post the expection message as text. - Klaus Gütter
Thank you here: 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)' - Chuya Siy
You should edit your question and put this information directly in the question (instead of the photo). - Klaus Gütter
first check the instance of MSSQL server is correct or not and check the MSSQL setting if the Remote connection is allowed or not - Rohit Poudel
Thank you I have already checked it and the Remote Connection is already allowed - Chuya Siy

5 Answers

1
votes

Initial Catalog=EmployeeDatabase

If you are unsure of your instance name(Data Source) do the following:

  1. Open a command prompt window
  2. services.msc
  3. Locate the entries beginning with SQL. There will be an entry for each instance called SQL Server (instancename). The instance name appears within the parentheses.
1
votes

if use default instanst sql server use from dot(.) As datasource else use instansname that you can find through server name in sql server

SqlConnection con = new SqlConnection("Data Source=your instance name;Initial Catalog=databasename;User Id=your user;Password=your password;Integrated Security=True;Pooling=False");

find your instance name enter image description here and select a login name for remote to sql server enter image description here and click on your user and do setting user enter image description here enter image description here

1
votes

in your picture the connection string is

 "Data Source=.;Initial Catalog=USER_TABLE;Integrated Security=True;Pooling=False"

change it to

"Data Source=.\MSSQLLocalDB;Initial Catalog=USER_TABLE;Integrated Security=True;Pooling=False"
0
votes

try to use . (dot) as the instance name

0
votes

There are many causes of this error message which are given below

Antivirus, Firewall SQL Server Network configuration Please check if you have the port in the connection string "Data Source=x.x.x.x,1433" SQL Server Express defaults to a SQLEXPRESS instance name try to use . or

So try to use .\SQLEXPRESS or (local)\SQLEXPRESS as your server+instance name.

And if that doesn't work: what server name do you use in Management Studio to connect to your SQL Server instance?? Use that name!