Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8597909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:01:42+00:00 2026-06-12T01:01:42+00:00

Possible Duplicate: Error while adding records to access database from c#.net I need to

  • 0

Possible Duplicate:
Error while adding records to access database from c#.net

I need to add a new record to an Access database using the below code, but I get an IndexOutOfRangeException. I tried to solve it but I could not get a solution. How should I overcome this error?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        System.Data.OleDb.OleDbConnection con;
        DataSet ds1;
        System.Data.OleDb.OleDbDataAdapter da;
        int MaxRows = 0;
        int inc = 0;

        private void Form2_Load(object sender, EventArgs e)
        {
            con = new System.Data.OleDb.OleDbConnection();
            ds1 = new DataSet();
            con.ConnectionString ="Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:/Users/JaYam/Documents/jaya.accdb";
            string sql = "SELECT * From Table1"; 
            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
            con.Open();
            da.Fill(ds1, "Table1");
            NavigateRecords(); 
            con.Close();
            con.Dispose();
        }
        private void NavigateRecords()
        {
            DataRow drow =ds1.Tables["Table1"].Rows[0];
            textBox1.Text = drow.ItemArray.GetValue(0).ToString();
            textBox2.Text = drow.ItemArray.GetValue(1).ToString();
            textBox3.Text = drow.ItemArray.GetValue(2).ToString();
            textBox4.Text = drow.ItemArray.GetValue(3).ToString();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            con = new System.Data.OleDb.OleDbConnection();
            ds1 = new DataSet();
            con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/JaYam/Documents/jaya.accdb";
            string sql = "SELECT * From Table1";
            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
            con.Open();
            da.Fill(ds1, "Table1");
            NavigateRecords();


            System.Data.OleDb.OleDbCommandBuilder cb;
            cb = new System.Data.OleDb.OleDbCommandBuilder(da);
            DataRow dRow = ds1.Tables["Table1"].NewRow();
            //dRow[0] = textBox1.Text;
            dRow[1] = textBox2.Text;
            dRow[2] = textBox3.Text;
            dRow[3] = textBox4.Text;
            dRow[4] = textBox4.Text;
            ds1.Tables["Table1"].Rows.Add(dRow);
            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;
            da.Update(ds1, "Table1");
            MessageBox.Show("Entry Added");
            con.Close();
            con.Dispose();

        }

    }
}     
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:01:44+00:00Added an answer on June 12, 2026 at 1:01 am

    You’re trying to access a fifth, non-existent column by dRow[4]. The 4 means you’re trying to access the fifth element since indexes are zero-based in most programming languages. The first index is 0, the second is 1, the third is 2, et c.

    Consider this piece of code, where you fetch the values:

    textBox1.Text = drow.ItemArray.GetValue(0).ToString();
    textBox2.Text = drow.ItemArray.GetValue(1).ToString();
    textBox3.Text = drow.ItemArray.GetValue(2).ToString();
    textBox4.Text = drow.ItemArray.GetValue(3).ToString();
    

    Notice how you fetch value at place 0 into textBox1. You need to replicate this behaviour when you try to save your values, so assign dRow[0] the value of textBox1, dRow[1] the value of textBox2, et c.

    It looks like you’ve made a simple copying mistake, since you already do assign the value of textBox4 to dRow[3] (correctly), and on the next row you try to do the same with dRow[4] which is a column that simply does not exist.


    Per your update – it seems you’re trying to add a new row and assign a value to column 0 which is an autoincrement column. The database will handle this column itself, you don’t need to assign values to that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Error loading Active Record gem with sinatra app using RVM I have
Possible Duplicate: Why can I only access static members from a static function? While
Possible Duplicate: Parsing errors in awk blocks I am getting a parsing error while
Possible Duplicate: “Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given” error while
Possible Duplicate: Method group in VB.NET? While reading an answer I got this code:
Possible Duplicate: Why am I getting an access denied error for the Documents and
Possible Duplicate: Syntax error while defining an array as a property of a class
Possible Duplicate: How to make the + operator work while adding two Points to
Possible Duplicate: .NET Enumeration allows comma in the last field i noticed while i
Possible Duplicate: ClassnotFound exception using java reflection I get exception, java.lang.ClassNotFoundException: SavingAccount , while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.