Why is not this gridview loading the results? - asp.net

I am trying to fill the gridview with the search results. Here is the UI of my "AddDropClasses" page:
When this page is loaded, i want the gridview to be filled the current courses of the current user, and i do this using the Register table. Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = Userfunctions.GetConnectionString();
con.Open();
string query = "select * from RegisterTable where StudentID='" + MyGlobals.currentID + "'";
SqlDataAdapter adap = new SqlDataAdapter(query, con);
DataTable tab = new DataTable();
adap.Fill(tab);
showCourses.DataSource = tab;
}
I load (come to) this page by clicking a button from another page. The problem is, the table shows no results. When i debugged i realized that Myglobals.ID has the value what i already expect. What can be the problem here? Can anyone help?;
Thanks

You forgot to databind showCourses. Add showCourses.DataBind(); after showCourses.DataSource = tab;

Related

Load report failed, If put SQL Command in place otherwise FormLoad

I have CrystalReport page, The below code is just load one time through FormLoad and by using !Page.IsPostBack and If I try to reload it again give me error "Load report failed."
if (!Page.IsPostBack)
{
cmd = new SqlCommand("SELECT * FROM [tbJournals]", cnn);
cnn.Open();
da = new SqlDataAdapter(cmd);
dt = new DataTable("tbl");
da.Fill(dt);
cnn.Close();
_rdStudents = new ReportDocument();
_rdStudents.Load(Server.MapPath("~\\Accounting\\Journal_Report.rpt"));
_rdStudents.SetDatabaseLogon("sa", "password", ".\\SQLEXPRESS", "GoldenDeveloper");
_rdStudents.SetDataSource(dt);
CrystalReportViewer1.ReportSource = _rdStudents;
CrystalReportViewer1.RefreshReport();
}
But What I want to do put this code under CommandButton to change in search variable to make SQL Statement for example like that
SELECT * FROM [tbJournals] where [Date] = '"+ txtDate.Text +"'
But once I put the code instead of FormLoad to CommandButton give me error "Load report failed."
Thanks In Advance ....
I think the next time, when you reload then the
(!IsPostBack)
{
//This works only when the first time page loads.
}
So the code written in the same will not get executed.Try to write the code in Page_Load and after this function
like:
protected void Page_Load(object sender, EventArgs e)
{
cmd = new SqlCommand("SELECT * FROM [tbJournals]", cnn);
cnn.Open();
da = new SqlDataAdapter(cmd);
dt = new DataTable("tbl");
da.Fill(dt);
cnn.Close();
_rdStudents = new ReportDocument();
_rdStudents.Load(Server.MapPath("~\\Accounting\\Journal_Report.rpt"));
_rdStudents.SetDatabaseLogon("sa", "password", ".\\SQLEXPRESS", "GoldenDeveloper");
_rdStudents.SetDataSource(dt);
CrystalReportViewer1.ReportSource = _rdStudents;
CrystalReportViewer1.RefreshReport();
}
Solved finally, The problem was in report path, I did "CrystalReportViewer" report path blank and putted the report path like that _rdStudents.Load(Server.MapPath("Journal_Report.rpt")); by using Server.MapPath

Asp.Net Datagrid view is not updating on new insertion

Well As the title explains it all so here is the code i used so far
public partial class data : System.Web.UI.Page
{
System.Data.SqlClient.SqlConnection con3;
System.Data.SqlClient.SqlDataAdapter da;
DataSet ds1;
DataTable dt;
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
con3 = new System.Data.SqlClient.SqlConnection();
con3.ConnectionString="Data Source=localhost; initial catalog=test;user id=xx;password=xxxx;";
con3.Open();
ds1 = new DataSet();
SqlCommand cmd3 = new SqlCommand();
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.CommandText = "selectdata";
cmd3.Connection = con3;
da = new SqlDataAdapter (cmd3);
da.Fill(ds1, "abc");
con3.Close();
dt = new DataTable();
dt = ds1.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
Stored Procedure is
Create PROCEDURE [dbo].[selectdata]
AS
BEGIN
select * from data
END
i searched stack overflow & did search on Google but nothing happened.
i tried closing connection after data bind, defining a new data set & creating the table again & much more please help
Try reloading the page after insertion.
The Answer Above just refresh the page so in my view it should be
1--> Define A Function & bind data in this such as
private void bind_data()
{
ASPxGridLookup2.DataSource = dt_employee;
ASPxGridLookup2.DataBind();
ASPxGridLookup3.DataSource = dt_attendance;
ASPxGridLookup3.DataBind();
ASPxGridView1.DataSource = dt_attendance;
ASPxGridView1.DataBind();
}
2--> Call this function whenever you need to refresh the datagrid such as right after insertion
obj2.insertion();
bind_data();

asp.net paging, 1st page shows records, but 2nd page is empty

I've used asp.net gridview control. In gridview doing paging manually. But only first page shows the records. the page is completely blank. can anyone suggest what & where will I need to modify for the PageIndex Events?
You need to change the page index of grid and bind it in PageIndexChanged.
void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
GridView1.CurrentPageIndex = e.NewPageIndex;
YourFunctionToBindGrid();
}
//Solved 100% work
use this code inside
Page_index_Changing
{
GridView1.PageIndex = e.NewPageIndex;
SqlCommand cmd = new SqlCommand("Select * from Requseted_movie ORDER BY [ID] DESC", con);
SqlDataAdapter DA1 = new SqlDataAdapter(cmd);
DA1.Fill(DT1);
GridView1.DataSource = DT1;
GridView1.DataBind();
}

sorting filtered data in asp.net listview

I've created a listview that's filled up with a list of guitars from the database on page load like so:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["GuitarsLTDBConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM Guitars", con);
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);
DataTable dt = new DataTable();
da.Fill(dt);
lvGuitars.DataSource = dt;
lvGuitars.DataBind();
}
The following code filters that list of guitars by a certain Make when the user checks the checkbox corresponding to that make
protected void chkOrgs_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)lvGuitars.DataSource;
DataView dv = new DataView(dt);
if (chkOrgs.SelectedValue == "Gibson")
{
dv.RowFilter = "Make = 'Gibson' OR Make='Fender'";
}
lvGuitars.DataSource = dv.ToTable();
lvGuitars.DataBind();
}
Now, what I want to do is be able to sort the latest data that is present within the listview. Meaning, if sort is clicked before filtering, the it should sort all data. If sort is clicked after filtering, it should sort the filtered data. I'm using the following
code, which is triggered upon a LinkButton click
protected void lnkSortResults_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)lvGuitars.DataSource;
DataView dv = new DataView(dt);
dv.Sort = "Make ASC";
lvGuitars.DataSource = dv.ToTable();
lvGuitars.DataBind();
}
The problem is that all the data that the listview was loaded with before any filtering is sorted, and not the latest filtered data. How can I change this code so that the latest data available in the listview is the one that's sorted?
Thanks
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["GuitarsLTDBConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM Guitars", con);
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);
DataTable dt = new DataTable();
da.Fill(dt);
lvGuitars.DataSource = dt;
lvGuitars.DataBind();
ViewState["dt"] = dt;
}
The loading need to be set like this. Access/Update the Viewstate as well on filter and Sorting.

Gridview from button click inside a popup window

I have a text box with event name and two calendar controls to get the date and a button to display a gridview.
I need to display the grid with the details like eventname start date and end date from the database..
My code is .aspx.cs
protected void lnbtnSearch_OnClick(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=CTSINDLFVMOSS;Initial Catalog=DB_CGTPO_DEVE;Persist Security Info=True;User ID=*****;Password=*****");
SqlDataAdapter adapter = new SqlDataAdapter("select EventId,EventName,StartDate,EndDate,Tactics,Perct_VolLift,Perct_ROI from TableNamewhere (( EventName='" + textfield3 + "') and (StartDate= StartDate) and ( EndDate= EndDate))", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
grdEventDetails.DataSource = ds;
grdEventDetails.
}
set grdEventDetails.AutoGenerateColumns = true;
add grdEventDetails.DataBind();
but it will look ugly (especially date fields) if you don't define the columns manually.

Resources