Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my code to navigate records, but am understanding where I am making mistake or its totally wrong code please guide me...

My getting such error: Object reference not set to an instance of an object.


public partial class _Default : System.Web.UI.Page
    {
        static int n = 0;
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
             
                string StrCon = ConfigurationManager.ConnectionStrings["abc"].ToString();
                SqlConnection con;
                con = new SqlConnection(StrCon);
                string query = "select Name from table1 where Name like '" + TextBox2.Text + "%'";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                DataSet ds = new DataSet();
                da.Fill(ds, "table1");
                dt = ds.Tables["table1"];
                Navigate();
            }  
        }
        private void Navigate()
        {
            try
            {
                DataRow dRow = dt.Rows[n];
                TextBox1.Text = dRow[0].ToString();
            }
            catch
            {
                return;
            }
        }

        protected void NextButton1_Click(object sender, EventArgs e)
        {
            n = n + 1;
            if (n > dt.Rows.Count - 1)
            {
                message("No more Records");
                return;
            }
            else
            {
                Navigate();
            }
        }

        protected void PrevButton2_Click(object sender, EventArgs e)
        {
            n = n - 1;
            if (n == -1)
            {
                message("No more Records");
                return;
            }
            Navigate();
        }

        protected void LastButton3_Click(object sender, EventArgs e)
        {
            n = dt.Rows.Count - 1;
            Navigate();
        }

        protected void FirstButton4_Click(object sender, EventArgs e)
        {
            n = 0;
            Navigate();
        }
Posted
Updated 31-Mar-12 16:53pm
v2
Comments
[no name] 31-Mar-12 22:53pm    
added pre tags

Rewriting your Navigate function

C#
private void Navigate()
        {
            try
            {
                DataRow dRow = dt.Rows[n];
                if(dt.Rows[n]!=null)
                    TextBox1.Text = dRow[n].ToString();
            }
            catch
            {
                return;
            }
        }
 
Share this answer
 
Comments
Gopal Rakhal 1-Apr-12 3:13am    
thanks, but it is not working, please give perfect way to do so.
Hi ,
Try this .
C#
static int n = 0;
   DataTable dt ;
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
           dt = Bind();
           ViewState.Add("dtBind", dt);
           Navigate();
       }
   }
   DataTable Bind()
   {
       SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
       // con = new SqlConnection(StrCon);
       string query = "select Item_name from Items where Item_name like '" + TextBox2.Text + "%'";
       SqlDataAdapter da = new SqlDataAdapter(query, con);
       DataTable dt = new DataTable();
       da.Fill(dt);
       return dt;
   }
   private void Navigate()
   {
       try
       {
           DataRow dRow = dt.Rows[n];
           TextBox2.Text = dRow[0].ToString();
       }
       catch
       {
           return;
       }
   }

   protected void NextButton1_Click(object sender, EventArgs e)
   {
       dt = (DataTable)ViewState["dtBind"];
       n = n + 1;
       if (n > dt.Rows.Count - 1)
       {
           //message("No more Records");
           return;
       }
       else
       {
           Navigate();
       }
   }

   protected void PrevButton2_Click(object sender, EventArgs e)
   {
       dt = (DataTable)ViewState["dtBind"];

       n = n - 1;
       if (n == -1)
       {
        //   message("No more Records");
           return;
       }
       Navigate();
   }

   protected void LastButton3_Click(object sender, EventArgs e)
   {
       dt = (DataTable)ViewState["dtBind"];
       n = dt.Rows.Count - 1;
       Navigate();
   }

   protected void FirstButton4_Click(object sender, EventArgs e)
   {
       dt = (DataTable)ViewState["dtBind"];
       n = 0;
       Navigate();
   }

Best Regards
M.Mitwalli
 
Share this answer
 
Comments
Gopal Rakhal 1-Apr-12 3:12am    
its navigating but not displaying result like names.., just navigating in sequence
Mohamed Mitwalli 1-Apr-12 5:13am    
for what i did and tested it navigate and show the data right try to debug your code and make sure in your SQL statement make sure you are typing right

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