Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 2 methods that are used on the btnSave click event, at first I thought the connection was ok and something was wrong with sql, but after stepping through, I am getting an error when the connection is attempted to open (conn.Open();).

really appreciate any help.


Here is the error message:

[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. (p...


Here is my code:

private void AddClient()
        {
            

            string ConnectionString = "Data Source= ./SQLEXPRESS;Initial Catalog=iPhotographer;Integrated Security=SSPI";

            string sqlInsert = "INSERT INTO Clients(Client, Contact, Address, Telephone, NumberOfJobs, LastJob)VALUES(@Client, @Contact, @Address, @Telephone, @Jobcount, @Lastjob)";

            SqlConnection conn = new SqlConnection(ConnectionString);

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sqlInsert, conn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Client", this.Client);
                cmd.Parameters.AddWithValue("@Contact", this.Contact);
                cmd.Parameters.AddWithValue("@Address", this.Address);
                cmd.Parameters.AddWithValue("@Telephone", this.Telephone);
                cmd.Parameters.AddWithValue("@Jobcount", this.NumberOfJobs);
                cmd.Parameters.AddWithValue("@Lastjob", this.LastJob);
                cmd.ExecuteNonQuery();

            }
            catch (Exception error)
            {
                MessageBox.Show("Error: " + error);
            }

            finally
            {
                conn.Close();
            }
            MessageBox.Show("A new client has successfully been added to the client database.");

            pnlClients.Visible = false;
        }

        private void NewClientDetails()
        {
            this.Client = tbClient.Text;
            this.Contact = this.tbContact.Text;
            this.Address = this.tbAddress.Text;
            this.Telephone = int.Parse(this.tbTelephone.Text.ToString());
            this.NumberOfJobs = int.Parse(this.tbJobs.Text.ToString());
            this.LastJob = DateTime.Parse(this.dtpLastJob.Text.ToString());
        }


regards

D
Posted

Check the SQL server is functioning well or not.
Go to SQL Server Configuration Manager and check whether all the related services are running.
 
Share this answer
 
Comments
TeacherDean 23-Nov-11 3:20am    
Checked and Sqlexpress and sql client are both running
I have solved this myself, the solution is a @ symbol!

This:
string ConnectionString = "Data Source= ./SQLEXPRESS;Initial Catalog=iPhotographer;Integrated Security=SSPI";


Should be like this:
string ConnectionString = @"Data Source= ./SQLEXPRESS;Initial Catalog=iPhotographer;Integrated Security=SSPI";


Sigh lol

Thanks for any advice.

regards

D
 
Share this answer
 
Hi deanmichaelcook,

Did you try to debug it? On which line/part did the error appeared?

Regards,
Eduard



Try doing this:

try
{
// move it down
SqlCommand cmd = new SqlCommand(sqlInsert, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Client", this.Client);
cmd.Parameters.AddWithValue("@Contact", this.Contact);
cmd.Parameters.AddWithValue("@Address", this.Address);
cmd.Parameters.AddWithValue("@Telephone", this.Telephone);
cmd.Parameters.AddWithValue("@Jobcount", this.NumberOfJobs);
cmd.Parameters.AddWithValue("@Lastjob", this.LastJob);
conn.Open();
cmd.ExecuteNonQuery();
}

hope this works.

Regards.
 
Share this answer
 
v2
Comments
TeacherDean 22-Nov-11 21:04pm    
Not sure what moving conn.Open would achieve, but tried it anyway, and still get the same error message when it gets to Conn.Open
[no name] 23-Nov-11 0:59am    
Are you using a public or private database server?
TeacherDean 23-Nov-11 3:20am    
public database
[no name] 23-Nov-11 4:08am    
contact your network administrator, maybe firewall is blocking your connection to the server.
[no name] 23-Nov-11 4:09am    
or your database server has credentials?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900