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