Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Two records in my database.when i click on next button then one error message coming
"There is no row at position 1"
i am using below code .
anyone please help me where i am doing mistake in code.

C#
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;
using System.Data.SqlClient;

namespace MyProject
{

    public partial class Form1 : Form
    {
        int i = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

       private void Form1_Load(object sender, EventArgs e)
        {
           SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            conn.Open();
            SqlCommand command = new SqlCommand("select * from emp_detail", conn);
            SqlDataAdapter adp = new SqlDataAdapter(command);
            DataTable tbl = new DataTable();
            adp.Fill(tbl);
            display(tbl);
       }
           private void display(DataTable tbl)
           {
           txtempid.Text = tbl.Rows[i][0].ToString();
            txtname.Text = tbl.Rows[i][1].ToString();
            txtsurname.Text = tbl.Rows[i][2].ToString();
            txtfathername.Text = tbl.Rows[i][3].ToString();
            dtdob.Text = tbl.Rows[i][4].ToString();
            cbgender.Text = tbl.Rows[i][5].ToString();
            cbcity.Text = tbl.Rows[i][6].ToString();
            txtcontactno.Text = tbl.Rows[i][7].ToString();
            dtdoj.Text = tbl.Rows[i][8].ToString();
            txtdept.Text = tbl.Rows[i][9].ToString();
            txtdesig.Text = tbl.Rows[i][10].ToString();
            txtqualification.Text = tbl.Rows[i][11].ToString();
            rtaddress.Text = tbl.Rows[i][12].ToString();
            rtremarks.Text = tbl.Rows[i][13].ToString();
            
        }
           private void button8_Click(object sender, EventArgs e)
           {
               DataTable tbl = new DataTable();

               if (i > tbl.Rows.Count - 1)
               {
                   i++;

                   display(tbl);
               }
               else
               {
                   MessageBox.Show("this is last records");
               }
           }
Posted
Updated 19-Mar-14 2:26am
v4
Comments
CHill60 19-Mar-14 8:29am    
I presume button8 is your "Next button" ? You create a new DataTable tbl but don't populate it
ZurdoDev 19-Mar-14 8:31am    
I think you want < in your if statement in the button click. If I follow the way you have it now you are saying if the current record is greater than the number of rows then increment the row counter and display the table. I think you mean <

However, this is easily solved if you use a breakpoint and step through the code. You'll see exactly what is happening. No need for mysteries.

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;
using System.Data.SqlClient;
 
namespace MyProject
{
 
    public partial class Form1 : Form
    {
        int i = 0;
        DataTable tbl = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }
 
    private void Form1_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\HRM.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        conn.Open();
        SqlCommand command = new SqlCommand("select * from emp_detail", conn);
        SqlDataAdapter adp = new SqlDataAdapter(command);
        //DataTable tbl = new DataTable();
        adp.Fill(tbl);
        display(tbl);
    }
    private void display(DataTable tbl)
    {
        txtempid.Text = tbl.Rows[i][0].ToString();
        txtname.Text = tbl.Rows[i][1].ToString();
        txtsurname.Text = tbl.Rows[i][2].ToString();
        txtfathername.Text = tbl.Rows[i][3].ToString();
        dtdob.Text = tbl.Rows[i][4].ToString();
        cbgender.Text = tbl.Rows[i][5].ToString();
        cbcity.Text = tbl.Rows[i][6].ToString();
        txtcontactno.Text = tbl.Rows[i][7].ToString();
        dtdoj.Text = tbl.Rows[i][8].ToString();
        txtdept.Text = tbl.Rows[i][9].ToString();
        txtdesig.Text = tbl.Rows[i][10].ToString();
        txtqualification.Text = tbl.Rows[i][11].ToString();
        rtaddress.Text = tbl.Rows[i][12].ToString();
        rtremarks.Text = tbl.Rows[i][13].ToString();
    }
    private void button8_Click(object sender, EventArgs e)
    {
        //DataTable tbl = new DataTable();
 
        if (i <=tbl.Rows.Count)
        {
        i++;
 
        display(tbl);
    }
    else
    {
        MessageBox.Show("this is last records");
    }
}
 
Share this answer
 
v4
Comments
CHill60 19-Mar-14 8:39am    
I've formatted the code in your solution for you. Not sure why you've moved int i = 0; up to the form level scope.
manish7664 19-Mar-14 8:53am    
i am newly in c#. could you please tell me how can move records without using [i].
Thanks in advance
CHill60 19-Mar-14 9:09am    
Well you could use a SqlDataReader instead of the DataAdaptor and DataTable then you could the .Read method to advance through the set of records
manish7664 19-Mar-14 8:45am    
Mr Jawad first thanks for your reply. could you please explain wht's the differance if i declare "datatable tbl= new datatable()" inside of form block or declare inside of button_click block.
Jawad Ahmed Tanoli 19-Mar-14 8:49am    
if you declare datatable inside button event you will have access to that variable inside that event.if you define it globally you will have access to it in all other events.
Further to Jawad Ahmed Tanoli's solution...

The cause of your problem is Variable Scope[^]

The DataTable tbl is not the same as the tbl in your Button8_Click event!

What Jawad's code does is move the declaration of DataTable tbl to Form level scope, so the same instance (i.e. the data that is loaded at Form_Load) is available to all the functions within this Form class.
 
Share this answer
 
C#
private void button8_Click(object sender, EventArgs e)
          {
              DataTable tbl = new DataTable();
//this datatable tbl is not filled here with data
              if (i > tbl.Rows.Count - 1)
              {
                  i++;

                  display(tbl);
              }
              else
              {
                  MessageBox.Show("this is last records");
              }
          }
 
Share this answer
 
Comments
CHill60 19-Mar-14 8:41am    
Why have you posted this as another solution? It is already in your original post. You can use the "Improve Solution" link to update your answer if necessary.
See also my solution - I've explained *why* that datatable is not filled with data
Jawad Ahmed Tanoli 19-Mar-14 8:45am    
Chill60 i just want to post that piece of code.

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