Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ds.Tables[0].Rows[inc].Delete();
                objConnect.UpdateDatabase(ds);
                this.ds.Reset();

                MaxRows = ds.Tables[0].Rows.Count;
                inc = inc-1;
                NavigateRecords(ds);
                MessageBox.Show("Records Deleted");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }



I dont understand what is wrong with the code. I am creating this data navigation and manipulation app. The error is on the first line "Cannot find table 0" IndexOutOfRange. Please if anyone can help!

ADDED:
COde from the comment:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace testNavData
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      DatabaseConnection objConnect;
      string conString;

      DataSet ds;
      DataRow dRow;

      int MaxRows;
      int inc = 0;

      private void Form1_Load(object sender, EventArgs e)
      {
         DataSet ds1=new DataSet();
         try 
         {
            objConnect = new DatabaseConnection();
            conString = Properties.Settings.Default.EmployeesConnectionString;

            objConnect.connection_string = conString;
            objConnect.Sql = Properties.Settings.Default.SQL;;
            //MessageBox.Show(get{return objConnect.Sql});

            ds = objConnect.GetConnection;

            MaxRows = ds.Tables[0].Rows.Count;

            NavigateRecords(ds);
         }
         catch (Exception err)
         {
            MessageBox.Show(err.Message);
         }
      }
      private void NavigateRecords(DataSet ds)
      {
         dRow = ds.Tables[0].Rows[inc];

         txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString();
         txtSurname.Text = dRow.ItemArray.GetValue(2).ToString();
         txtJobTitle.Text = dRow.ItemArray.GetValue(3).ToString();
         txtDepartment.Text = dRow.ItemArray.GetValue(4).ToString();
      }

      private void btnAddNew_Click(object sender, EventArgs e)
      {
         txtFirstName.Clear();
         txtSurname.Clear();
         txtJobTitle.Clear();
         txtDepartment.Clear();

         btnAddNew.Enabled = false;
         btnSave.Enabled = true;
         btnCancel.Enabled = true;
      }

private void btnCancel_Click(object sender, EventArgs e)
{
NavigateRecords(ds);
btnCancel.Enabled = false;
btnAddNew.Enabled = true;
btnSave.Enabled = false;
}

private void btnSave_Click(object sender, EventArgs e)
{
DataRow row = ds.Tables[0].NewRow();
row[1] = txtFirstName .Text;
row[2] = txtSurname.Text;
row[3] = txtJobTitle.Text;
row[4] = txtDepartment.Text;

ds.Tables[0].Rows.Add(row);
try
{
objConnect.UpdateDatabase(ds);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;

this.ds.Tables[0].Clear(); 


MessageBox.Show("Database is Updated");
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
btnCancel.Enabled = false;
btnSave.Enabled = false;
btnAddNew.Enabled = true;

}



public string Sql { get; set; }

private void btnNext_Click(object sender, EventArgs e)
{
if (inc != (MaxRows-1))
{
inc= inc+1;
NavigateRecords(ds);
}
else
{
if (inc != (MaxRows - 1))
{

inc = (MaxRows-1);
NavigateRecords(ds);

}

}
}

private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{

inc= inc-1;
NavigateRecords(ds);

}
else
{
if (inc != 0)
{

inc = 0;
NavigateRecords(ds);

}


}


}

private void btnLast_Click(object sender, EventArgs e)
{
if (inc != M
Posted
Updated 12-Jul-15 6:01am
v2

1 solution

Most likely you haven't created any tables in this dataset so the dataset exists but the tables collection is empty.

Make sure that you populate correct dataset or create the tables in your code (depending what is the logic of the application).

In order to help more, we should see the part of the code where you create and populate the dataset since the problem lies over there, not in the delete command :)
 
Share this answer
 
Comments
Member 11341510 12-Jul-15 11:17am    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace testNavData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

DatabaseConnection objConnect;
string conString;

DataSet ds;
DataRow dRow;

int MaxRows;
int inc = 0;



private void Form1_Load(object sender, EventArgs e)
{



DataSet ds1=new DataSet();
try
{
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.EmployeesConnectionString;

objConnect.connection_string = conString;
objConnect.Sql = Properties.Settings.Default.SQL;;
//MessageBox.Show(get{return objConnect.Sql});

ds = objConnect.GetConnection;


MaxRows = ds.Tables[0].Rows.Count;

NavigateRecords(ds);
}
catch (Exception err)
{

MessageBox.Show(err.Message);
}
}
private void NavigateRecords(DataSet ds)
{

dRow = ds.Tables[0].Rows[inc];


txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString();
txtSurname.Text = dRow.ItemArray.GetValue(2).ToString();
txtJobTitle.Text = dRow.ItemArray.GetValue(3).ToString();
txtDepartment.Text = dRow.ItemArray.GetValue(4).ToString();
}

private void btnAddNew_Click(object sender, EventArgs e)
{
txtFirstName.Clear();
txtSurname.Clear();
txtJobTitle.Clear();
txtDepartment.Clear();

btnAddNew.Enabled = false;
btnSave.Enabled = true;
btnCancel.Enabled = true;
}

private void btnCancel_Click(object sender, EventArgs e)
{
NavigateRecords(ds);
btnCancel.Enabled = false;
btnAddNew.Enabled = true;
btnSave.Enabled = false;
}

private void btnSave_Click(object sender, EventArgs e)
{
DataRow row = ds.Tables[0].NewRow();
row[1] = txtFirstName .Text;
row[2] = txtSurname.Text;
row[3] = txtJobTitle.Text;
row[4] = txtDepartment.Text;

ds.Tables[0].Rows.Add(row);
try
{
objConnect.UpdateDatabase(ds);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;

this.ds.Tables[0].Clear();


MessageBox.Show("Database is Updated");
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
btnCancel.Enabled = false;
btnSave.Enabled = false;
btnAddNew.Enabled = true;

}



public string Sql { get; set; }

private void btnNext_Click(object sender, EventArgs e)
{
if (inc != (MaxRows-1))
{
inc= inc+1;
NavigateRecords(ds);
}
else
{
if (inc != (MaxRows - 1))
{

inc = (MaxRows-1);
NavigateRecords(ds);

}

}
}

private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{

inc= inc-1;
NavigateRecords(ds);

}
else
{
if (inc != 0)
{

inc = 0;
NavigateRecords(ds);

}


}


}

private void btnLast_Click(object sender, EventArgs e)
{
if (inc != M
Member 11341510 12-Jul-15 11:51am    
kindly help plz plz
Wendelius 12-Jul-15 12:02pm    
From the code you sent, I can't find where do you fill the dataset ds

Also you define a dataset ds1 in form load. As far as I can see this is not used and should be removed in order to avoid using wrong dataset
Member 11341510 12-Jul-15 12:40pm    
yes you are right, I will remove the ds1 dataset:

I have created a class DatabaseConnection.cs

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testNavData
{
class DatabaseConnection
{
private string sql_String;
private string strCon;
System.Data.SqlClient.SqlDataAdapter da_1;

public string Sql
{
set { sql_String = value; }
}

public string connection_string
{
set { strCon = value; }

}
public System.Data.DataSet GetConnection
{
get { return MyDataSet(); }
}
private System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
con.Open();
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_String, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set, "Table_Data_1");
con.Close();
return dat_set;
}
public void UpdateDatabase(System.Data.DataSet ds)
{
System.Data.SqlClient.SqlCommandBuilder cb = new SqlCommandBuilder(da_1);
cb.DataAdapter.Update(ds.Tables[0]);
}
}

}
Wendelius 12-Jul-15 12:52pm    
The likeliest situation is that the ds in the form load is different then the ds in delete button's click event or you create a completely new dataset into the ds somewhere.

Having that said I'd suggest that you use the debugger to see what happens in each of the methods you call and all the time monitor the ds dataset and changes in it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900