4 Replies - 815 Views - Last Post: 13 February 2013 - 02:33 PM Rate Topic: -----

#1 Leader2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-February 13

Why still stucking for the Button "Last"?

Posted 13 February 2013 - 03:17 AM

Hi!

I have a form with two textboxs (txtBx_Name and txtBx_Datebirth) and fours button ("First, Previous, Next and Last").
But curiosly my problem it's on Button "Last". When I'm scrolling my record using all these buttons its works fine. Except the button "Last". When click this button there s message that says" There is no row at position -1.
Need explanation and help.
The cursor show me where I colored in red.

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

        System.Data.SqlServerCe.SqlCeConnection con;
        System.Data.SqlServerCe.SqlCeDataAdapter da;
        DataSet ds1;

        int MaxRows = 0;
        int inc = 0;


        private void Form1_Load(object sender, EventArgs e)
        {
            con = new System.Data.SqlServerCe.SqlCeConnection();
            //con.Open();

            ds1 = new DataSet();

            con.ConnectionString = "Data Source=C:\\employees.sdf";
            string sql = "SELECT * From tbl_employees";
            da = new System.Data.SqlServerCe.SqlCeDataAdapter(sql,con);

            da.Fill(ds1, "Israel");
            NavigateRecords();

            //MessageBox.Show("Connection Open");

            //con.Close();

        }

        private void NavigateRecords()
        {
            
            [color="#FF0000"]DataRow dRow = ds1.Tables["Israel"].Rows[inc];[/color]
            textBox1.Text = dRow.ItemArray.GetValue(1).ToString();
            textBox2.Text = dRow.ItemArray.GetValue(2).ToString();
            textBox3.Text = dRow.ItemArray.GetValue(3).ToString();
            textBox4.Text = dRow.ItemArray.GetValue(4).ToString();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc++;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("No more rows");
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (inc > 0)
            {
                inc--;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("First Record");
            }
        }

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

        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (inc != 0)
            {
                inc = 0;
                NavigateRecords();
            }
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Why still stucking for the Button "Last"?

#2 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: Why still stucking for the Button "Last"?

Posted 13 February 2013 - 03:54 AM

The problem is that you never change the value of MaxRow, this means it will stay 0 all the time. Which makes your error very easy to spot, since 0 minus 1 results in -1 :)

Assign MaxRows with the amount of rows retreived.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9204
  • View blog
  • Posts: 34,581
  • Joined: 12-June 08

Re: Why still stucking for the Button "Last"?

Posted 13 February 2013 - 08:25 AM

Moving to c#.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,830
  • Joined: 12-December 12

Re: Why still stucking for the Button "Last"?

Posted 13 February 2013 - 01:31 PM

I suppose ds1.Tables(0).Rows.Count would return the number of rows, not sure if this should be called before or after the DataAdapter is filled.
Was This Post Helpful? 0
  • +
  • -

#5 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Why still stucking for the Button "Last"?

Posted 13 February 2013 - 02:33 PM

It would have to be after the da.Fill call, that's what will place the data in the DataSet.

EDIT: Nevermind, I was looking at the wrong MSDN page.

This post has been edited by h4nnib4l: 13 February 2013 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1