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);
}
}
}
}
}




