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();
}
}
}
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:
Notice how you fetch value at place 0 into
textBox1. You need to replicate this behaviour when you try to save your values, so assigndRow[0]the value oftextBox1,dRow[1]the value oftextBox2, et c.It looks like you’ve made a simple copying mistake, since you already do assign the value of
textBox4todRow[3](correctly), and on the next row you try to do the same withdRow[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.