Click here to Skip to main content
15,176,948 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In i have connected SQL 2008DB with asp.net 2010 web application using c# and I can not make next and previous buttons work,
and i want to show selected row number in label1 ,
only last and first buttons are working - Help me please
and i write these codes in defualt.aspx.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        int i = 0;
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=HARB\SQLEXPRESS;Initial Catalog=daily_log_golf;Integrated Security=True");
            conn.Open();
            SqlDataAdapter da =new SqlDataAdapter  ("select * from school", conn);
            da.Fill(ds, "school");
            dt = ds.Tables["school"];         
        }
        public void Display()
        {
            DataRow dr = dt.Rows[i];
            TextBox1.Text = dr["id"].ToString();
            TextBox2.Text = dr["name"].ToString();
            TextBox3.Text = dr["email"].ToString();
            TextBox4.Text = dr["town"].ToString();
        }
        protected void firstbtn_Click(object sender, EventArgs e)
        {
            {
                i = 0;
                Display();
            }
        }

        protected void nextbtn_Click(object sender, EventArgs e)
        {
            if (i < ds.Tables[0].Rows.Count - 1)
            {
                i++;
                Display();
            }
            else
            {
            }
        }

        protected void previousbtn_Click(object sender, EventArgs e)
        {
            i++;
            if (i >= 0)
            {
               Display();
            }
        }

        protected void lastbtn_Click(object sender , EventArgs e)
        {
            i = dt.Rows.Count - 1;
            Display();
        }

        protected void showrowbtn_Click(object sender, EventArgs e)
        {
            //to show selected row number in label 1
            DataRow row = dt.NewRow();
            dt.Rows.Add(row);
            // get the index now
            Label1.Text = (dt.Rows.IndexOf(row)).ToString();
        }
    }
}
Posted
Updated 17-Sep-15 3:56am
v2
Comments
CHill60 17-Sep-15 9:11am
   
For starters shouldn't previousbtn_Click be decrementing the index? i-- not i++
vagelis1 17-Sep-15 9:25am
   
In previousbtn_Click i++ should be i--. Give us more details. When you click next or previous, does your code raise an exception? and if so, what does the exception message say?

1 solution

Your code is working for First, Last and Next buttons.

Change the code in the Previous button to be
C#
private void previousbtn_Click(object sender, EventArgs e)
{
    if (i > 0)
    {
        i--;
        Display();
    }
}

I.e. decrement the index don't keep adding to it, and check that you want to change it before changing it! (otherwise it just keeps getting further and further out of the range)

The code in your showrowbtn_Click event is doing nothing wrong, although I can't really see the point of it. I suspect that you wanted to enter a row in a textbox perhaps and show that row?

If so then just validate that the number entered is between 0 and the number of rows - 1, set i and Display()
   

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