0

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
  • 1
    Your photo is hardly readable. Please post the expection message as text. Commented Oct 18, 2020 at 6:14
  • 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)' Commented Oct 18, 2020 at 6:18
  • 3
    You should edit your question and put this information directly in the question (instead of the photo). Commented Oct 18, 2020 at 6:19
  • first check the instance of MSSQL server is correct or not and check the MSSQL setting if the Remote connection is allowed or not Commented Oct 18, 2020 at 7:00
  • Thank you I have already checked it and the Remote Connection is already allowed Commented Oct 18, 2020 at 7:15

4 Answers 4

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

And check if Sql Server service is Running.
It is running but there is only SQL Server VVS Writer
1

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

6 Comments

I already tried it and it didn't work. Right now I'm downloading SQL Server Management Studio, is it necessary?
What is your default instans name? Are you add login in sql server?are you sql server mixed mod enable?
I think it is /MSSQLLocalDB ,yes sql server mixed mod enable
i updated my answer please do this answer if you have problem say me
if you have problem upload photo from sql server engin name
|
1

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"

5 Comments

The Backslash Before MSSQLLocalDB has red underline/error that says 'Unrecognized escape sequence' Severity Code Description Project File Line Suppression State Error CS1009 Unrecognized escape sequence EmployeeDataSystem C:\Users\siych\OneDrive\Desktop\Employee Data System\EmployeeDataSystem\EmployeeDataSystem\Form1.cs 62 Active
use \\ instead of \ and again try a chance
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)'
can you take a picture from SQL server that you connected it
Hello I already got it thanks for the help!! here: SqlConnection con = new SqlConnection("Data Source =(localdb)\\MSSQLLocalDB; Initial Catalog = USER_TABLE; Integrated Security = True; Pooling = False");
0

try to use . (dot) as the instance name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.