Question

I'm having trouble to save details about a movie in my database. I know the name giving is not good, but I have just made 1 textbox for input right now, as I am getting desperate. Can anyone tell me what I have done wrong in my code. If it helps I having the following error which suggests that it can't connect to my localDB but I am not sure.

Error from code : enter image description here

Code:

        private  void btnRes_Click(object sender, EventArgs e)
    {

        SqlConnection sc = new SqlConnection();
        sc.ConnectionString = ("Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True");
        SqlCommand com = new SqlCommand();
        sc.Open();
        com.Connection = sc;
        com.CommandText = ("INSERT into movieTable (movieID, movieName, movieLength, movieDescription) VALUES  ('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"');");
        com.ExecuteNonQuery();
        sc.Close();
Was it helpful?

Solution

Your local database is not accessible over network. Try the following -

  1. Check whether server is on
  2. Check whether firewall is not blocking the Program
  3. Check whether Named Pipes and TCP/IP is enabled in SQL Configuration manager. To access SQL Configuration Manager, go to Start > SQL Server (Version) > SQL Configuration Manager. Then expand Client Configuration (if it's x64 then there will be 2 item , click that does not have 32 with it). From the right pane select Named Pipe, Vias and TCP/IP and enable all and restart server.
  4. Make sure you are using correct instance name. MQL Server Enterprise or Standard editions might not have instance names but also can be installed with instance name. See in you SQL configuration manager that you are using the proper instance name.

Then try again.

OTHER TIPS

  • First of All check the Authentication mode of the Sql Server whether it is in Windows Authentication Mode or SQL Server Authentication Mode
  • Secondly Check whether the service of the SQL Server is in running state or not

If the SQl Server installation mode is SQl Server Authentication Mode then the connection string used is : Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername; Password=myPassword;

If the SQL Server installation mode is Windows Authentication then the connection string is : Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top