1

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

3
  • Mind me asking why you are using Microsoft Access? I haven't seen someone use an access database for data storage on an app in a decade, it's generally not considered a good idea to do. Unless it's for work and a work requirement you have no control of. Better choices include SQLite, Sql Server Express, Sql Server Compact Edition, PostGreSQL, MySql, or even nosql stuff like MongoDB. Or maybe it's for a college class, and I really hope college classes aren't still teaching Microsoft Access databases in C# classes. Commented Dec 15, 2015 at 3:57
  • This code is crazy-vulnerable to sql injection. It's practically begging to get hacked. Commented Dec 15, 2015 at 4:14
  • this is only for a college project but I appreciate the advice Commented Dec 15, 2015 at 4:17

2 Answers 2

1

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

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

1 Comment

well spotted but that didn't fix the problem
0

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;"

3 Comments

It's a sample connection string. On my real file I've got the proper file path.
It's probabl UAC. It's an access database file, if it requires elevated permissions then your process doing this would need to run as administrator to open it. Grant the process/users running your app access to the file. Run visual studio as administrator, then open your project and see if it works to test that theory.
Thanks for the suggestion but it did not work. This is frustrating.

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.