1

I am not sure if this problem has anything to do with my code or simply the way that my database is set up. Any pointers would be awesome!

This is the error message that I get:

enter image description here

I have gone to "Modify Connection" and used the 'Test Connection' tool and that says that it connects fine but when the actual program runs nothing happens and I get the error.

Here is my code:

    private void btnAddCustomer_Click(object sender, EventArgs e)
    {
    SqlConnection CustomerInfo = new SqlConnection("Data Source=C:\\Users\\Cory\\Desktop\\DeluxWrapsWindows\\DeluxWrapsWindows\\DeluxWraps_DB.mdb");
    { 
    SqlCommand xp = new SqlCommand("Insert into CustomerInfo(LastName, FirstName, Email, PhoneNumber, Address, Instagram, CarMake, CarModel, AdditionalNotes) Values(@LastName, @Firstname, @Email, @PhoneNumber, @Address, @Instagram, @CarMake, @CarModel, @AdditionalNotes)", CustomerInfo);

     xp.Parameters.AddWithValue("@LastName", txtLastName.Text);
     xp.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
     xp.Parameters.AddWithValue("@Email", txtEmail.Text);
     xp.Parameters.AddWithValue("@PhoneNumber", txtPhoneNumber.Text);
     xp.Parameters.AddWithValue("@Address", txtAddress.Text);
     xp.Parameters.AddWithValue("@Instagram", txtInstagram.Text);
     xp.Parameters.AddWithValue("@Carmake", txtCarMake.Text);
     xp.Parameters.AddWithValue("@CarModel", txtCarModel.Text);
     xp.Parameters.AddWithValue("@AdditionalNotes", txtAdditionalNotes.Text);

     CustomerInfo.Open();
     xp.ExecuteNonQuery();
     CustomerInfo.Close();

    }
}
1

3 Answers 3

3

You should try create the SqlCommand with:

SqlCommand xp = CustomerInfo.CreateCommand();

See this example:

https://msdn.microsoft.com/pt-br/library/system.data.sqlclient.sqlconnection.createcommand(v=vs.110).aspx

Update:

Try use OleDbConnection. See:

public DataSet GetDataSetFromAdapter(
    DataSet dataSet, string connectionString, string queryString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbDataAdapter adapter =
            new OleDbDataAdapter(queryString, connection);

        // Set the parameters.
        adapter.SelectCommand.Parameters.Add(
            "@CategoryName", OleDbType.VarChar, 80).Value = "toasters";
        adapter.SelectCommand.Parameters.Add(
            "@SerialNum", OleDbType.Integer).Value = 239;

        // Open the connection and fill the DataSet. 
        try
        {
            connection.Open();
            adapter.Fill(dataSet);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the 
        // code exits the using block.
    }
    return dataSet;
}

See more details in: https://msdn.microsoft.com/en-us/library/System.Data.OleDb.OleDbParameterCollection(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the advice Renatto Machado! I am still getting the same error though. For some reason it will not connect to the database.
0

It sounds like your trying to use a local database or auto-named database: Here is the syntax for your connection string:

Data Source=(LocalDB)\v11.0;
AttachDbFilename=Your local location here;
Integrated Security=True

Hope that helps.

4 Comments

Also, you will need to specify the local db verson of sql server. Mine is version v11.0.
I am using MS Access 2010. This is the first time that I have tried to connect to a database like this so thank you for your help. The Database is on my local machine.
In that case your connection string with need to use a different driver because you will be using Microsoft.Jet.OLEDB.4.0. Here is a connection string example: connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Northwind.mdb;" msdn.microsoft.com/en-us/library/hktw939c%28v=vs.85%29.aspx. Just make sure that your .mdb file goes in your data directory and replace the name with your own .mdb file name.
Thanks Eric! I will try this as soon as possible.
0

The problem is you're using System.Data.Sql as the provider, when it should all be coming from System.Data.OleDb;

However, you can use <asp:SqlDataSource> in ASPX, to connect to an Access Database, but that is not the same thing as System.Data.Sql.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.