Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I search the gridview record it search but repeated infinity times help me.

This is my code behind:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Data;
using AjaxControlToolkit;

namespace Virtual_Classroom.Student
{
    public partial class Inbox : System.Web.UI.Page
    {

       
        protected void Page_Load(object sender, EventArgs e)
        {
           if (!this.IsPostBack)
    {
        this.BindGrid();
    }
        }
private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
             cmd.CommandText="SELECT ReplyDoubtID,Name,Subject,ReplyDoubt,Date,Files,FileName,Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt";
            
             
            cmd.Connection = con;
            
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }
}
 

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument);
                Response.TransmitFile(Server.MapPath("~/Files/") + e.CommandArgument);
                Response.End();
            }
        }

        protected void OnRatingChanged(object sender, RatingEventArgs e)
        {
            int rowIndex = ((sender as Rating).NamingContainer as GridViewRow).RowIndex;
            int ReplyDoubtID = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);
            string constr = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO Rates VALUES(@ReplyDoubtID, @Rating)"))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@ReplyDoubtID", ReplyDoubtID);
                    cmd.Parameters.AddWithValue("@Rating", e.Value);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           string constr = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = " SELECT * FROM ReplyDoubt,Rates WHERE Topic LIKE '%' + @Topic + '%'"; ;
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Topic", TextBox3.Text.Trim());
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BindGrid();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

       
    }
}
Posted
Updated 30-Mar-15 19:39pm
v3
Comments
Sinisa Hajnal 31-Mar-15 3:48am    
Remove BindGrid call from pageIndexChanging. That should resolve the loop.

1 solution

Try this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Data;
using AjaxControlToolkit;
 
namespace Virtual_Classroom.Student
{
    public partial class Inbox : System.Web.UI.Page
    {
 
       
        protected void Page_Load(object sender, EventArgs e)
        {
           if (!this.IsPostBack)
    {
        this.BindGrid();
    }
        }
private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
             cmd.CommandText="SELECT ReplyDoubtID,Name,Subject,ReplyDoubt,Date,Files,FileName,Topic, ISNULL((SELECT AVG(Rating) FROM Rates WHERE ReplyDoubtID = ReplyDoubt.ReplyDoubtID), 0) Rating FROM ReplyDoubt";
            
             
            cmd.Connection = con;
            
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }
}
 
 
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
 
        }
 
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument);
                Response.TransmitFile(Server.MapPath("~/Files/") + e.CommandArgument);
                Response.End();
            }
        }
 
        protected void OnRatingChanged(object sender, RatingEventArgs e)
        {
            int rowIndex = ((sender as Rating).NamingContainer as GridViewRow).RowIndex;
            int ReplyDoubtID = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);
            string constr = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO Rates VALUES(@ReplyDoubtID, @Rating)"))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@ReplyDoubtID", ReplyDoubtID);
                    cmd.Parameters.AddWithValue("@Rating", e.Value);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
           string constr = ConfigurationManager.ConnectionStrings["VCRConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = " SELECT * FROM ReplyDoubt,Rates WHERE Topic LIKE '%' + @Topic + '%'"; ;
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Topic", TextBox3.Text.Trim());
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }
        }
 
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {           
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
 
       
    }
}
 
Share this answer
 
v2

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