Reggie
Reggie

Reputation: 13

My access database does not update (ASP.NET)

I think I have trouble connecting to my database because it does not update when it's supposed to. I'm not sure as to why this is. Here's my .aspx.cs file:

public partial class addClass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private static OleDbConnection GetConnection()
    {
        String connection;
        connection = @"connectionstring";

        return new OleDbConnection(connection);

    }
    string one = "one";
    protected void submitButton_Click(object sender, EventArgs e)
    {
        OleDbConnection myConnection = GetConnection();


        try
        {

            myConnection.Open();
            String myQuery = "INSERT INTO Yoga Class([ClassName], [Level], [Duration], [Capacity], [Description]) values ('" +
                classNameTextBox.Text + "','" + levelDropDownList.SelectedItem.Text + "','" + durationDropDownList.SelectedItem.Text + "','" + capacityDropDownList.SelectedItem.Text +
                "','" + descriptionTextBox.Text + "');";

            String myQuery2 = "INSERT INTO YogaTeacher([TeacherID]) values('" +one+ "');";


            OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection); //get rid of parameters 



            myCommand.ExecuteNonQuery();

            myConnection.Close();
        }

        catch
        {
            Response.Write("Form did not submit due to an error");
        }

        finally
        {
            myConnection.Close();
        }
    }



   }
}

I think the reason it is not updating is because the column teacherID in the table 'Yoga Class' is a foreign key from another table. The error message in the 'catch' statement displays when I click the submit button. Am I correct? And if am how do I change my code to make it work?

Any help appreciated

Thank You

Upvotes: 1

Views: 326

Answers (2)

Ky  Wu
Ky Wu

Reputation: 86

Your code: String myQuery = "INSERT INTO Yoga Class... The tablename "Yoga Class" shouldn't have a space in it.

Upvotes: 1

Ryan Mann
Ryan Mann

Reputation: 5357

Your connection string is invalid. You have it set to the string "connectionstring" and it should be a full formated proper Connection String: such as

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Persist Security Info=False;"

Upvotes: 0

Related Questions