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();
}
}
}