0

So i created a database on Microsoft SQL server 2014 called WaitListData and created a table called Table, i added 5 columns. I want my C# program to insert data into these columns when a button is pressed.

My problem is (and i have researched this problem for hours) that no matter what i do, i always get the same error when i click my button, which is:System.Data.SqlClient.SqlException in System.Data.dll

Obviously my application is going to be a lot more than just a button but i cannot seem to just get this basic function to work properly and its very frustrating. I can connect to database using Visual Studio's Server explorer and do whatever i want from there but that isn't what i need.

So any help on this would be very much appreciated, if anyone needs additional information then i will happily try to provide it.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        SqlConnection myConnection = new SqlConnection("Server=PH-IT-CC1@SQLEXPRESS;Database=WaitListData;Trusted_Connection=True");
        myConnection.Open();

        SqlCommand myCommand = new SqlCommand("INSERT INTO Table (Firstname, Lastname, Email, Nationality, Time_Of_Play) " + "VALUES ('" + txtFirstname1.Text + "' , '" + txtLastname1.Text + "' , '" + txtEmail1.Text + "' , '" + txtNationality1.Text + "' , '" + txtTimeOfPlay1.Text + "')", myConnection);

        myCommand.ExecuteNonQuery();
        myConnection.Close();
    }
}
}
Aakash Goplani
  • 1,150
  • 1
  • 19
  • 36
  • It would help if we knew what the exception message was – Randy Minder Mar 28 '16 at 11:40
  • Can you connect to the database using SQL SMS on the same machine? How are you authenticating the user? – ChrisF Mar 28 '16 at 11:41
  • Here is the exception message: 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) – Campbell Carmichael Mar 28 '16 at 11:46
  • 1
    Yes, on SQL SMS, i can connect using PH-IT-CC1\SQLEXPRESS – Campbell Carmichael Mar 28 '16 at 11:48
  • try it with 2 backslashes : PH-IT-CC1\\SQLEXPRESS or pc-ip\\SQLEXPRESS – SnakeFoot Mar 28 '16 at 11:51
  • change `PH-IT-CC1@SQLEXPRESS` to `PH-IT-CC1\SQLEXPRESS` or `.\SQLEXPRESS`. Also check for TCP ports settings to be enabled. – Hemal Mar 28 '16 at 11:53

1 Answers1

1

Might found silly, but this happened with me once. 1. Close the IIS express locally and then run the app again. 2. If the DB is installed locally, change the server name to (local) or try IP address instead of hostname.

A3006
  • 1,051
  • 1
  • 11
  • 28