Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,
This my whole code..

C#
////////////
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Admin_deletescore : System.Web.UI.Page
{
string username = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["UserName"] != null)
{
username = Convert.ToString(Session["UserName"]);
lblUser.Text = "Welcome" + ' ' + username;
}
else
{
Response.Redirect("login.aspx");
}
bindTournament();
bindRecentMatches();
}
}
private void bindTournament()
{
try
{
string str = "select_t_by_organizer_name";
SqlConnection objCon;
SqlCommand objCmd;
DataSet objDs = new DataSet();
SqlDataAdapter objDa;
using (objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
objCon.Open();
using (objCmd = new SqlCommand(str, objCon))
{
if (username == "vipin")
{
GetTournament();
}
else
{
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.AddWithValue("@eventorganizer", Convert.ToString(Session["UserName"]));
using (objDa = new SqlDataAdapter(objCmd))
{
objDa.Fill(objDs);
if (objDs.Tables[0].Rows.Count > 0)
{
if (grdlistTeam != null)
{
ddlComp.DataSource = objDs.Tables[0].DefaultView;
ddlComp.DataTextField = "tname";
ddlComp.DataValueField = "tid";
ddlComp.DataBind();
ddlComp.Items.Insert(0, new ListItem("Select Tournament"));
}
}
else
{
}
}
}
}
}
}
catch (Exception ex)
{
}
}
private void GetTournament()
{
SqlConnection objCon;
SqlCommand objCmd;
string strQuery;
DataSet objDs = new DataSet();
SqlDataAdapter objDa;
int i = 0;
try
{
strQuery = "select * from tdescription order by TDescription.TStartDate ASC";
using (objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
objCon.Open();
using (objCmd = new SqlCommand(strQuery, objCon))
{
using (objDa = new SqlDataAdapter(objCmd))
{
i = objDa.Fill(objDs);
if (i > 0)
{
ddlComp.DataSource = objDs;
ddlComp.DataTextField = "TName";
ddlComp.DataValueField = "TtId";
ddlComp.DataBind();
ddlComp.Items.Insert(0, new ListItem("--Select Tournament--", "0"));
}
}
}
}
}
catch (Exception ex)
{
Exceptionhandle exception = new Exceptionhandle();
exception.Exceptionhandle1(ex.ToString());
}
}
private void bindRecentMatches()
{
SqlConnection objCon;
SqlCommand objCmd;
string strQuery;
DataSet objDs = new DataSet();
SqlDataAdapter objDa;
int i = 0;
try
{
strQuery = "select_recent_match_by_eventorganizer";
using (objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
objCon.Open();
using (objCmd = new SqlCommand(strQuery, objCon))
{
objCmd.CommandType = CommandType.StoredProcedure;
//objCmd.Parameters.AddWithValue("@eventorganizer", username);
objCmd.Parameters.AddWithValue("@eventorganizer", Convert.ToString(Session["UserName"]));
using (objDa = new SqlDataAdapter(objCmd))
{
// try {
i = objDa.Fill(objDs);
if (objDs.Tables[0].Rows.Count > 0)
{
if (grdlistTeam != null)
{
grdlistTeam.DataSource = objDs.Tables[0].DefaultView;
grdlistTeam.DataBind();
}
}
else
{
}
//} catch (Exception ej) { throw ej; }
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void grdlistTeam_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
if (e.Row.RowType != DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
cell.Attributes.Add("Style", "border-bottom:5px solid #e1dfe0;");
}
Label lblDate = (Label)e.Row.FindControl("lbldate");
string dtCust = ConvertToDate(lblDate.Text);
lblDate.Text = dtCust;
Label lblResult = (Label)e.Row.FindControl("lblResult");
if (lblResult.Text.Length > 35)
{
lblResult.ToolTip = lblResult.Text;
lblResult.Text = lblResult.Text.Substring(0, 35) + "..";
}
else
{
lblResult.Text = lblResult.Text;
lblResult.ToolTip = lblResult.Text;
}
LinkButton lnkFirstTeam = (LinkButton)e.Row.FindControl("lnkFirstTeam");
if (lnkFirstTeam.Text.Length > 15)
{
lnkFirstTeam.ToolTip = lnkFirstTeam.Text;
lnkFirstTeam.Text = lnkFirstTeam.Text.Substring(0, 15) + "..";
}
else
{
lnkFirstTeam.Text = lnkFirstTeam.Text;
lnkFirstTeam.ToolTip = lnkFirstTeam.Text;
}
LinkButton lnkSecondTeam = (LinkButton)e.Row.FindControl("lnkSecondTeam");
if (lnkSecondTeam.Text.Length > 15)
{
lnkSecondTeam.ToolTip = lnkSecondTeam.Text;
lnkSecondTeam.Text = lnkSecondTeam.Text.Substring(0, 15) + "..";
}
else
{
lnkSecondTeam.Text = lnkSecondTeam.Text;
lnkSecondTeam.ToolTip = lnkSecondTeam.Text;
}
LinkButton lnkTournament = (LinkButton)e.Row.FindControl("lnkTournament");
if (lnkTournament.Text.Length > 9)
{
lnkTournament.ToolTip = lnkTournament.Text;
lnkTournament.Text = lnkTournament.Text.Substring(0, 9) + "..";
}
else
{
lnkTournament.Text = lnkTournament.Text;
lnkTournament.ToolTip = lnkTournament.Text;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
public static string ConvertToDate(string DateString)
{
string[] strArr = DateString.Split('-');
string strMM = strArr[0];
string strDD = strArr[1];
string strYYYY = strArr[2];
DateString = strDD + " " + strMM + "," + strYYYY;
return DateString;
}
protected void grdlistTeam_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdlistTeam.PageIndex = e.NewPageIndex;
bindRecentMatches();
}
protected void ddlComp_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
// string str = "select * from currentmatch where tid=@tid order by EventDate ASC";
string str = "select * from currentmatch where tid=@tid";
SqlConnection objCon;
SqlCommand objCmd;
DataSet objDs = new DataSet();
SqlDataAdapter objDa;
using (objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
objCon.Open();
using (objCmd = new SqlCommand(str, objCon))
{
objCmd.CommandType = CommandType.Text;
objCmd.Parameters.AddWithValue("@tid", Convert.ToInt32(ddlComp.SelectedValue));
using (objDa = new SqlDataAdapter(objCmd))
{
objDa.Fill(objDs);
if (objDs.Tables[0].Rows.Count > 0)
{
grdlistTeam.DataSource = objDs.Tables[0].DefaultView;
grdlistTeam.DataBind();
}
else
{
grdlistTeam.EmptyDataText = "No record found";
grdlistTeam.DataBind();
}
}
}
}
}
catch (Exception ex)
{
}
}
}


GridView source.

ASP.NET
runat="server" AutoPostBack="true" AllowPaging="true"  
                                            AllowSorting="true" AutoGenerateColumns="false" DataKeyNames="Currentmatch_Id" PageSize="10"
                                            OnPageIndexChanging="grdlistTeam_PageIndexChanging" 
                                            OnRowDataBound="grdlistTeam_RowDataBound1" ShowHeader="false" 
                                            Width="100%" PagerSettings-Mode="NumericFirstLast">

//////////////
Thanks in advance.
Posted
Updated 25-Sep-14 4:54am
v4
Comments
Duncan Edwards Jones 25-Sep-14 5:43am    
Why are you catching the exception and not doing anything with it? Surely if something goes wrong you want to be told about it?
Rajesh Rathor 25-Sep-14 5:47am    
Hi,
I debug code but it not going in catch exception.
but still if you want i will use them.
Thanks

In your code it is not clear if the GridView has the AllowPaging = true. This might be set in the .aspx file

You don't set the PageIndex property for the grdlistTeam GridView in the BindRecentMatches() method:
You should add a property in your class so you can use the NewPageIndex value from the grdlistTeam_PageIndexChanging method in the BindRecentMatches method.

"cs"
grdlistTeam.DataSource = objDs.Tables[0].DefaultView;
grdlistTeam.DataBind()


You must set the grdLstTeam.PageIndex value before the grdLisTeal.DataBind() method.
In fact what yo do now is set a PageIndex and reload all the data after that. Since there is no data when setting the pageindex, the pageindex will be 0 when addng the new dataset (see DataSet objDs = new DataSet(); in BindRecentMatches()). So after the data is added the correct pageIndex can be set.
 
Share this answer
 
v2
Comments
Rajesh Rathor 25-Sep-14 6:04am    
Hi,
It is true.

/////////////
<asp:GridView ID="grdlistTeam" runat="server" AutoPostBack="true" AllowPaging="true"
AllowSorting="true" AutoGenerateColumns="false" DataKeyNames="Currentmatch_Id" PageSize="10"
OnPageIndexChanging="grdlistTeam_PageIndexChanging"
OnRowDataBound="grdlistTeam_RowDataBound1"
Herman<T>.Instance 25-Sep-14 6:07am    
I assumed it would be true based on the fact you have the PageIndexChanged event in the code.

So... set the PageIndex value just before the DataBind() method. Store the value of the new pageindex in an int variable or property and use that one in the BindRecentMatches method.

Good luck!
Rajesh Rathor 25-Sep-14 6:24am    
Hi,
Can you use my code and add what you saying.
I am not able to understand it theoretically.

Thanks
Herman<T>.Instance 25-Sep-14 6:34am    
when you read ..... means further on up the code

public partial class Admin_deletescore : System.Web.UI.Page
{
string username = string.Empty;
int currentGridViewPageIndex = 0;

.....

private void bindRecentMatches()
......

if (grdlistTeam != null)
{
grdlistTeam.DataSource = objDs.Tables[0].DefaultView;
grdlistTeam.PageIndex = currentGridViewPageIndex;
grdlistTeam.DataBind();
}
................

protected void grdlistTeam_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
currentGridViewPageIndex = e.NewPageIndex;
bindRecentMatches();
}
Rajesh Rathor 25-Sep-14 6:51am    
Hi,
Thanks but not working.
Check the below points

1. Check GridView's AllowPaging Propery to True.
2 .Check Fire GridView's PageIndexChanging event.
 
Share this answer
 
Comments
Rajesh Rathor 25-Sep-14 7:06am    
Hi,
I request please check the Whole post.
Thanks
Hi,
Now i changed some code but now it not giving any data on page index clicked.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

if (Session["UserName"] != null)
{
username = Convert.ToString(Session["UserName"]);
lblUser.Text = "Welcome" + ' ' + username;

}
else
{
Response.Redirect("login.aspx");
}

bindTournament();
// ViewState["SortExpr"] = Sort_Direction;
DataView dvEmployee = Getdata();
grdlistTeam.DataSource = dvEmployee;
grdlistTeam.DataBind();


/* string strQuery = "select c.*, eventorganizer from currentmatch c join tournamentdescription on c.tournamentid=tournamentdescription.tournamentid where tournamentdescription.eventorganizer=@eventorganizer";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
grdlistTeam.DataSource = dt;
grdlistTeam.DataBind(); */

// bindRecentMatches();

}


private DataView Getdata()
{
string strQuery = "select_recent_match_by_eventorganizer";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sports"].ToString()))
{
DataSet dsEmployee = new DataSet();
// string strQuery = "select c.*,eventorganizer from currentmatch c join TournamentDescription on c.tournamentid= TournamentDescription.tournamentid where TournamentDescription.eventorganizer=@eventorganizer order by TournamentDescription.TournamentStartDate ASC";
SqlCommand strSelectCmd = new SqlCommand(strQuery,conn);
// SqlCommand strSelectCmd = "select c.*, eventorganizer from currentmatch c join tournamentdescription on c.tournamentid=tournamentdescription.tournamentid where tournamentdescription.eventorganizer=@eventorganizer";
strSelectCmd.CommandType = CommandType.StoredProcedure;
//objCmd.Parameters.AddWithValue("@eventorganizer", username);
strSelectCmd.Parameters.AddWithValue("@eventorganizer", Convert.ToString(Session["UserName"]));
// strSelectCmd.Parameters.AddWithValue("@eventorganizer", username);
SqlDataAdapter da = new SqlDataAdapter(strSelectCmd);
da.Fill(dsEmployee, "tournamentdescription");
DataView dvEmp = dsEmployee.Tables["tournamentdescription"].DefaultView;
// dvEmp.Sort = ViewState["SortExpr"].ToString();
return dvEmp;
}
}

protected void grdlistTeam_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdlistTeam.PageIndex = e.NewPageIndex;
DataView dvEmployee = Getdata();
grdlistTeam.DataSource = dvEmployee;
grdlistTeam.DataBind();
}

Is there anyone who have the solution for my problem.

Thanks
 
Share this answer
 

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