Wrox Programmer Forums
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 4th, 2004, 05:07 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Datagrid error

Hi,
I am very thankful to this forum.
I need one more help from ur forum,I am working on search functionality where i have provided four textboxes to enter the search text and one datagrid.I have added paging and doing sorting too.But my problem is when i dont enter any text I am getting all the records,if I go to last page and there if enter some text in the text box, its searching on the same page not in the complete record and displaying error message:

"Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount."

Can anyone help me how to handle this error.

Thanks and regards
Lily


 
Old December 4th, 2004, 06:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can you post the relevant parts of your code? Otherwise it's hard to see what's going on.

You're probably not resetting the page's CurrentPageIndex when you are assigning a new search result to the grid, but that's a wild guess.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Wicked by KoRn (Track 10 from the album: Life Is Peachy) What's This?
 
Old December 4th, 2004, 06:40 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is my code:


nhrd_searchprofile.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace nhrdadmin
{
    /// <summary>
    /// Summary description for searchprofile.
    /// </summary>
    public class nhrd_searchprofile : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox firstname;
        protected System.Web.UI.WebControls.TextBox lastname;
        protected System.Web.UI.WebControls.TextBox membershipno;
        protected System.Web.UI.WebControls.TextBox emailid;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.HtmlControls.HtmlGenericControl outError;
        protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
        protected System.Web.UI.WebControls.Label Label9;
        protected System.Web.UI.WebControls.Label Label10;
        protected System.Web.UI.HtmlControls.HtmlGenericControl count1;
        protected System.Web.UI.HtmlControls.HtmlGenericControl l1;
        protected System.Web.UI.HtmlControls.HtmlGenericControl l2;
        protected System.Web.UI.HtmlControls.HtmlGenericControl l3;
        protected System.Web.UI.HtmlControls.HtmlGenericControl l4;
        protected System.Web.UI.HtmlControls.HtmlGenericControl l5;
        protected System.Web.UI.WebControls.Label Label12;
        string strConString= System.Configuration.ConfigurationSettings.AppSett ings["constring"].ToString();


        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            //Label9.Visible=false;
            //Label10.Visible=false;


        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);
            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEvent Handler(this.DataGrid1_PageIndexChanged_1);
            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEvent Handler(this.DataGrid1_SortCommand_1);


        }
        #endregion

        private void BindData(string stsort)
        {

            outError.InnerHtml="";
            SqlConnection mycon = new SqlConnection(strConString);

            SqlCommand MyComm= new SqlCommand("sp_search1", mycon);
            MyComm.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da =new SqlDataAdapter(MyComm);
            SqlParameter myParm1= MyComm.Parameters.Add("@fname", SqlDbType.VarChar,50);
            myParm1.Value =firstname.Text;
            SqlParameter myParm2= MyComm.Parameters.Add("@lname", SqlDbType.VarChar,50);
            myParm2.Value =lastname.Text;
            SqlParameter myParm3= MyComm.Parameters.Add("@membershipno", SqlDbType.VarChar,50);
            myParm3.Value =membershipno.Text;
            SqlParameter myParm4= MyComm.Parameters.Add("@email", SqlDbType.VarChar,100);
            myParm4.Value =emailid.Text;
            SqlParameter myParm5= MyComm.Parameters.Add("@strSort", SqlDbType.VarChar,50);
            myParm5.Value =stsort;

            mycon.Open();
            MyComm.ExecuteNonQuery();
            DataSet ds = new DataSet();
            da.Fill(ds,"firstcount");
            if (ds.Tables["firstcount"].Rows.Count.ToString() == "0")
            {
                outError.InnerHtml = "" +"There is no member matching your search criteria"+"";
            }
            else
            {
                Label12.Text=ds.Tables["firstcount"].Rows.Count.ToString();
                DataGrid1.DataSource=ds.Tables["firstcount"];
                DataGrid1.DataBind();
                mycon.Close();

            }
        }

        private void Button1_Click(object sender, System.EventArgs e)
        {


                Label9.Visible=true;
                Label10.Visible=true;
                BindData("firstname");


                if(firstname.Text!="")
                {
                    l1.Visible=true;
                    l1.InnerHtml="" + "First Name :"+"" +" "+firstname.Text;
                    l5.Visible=false;

                }
                if (lastname.Text!="")
                {
                    l2.Visible=true;
                    l2.InnerHtml="" + "Last Name :"+"" +" "+lastname.Text;
                    l5.Visible=false;
                }
                if (membershipno.Text!="")
                {
                    l3.Visible=true;
                    l3.InnerHtml="" + "Membership No. :"+"" +" "+membershipno.Text;
                    l5.Visible=false;
                }
                if (emailid.Text!="")
                {
                    l4.Visible=true;
                    l4.InnerHtml="" + "E-Mail Id :"+"" +" "+emailid.Text;
                    l5.Visible=false;

                }
                if((firstname.Text=="") && (lastname.Text=="")&&(membershipno.Text=="") && (emailid.Text==""))
                {
                    l5.Visible=true;
                    l5.InnerHtml="" + "Search for all members"+"";
                    l1.Visible=false;
                    l2.Visible=false;
                    l3.Visible=false;
                    l4.Visible=false;
                }



        }




        private void DataGrid1_SortCommand_1(object source, System.Web.UI.WebControls.DataGridSortCommandEvent Args e)
        {
            BindData(e.SortExpression);
        }

        private void DataGrid1_PageIndexChanged_1(object source, System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
        {
            if((e.NewPageIndex >= 0) & (e.NewPageIndex < DataGrid1.PageCount))
            {
                DataGrid1.CurrentPageIndex = e.NewPageIndex;
                BindData("firstname");
            }
            else
            {
                DataGrid1.CurrentPageIndex=0;
                BindData("firstname");
            }
        }


    }
}




.aspx code:

<%@ Page language="c#" Codebehind="nhrd_searchprofile.aspx.cs" AutoEventWireup="false" Inherits="nhrdadmin.nhrd_searchprofile" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
    <HEAD>
        <title>NHRD</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <LINK href="css/nhrd.css" type="text/css" rel="stylesheet">
            <script language="javascript">
            function fnwindowopen1(i)
            {

                window.open('nhrd_editsearch.aspx?userid='+i,"",'w idth=700; height=700; toolbar=false; scrollbars=yes');
                return false;
            }
            function fnrefresh1()
            {
                window.location.reload();
                //window.close();

            }


            </script>
            <LINK href="../CSS/tmm.css" type="text/css" rel="stylesheet">
    </HEAD>
    <body bgColor="#bfcfd5">
        <form name="form1" action="" runat="server">
            <table height="150" cellSpacing="0" cellPadding="0" width="760" align="center" border="0">
                <tr>
                    <td vAlign="bottom" bgColor="#ffffff" height="41">
                        <table cellSpacing="0" cellPadding="0" width="760" border="0">
                            <tr>
                                <td width="379" height="50">&nbsp;<IMG height="41" src="imgs/logo.gif" width="222"></td>
                                <td vAlign="bottom" width="381">
                                    <div align="right"><A href="../nhrd_new/index.asp">Home</A> |
                                        <A href="../nhrd_new/logout.asp">Logout</A> |</div>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr bgColor="#859494">
                    <td height="1"></td>
                </tr>
                <tr>
                    <td height="20">
                        <table cellSpacing="0" cellPadding="0" width="760" background="imgs/back.jpg" border="0">
                            <tr>
                                <td>
                                    <div align="center"><IMG height="120" src="imgs/ani_baner.gif" width="450"></div>
                                </td>
                                <td width="152">
                                    <div align="right"><IMG height="120" src="imgs/eye.jpg" width="152"></div>
                                </td>
                            </tr>
                        </table>
                        <table cellSpacing="1" cellPadding="0" width="760" align="center" bgColor="#bfcfd5" border="0">
                            <tr bgColor="#bfcfd5">
                                <td width="17">&nbsp;</td>
                            </tr>
                            <tr bgColor="#ffffff">
                                <td vAlign="top" width="17" bgColor="#ffffff">
                                    <p>&nbsp;</p>
                                </td>
                                <td vAlign="top" width="100%">
                                    <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
                                        <tr>
                                            <td vAlign="top"><br>
                                                <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
                                                    <tr>
                                                        <td colSpan="2"><b></b>
                                                            <div align="center">
                                                                <table height="50" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
                                                                    <tr vAlign="top">
                                                                        <td colSpan="2"><form name="PageNos" method="post" action="nhrd_searchprofile.asp">
                                                                                <div id="outError" align="center" runat="server"></div>
                                                                                <table width="100%" align="center" border="0">
                                                                                    <tr>
                                                                                        <td vAlign="top" align="left" width="75%" height="425">
                                                                                            <p>
                                                                                            <p>
                                                                                            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;<STRONG>Search
                                                                                                    Members</STRONG>
                                                                                            <P>
                                                                                                <TABLE style="WIDTH: 408px; HEIGHT: 157px" cellSpacing="1" cellPadding="1" width="408"
                                                                                                    align="center" border="1">
                                                                                                    <TR>
                                                                                                        <TD>First Name</TD>
                                                                                                        <TD><asp:textbox id="firstname" runat="server"></asp:textbox></TD>
                                                                                                    </TR>
                                                                                                    <TR>
                                                                                                        <TD>Last Name</TD>
                                                                                                        <TD><asp:textbox id="lastname" runat="server"></asp:textbox></TD>
                                                                                                    </TR>
                                                                                                    <TR>
                                                                                                        <TD>Membership No.</TD>
                                                                                                        <TD><asp:textbox id="membershipno" runat="server"></asp:textbox></TD>
                                                                                                    </TR>
                                                                                                    <TR>
                                                                                                        <TD>E-Mail ID</TD>
                                                                                                        <TD><asp:textbox id="emailid" runat="server"></asp:textbox></TD>
                                                                                                    </TR>
                                                                                                    <TR>
                                                                                                        <TD></TD>
                                                                                                        <TD>
                                                                                                            <P>&nbsp;</P>
                                                                                                            <P><asp:button id="Button1" runat="server" Text="Search"></asp:button></P>
                                                                                                        </TD>
                                                                                                    </TR>
                                                                                                </TABLE>
                                                                                                <BR>
                                                                                                <BR>
                                                                                                <asp:label id="Label9" runat="server" Font-Bold="True">Search Criteria:</asp:label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;
                                                                                                <asp:label id="Label10" runat="server" Font-Bold="True">No. of search results:</asp:label>&nbsp;&nbsp;
                                                                                                <asp:label id="Label12" runat="server" Font-Bold="True"></asp:label>
                                                                                                <div id="l1" runat="server"></div><br>
                                                                                                <div id="l2" runat="server"></div><br>
                                                                                                <div id="l3" runat="server"></div><br>
                                                                                                <div id="l4" runat="server"></div><br>
                                                                                                <div id="l5" runat="server"></div>
                                                                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                                                                                <BR>
                                                                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
Old December 4th, 2004, 07:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What a lot of code. You must have missed something in what I said earlier:
Quote:
quote:Can you post the relevant parts of your code?
But I think it's indeed the problem I mentioned earlier. When your user searches for a new name, that has no (or less) matching records, your selected index is incorrect.

For example, you're browsing page 4 for John and then search for lily611 which has only two pages of records. Your current code doesn't reset the selected index, so you're trying to view page 4 of a resultset that only has two pages.

Does this help? If not, can you repost your code and limit it to the relevant stuff? I don't feel like wading through 6 square foot of code with both horizontal and vertical scroll bars.....


Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: The Tourist by Radiohead (Track 12 from the album: OK Computer) What's This?
 
Old December 8th, 2004, 10:00 AM
Authorized User
 
Join Date: Mar 2004
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I didn't go through your codes but it is probably because you didn't reset your CurrentPageIndex to 0 each time you bind your data. Try to put DataGrid1.CurrentPageIndex = 0; after DataGrid1.DataBind(); in your BindData() void function.







Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid Error rsm42 ASP.NET 1.0 and 1.1 Basics 0 January 2nd, 2007 02:29 PM
Error in displaying datagrid lamort ASP.NET 1.0 and 1.1 Basics 0 December 1st, 2006 01:02 PM
datagrid paging error RPG SEARCH ASP.NET 1.0 and 1.1 Basics 1 November 9th, 2004 08:29 AM
One Row in DataGrid error petersonginae VB.NET 2002/2003 Basics 2 July 14th, 2004 05:14 PM
DataGrid Error spm74 ASP.NET 1.0 and 1.1 Basics 0 May 20th, 2004 06:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.