Search code examples
c#database-connectionconnection-stringsqlconnection

C# Windows Forms: SqlConnection keyword not supported, connection timeout


Just learning how to create an Windows Forms application via Tom Owsiak C# Windows Forms video tutorials and I'm stuck at the database project (contacts management system) which requires to store data to a database.

I've been following his every single step yet somehow manage to mess up the application writing process. The error happen at the line

SqlConnection conn = new SqlConnection(connString);

Have been searching stackExchange for a while now and try possible solution but still couldn't work it out.

// error occurs here, stated key word not supported, connection timeout
using (SqlConnection connectforfucksake = new SqlConnection(connString)) 
{
    try
    {
        connectforfucksake.Open(); // open the connection

        // create the new SqlCommand object
        command = new SqlCommand(insert, connectforfucksake); 

        command.Parameters.AddWithValue(@"Data_Added", dateTimePicker1.Value.Date);
        command.Parameters.AddWithValue(@"Company", txtCompany.Text);
        command.Parameters.AddWithValue(@"Website", txtWebsite.Text);
        command.Parameters.AddWithValue(@"Title", txtTitle.Text);
        command.Parameters.AddWithValue(@"First_Name", txtFName.Text);
        command.Parameters.AddWithValue(@"Last_Name", txtLName.Text);
        command.Parameters.AddWithValue(@"Address", txtAddress.Text);
        command.Parameters.AddWithValue(@"City", txtCity.Text);
        command.Parameters.AddWithValue(@"State", txtState.Text);
        command.Parameters.AddWithValue(@"Postal_Code", txtPostalCode.Text);
        command.Parameters.AddWithValue(@"Mobile", txtMobile.Text);
        command.Parameters.AddWithValue(@"Note", txtNote.Text);

        command.ExecuteNonQuery();   // pushing whatever in the form into table
    }
    catch (Exception ex)
    {
       MessageBox.Show(ex.Message); // show the unforeseen error
    }
}

Expected application to take result and then store them into database but it seem like the SqlConnection object instantiate is causing the error.


Solution

  • It sounds like your connection string is simply wrong; most likely, you meant "Connect Timeout" rather than "connection timeout". A basic connection string that includes a connect timeout might be something like:

    Data Source=.;Initial Catalog=master;Integrated Security=True;Connect Timeout=42