0

Problem I am having is when I want to insert into multiple columns at the same time.

What I had was:
AccessDataSource1.InsertCommand = "INSERT INTO [CoursesTaken] (StudentID) VALUES ('" TextBox1.Text + "');"; AccessDataSource1.Insert();AccessDataSource1.DataBind();

That inserts into one column but when I attempt this: AccessDataSource1.InsertCommand = "INSERT INTO [CoursesTaken] (StudentID, CourseID) VALUES ('" + TextBox1.Text + "''" + TextBox2.Text + "');"; AccessDataSource1.Insert();AccessDataSource1.DataBind();

It gives me the error message "Number of query values and destination fields are not the same."

So then I attempted this:

   if (e.CommandName.CompareTo("Insert") == 0)
    {
        int newIndex = Convert.ToInt32(e.CommandArgument);

        AccessDataSource1.InsertCommand = "INSERT INTO [CoursesTaken] (StudentID, CourseID) VALUES ('" + TextBox1.Text + "''" + TextBox2.Text + "');";
        AccessDataSource1.InsertParameters.Add(TextBox1.Text, GridView1.Rows[newIndex].Cells[0].Text);
        AccessDataSource1.InsertParameters.Add(TextBox2.Text, GridView1.Rows[newIndex].Cells[1].Text);

        AccessDataSource1.Insert();
        AccessDataSource1.DataBind();
    }

And it is still giving me the error message. Would anyone mind explaining this to me?

1 Answer 1

2

You need to use a comma to separate the values like you do in your INSERT list

AccessDataSource1.InsertCommand = "
INSERT INTO [CoursesTaken] (StudentID, CourseID) 
VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "');";   
AccessDataSource1.Insert();AccessDataSource1.DataBind();
Sign up to request clarification or add additional context in comments.

2 Comments

if you used line breaks it may give you a hard time. If you didn't then I don't know what the issue could be
I don't know if it was a lag issue or what with Access but it is working now. Thank you!

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.