I am trying to make a gridview sortable which uses a stored procedure as a datasource, I would not want it to rerun the query each time to achieve this. How would I get it to work my current code is:
protected override void OnPreRender(EventArgs e)
{
if (!IsPostBack)
{
SqlCommand cmd2 = new SqlCommand("SR_Student_Course_List", new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RDCV2ConnectionString"].ConnectionString));
try
{
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.CommandTimeout = 120;
cmd2.Parameters.Add("student_id", SqlDbType.Char, 11).Value = student;
cmd2.Connection.Open();
grdCourses.DataSource = cmd2.ExecuteReader();
grdCourses.DataSourceID = string.Empty;
grdCourses.DataBind();
} finally
{
cmd2.Connection.Close();
cmd2.Connection.Dispose();
cmd2.Dispose();
}}}
This code just binds the data when it isn't a postback, the gridview has viewstate enabled. On pressing the column headers a postback happens but no sorting occurs. If anyone has a simple fix for this please let me know or even better an ajax sort which would avoid the postback would be even better. The dataset is relatively small however takes a long time to query which is why I would not like to requery on each sort.
If you are not paging the results, and just doing a read, then something like the jquery tablesorter plugin would be a quick and easy fix. I have used this on tables of up to 1400 rows and works great, although ~> few hundred probably better on slow putas.
If the gridview is editable, then aspnet event/input validation might spit a dummy if you don't go through the proper registration of client scripts etc.
You could try storing the data in view state (or cache).
In your case, I would use a SqlDataAdapter and fill a DataTable. Then, put the DataTable into a Session variable. When the GridView is sorting, check if the Session variable still exists. If it does not, then fill the DataTable again. Finally sort the DataTable using a DataView and rebind the GridView with the DataView.
Related
In my ASP.NET page, I'm using a GridView to view data (Items & their prices). Currently users can edit the data (prices) in the grid row by row. (Click --> "Edit" link, change the values then "Update"). This is ROW by ROW. Is it possible to open all rows in Edit mode & use a single button (eg. Submit) to update all data once?
If you don't need to read only mode, in that case you can put input boxes ( textbox, dropdownlist, etc.) in ItemTEmplate section and bind them with existing data.
Next, put a submit button at above/below of the GridView and handle button Click event and loop through the GridView item data and save all database.
I'll post code block if you need. Thanks for your time.
And you will have better control on that you doing by using listview instead of gridview.
My best practise is using listview and custom web user control for this kind of problems.
If you fill your listview with your user control, you will easy to manage your saving method, just have to iterate on the listview items, find control and call your Save() method for each item.
I know that this question has already been answered but here is the code for loop through the GridView getting data and store it in the Data Base:
Using libraries:
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.Odbc;
Code Behind:
// this is a variable that have the Query or SQL Commands.
string DataBaseQuery = "UPDATE [table] SET [variable2] = #variable2, [variable3] = #variable3) WHERE [variable1] = #variable1";
//Click Event from a LinkButton.
protected void LinkButton1_Click(object sender, EventArgs e)
{
//"ConnectionString" its the string connection for your DataBase (often get from the WebConfig File or a DataSource element.
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
//this is for open the database using the string connection.
connection.Open();
//this is the algorithm for going through the entire GridView.
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//"DataBaseQuery" it's a string variable that have the Query or SQL Commands.
SqlCommand cmd = new SqlCommand(DataBaseQuery, conexion);
//this case it's for obtain the text variable of the first column of the gridview (in my case it was the ID of the register).
cmd.Parameters.AddWithValue("#variable1", ((Label)GridView1.Rows[i].Cells[0].FindControl("Label1")).Text.ToString());
//this case it's for obtain the selected value of a DropDownList that were in the 14 th column)
cmd.Parameters.AddWithValue("#variable2", ((DropDownList)GridView1.Rows[i].Cells[15].FindControl("DropDownlist2")).SelectedValue.ToString());
//this command it's for obtain the text of a textbox that is in the 15 th column of the gridview.
cmd.Parameters.AddWithValue("#variable3", ((TextBox)GridView1.Rows[i].Cells[16].FindControl("TextBox17")).Text.ToString());
cmd.ExecuteNonQuery();
}
//after going through all the gridview you have to close the connection to the DataBase.
connection.Close();
}
}
Of course you have to adjust the code to your particular case but it's very easy. In this code you have the example to obtain values for other object like labes, textbox and dropdownlist in the gridview.
I suffered a lot to make run this code (I'm not good programming) but I'm happy to help.
NOTE: For count the columns of the gridview you have to start at zero.
NOTE2: Sorry for my bad English by the way... It's not my nature language.
In my simple starter asp page I create a DataTable and populate it with two rows. The web site allows users to add new rows. My problem is the DataTable doesn't save the information. I've stepped through the code and the row gets added but the next time a row is added it's not there, only the original two and the newest one to get entered.
I have gotten around this, in an inelegant way, but am really curious why the new rows are being saved.
My code:
protected void Page_Load(object sender, EventArgs e)
{
_Default.NameList = new DataTable();
DataColumn col = new DataColumn("FirstName", typeof(string));
_Default.NameList.Columns.Add(col);
col = new DataColumn("LastName", typeof(string));
_Default.NameList.Columns.Add(col);
DataRow row = _Default.NameList.NewRow();
row["FirstName"] = "Jane";
row["LastName"] = "Smith";
_Default.NameList.Rows.Add(row);
row = _Default.NameList.NewRow();
row["FirstName"] = "John";
row["LastName"] = "Doe";
_Default.NameList.Rows.Add(row);
}
protected void AddButton_Click(object sender, EventArgs e)
{
DataRow row = _Default.NameList.NewRow();
row["FirstName"] = this.TextBox1.Text;
row["LastName"] = this.TextBox2.Text;
_Default.NameList.Rows.Add(row);
_Default.NameList.AcceptChanges(); // I've tried with and without this.
}
I've tried saving them to GridView control but that seems like quite a bit of work.
I'm new to ASP.Net but have done windows programming in C# for the last two years.
You're creating a new DataTable object each time the page loads.
You need to persist the DataTable object to session state or a static variable, or save the data to a database.
Remember that handling events like your button click requires a full postback. You don't run just the click code, you run your entire page lifecycle on a new instance of your page class. The new instance of your page class means a new instance of the datatable as well.
The issue here is that your DataTable is being created every time your page loads and it goes out of scope when your page has finished loading and been displayed to the user. To get your desired effect, you will need to store the DataTable in either Session, ViewState, cache, use a control like GridView that will automatically store the underlying data in its state, or something else.
Since you're new to ASP.NET, check out your options for state management.
I have a *.aspx page that contains a text box and a button. When the user enters information in to the textbox and clicks post, it inserts the data into my sql database. The issue is, that if the user hits refresh, it will keep inserting the same data into the database. I am pretty sure the whole "click" method is called, not just the insert call. I tried messing with sessions but it complains. I am not really sure what to do. I am looking for a simple easy fix.
protected void PostButton_Click(object sender, EventArgs e)
{
string wpost = (string)Session["WallPost"];
DateTime wtime = (DateTime)Session["WallDateTime"];
if (txtWallPost.Text.Length > 0 && !wpost.Equals(txtWallPost.Text))
{
string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strCon))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "INSERT INTO [WallTable] ([UserId], [FriendId], [WallPost]) VALUES (#UserId, #FriendId, #WallPost)";
cmd.Parameters.AddWithValue("#UserId", User.Identity.Name);
cmd.Parameters.AddWithValue("#FriendId", User.Identity.Name);
cmd.Parameters.AddWithValue("#WallPost", txtWallPost.Text);
conn.Open();
cmd.ExecuteNonQuery();
Session.Add("WallPost", txtWallPost.Text);
Session.Add("WallDateTime", new DateTime());
conn.Close();
txtWallPost.Text = "";
LoadWallPosts();
}
}
}
return;
}
If the PostButton_Click is the server handler for the click event on your button then insert code shold be executed only if the user hits the post button - it should not happen in case of refresh.
I am pretty sure is not enough - to be sure put a breakpoint in your PostButton_Click method, hit F5 to run in debug mode and see if the breakpoint gets hit with a refresh.
If so it means that:
you are calling explicitly
PostButton_Click from somewhere
else (Page_Load?)
PostButton_Click was accidentally
set as event handler for some other
event on the page
A quick way of verifying hypotesis 1 & 2 is to run a search for PostButton_Click and see where you are calling it from or if it set as handler for some other element (maybe your form?) in the markup.
Add Response.Redirect(Request.Url.ToString(), false); to your button event and that should solve the problem.
if he refreshes or press the back button the post will happen again, thus your page will process the same data thus insert once again.
You have to change your logic
It would be preferable to redirect the client to another page after the insert, however, if you really need the client stay in the same page, you can use a (bool) variable on your page to state if the insert has been done, and skip the insert logic if it has been executed before
I have a gridview (actually a SPgridview)
And i made the columnname clickable so the users can sort the rows using the data.
And that works fine.
The problem occurs when users try to select a row after they sorted the data.
I can see that the gridview kinda "forgets" how the rows were sorted and selects the row that was at the clicked index before it got sorted..
How do i fix that?
I tried sorting the row again after the user selects a row, but that doesnt seem to work.
And should the gridview remember the fact that it was just sorted?
Thanks in advance :)
When you handle the Sorting event set a session variable set it to the sort direction and use it when you rebind your datasource.
protected void gridview_Sorting()
{
// BIND DATA Function
BindData();
DataTable dt = gridview.DataSource as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
Session["sort"] = dt.DefaultView.Sort;
gridview.DataSource = dt;
gridview.DataBind();
}
}
// bind data function//
private void BindData()
{
DataTable dt = GetDataTable();
if (Session["sort"] != null)
{
//Sort the data.
dt.DefaultView.Sort = Session["sort"].ToString();
}
gridview.DataSource = dt;
gridview.DataBind();
}
Make sure that you are not rebinding the grid after a postback.
if(!IsPostBack)
{
gridView.DataSource = yourDataSource;
gridView.DataBind();
}
Are you grabbing the selected row by it's row index or by the unique identifier of the data you are wanting to edit? If you're getting by row index, it may be 'forgetting' since you are recreating the Grid on OnPostBack. Try iterating through the data and select it by it's unique ID, not its row index.
Check Johans blog regarding SPGridView and LinqDataSource
I'd made a number of sortable GridViews, but none with row command interaction before today, when I stumbled on this problem. Mine is a "plain" GridView, not SPgridview. I found this works:
In bindData(), if we have not created a DataTable and put it in the Session object, do so. Otherwise, we'll use the existing, sorted DataTable:
if (Session["dtbl"] == null) {
Session["dtbl"] = method_to_fetch_datatable();
}
gv.DataSource = Session["dtbl"] as DataTable;
gv.DataBind();
In the GridView's handling of row commands that INSERT, UPDATE or DELETE underlying data, refresh the Session object, maintaining the sort, if there is one:
Session["dtbl"] = method_to_fetch_datatable();
if (ViewState["SortExpression"] != null) {
DataTable dt = Session["dtbl"] as DataTable;
dt.DefaultView.Sort = ViewState["SortExpression"] as string;
}
bindData();
I got it working. (kinda)
In the sorting event i saved the sortexpression (the name of the column used to sort by)
and the sortdirection ascending or descending.
Then i make the datasource for the gridview and databind, and after databinding it, i use the gridview.sort command to sort by the values i saved in viewstate.
That works fine, only one problem.
When sorting i made it switch direction after pressing the same column more than one time.
Now it thinks i keep pressing the column title, so it keeps reversing the sorting.
But i tempererarely made it only sort in one direction.
And now im playing with the sender object in the sorting event, im thinking that if i could get some info about whats causing the event i could tell it to only switch direction based on the sender.
Thanks :)
"Both DataSource and DataSourceID are defined on 'grdCommunication'. Remove one definition."
I just got this error today, the code has been working until this afternoon I published the latest version to our server and it broke with that error both locally and on the server. I don't use "DataSourceID", the application reads database queries into a datatable and sets the datatable as the DataSource on the GridViews. I did a search in Visual Studio, searching the entire solution and the string "DataSourceID" does not appear in even 1 line of code in the entire solution. This is the first thing that freaked me out.
I figure it had been working yesterday, so I reverted the code to yesterday's build. The error was still there. I kept going back a build, and still the issue is there. I went back a month, I am still getting the same error. This application was working fine this morning? There has really been no code changes, and no where in the application is the DataSourceID EVER set on any of the gridviews. Has anyone ever seen anything like this at all??
How can I get that error if DataSourceID is never set... and the word "DataSourceID" is not in my solution? I just did a wingrep on the entire tree doing a case insensitive search on datasourceid.... pulled up absolutely nothing. That word is absolutely no where in the entire application.
<asp:GridView ID="grdCommunication" runat="server"
Height="130px" Width="100%"
AllowPaging="true" >
... standard grid view column setup here...
</asp:GridView>
// Code behind.. to set the datasource
DataSet dsActivity = objCompany.GetActivityDetails();
grdCommunication.DataSource = dsActivity;
grdCommunication.DataBind();
// Updated: removed some confusing notes.
Try this:
DataSet dsActivity = objCompany.GetActivityDetails();
grdCommunication.DataSource = dsActivity.Tables[0];
grdCommunication.DataBind();
Holy smoke batman. The Table name was changed causing my Datasource to be no good. But that error message doesn't make any sense in this situation. So technically tsilb's solution will work if I call the table by index instead of by name, so I'll mark his solution as correct.
After reading his post, I tried dsActivity.Tables["Activities"] instead of passing the dataset to the Datasource and the table name to the Datamember, and obviously that didn't work, but If I pass the actual index, which I don't like doing because that index might change, then it is now working. But the messed up part, was that error.. That error was completely off base as to what the problem was. saying that I defined both and to remove one, when in reality, that was not the case. and another really messed up thing, was the table name was only changed to be all upper case... But hey, "Activities" is a different key than "ACTIVITIES".
Replace this code before this grdCommunication.DataSource = dsActivity;
grdCommunication.DataBind();
grdCommunication.DataSourceID="";
tslib is right, don't do:
grdCommunication.DataSourceID = null;
or the string.Empty version. You only use the DataSourceID if you're using a SqlDataSource or ObjectDataSource control for your binding.
It's called "declarative" binding because you're using "declared" controls from on your page. Binding to controls does not require a call to the DataBind() method.
Because you're DataBinding manually (calling grd.DataBind()) you only set the DataSourrce and then call DataBind().
I ran into the same error, but a totally different problem and solution. In my case, I'm using LINQ to SQL to populate some dropdown lists, then caching the results for further page views. Everything would load fine with a clear cache, and then would error out on subsequent page views.
if (Cache["countries"] != null)
{
lbCountries.Items.Clear();
lbCountries.DataValueField = "Code";
lbCountries.DataTextField = "Name";
lbCountries.DataSource = (Cache["countries"]);
lbCountries.DataBind();}
else
{
var lstCountries = from Countries in db_read.Countries orderby Countries.Name select Countries;
lbCountries.Items.Clear();
lbCountries.DataValueField = "Code";
lbCountries.DataTextField = "Name";
lbCountries.DataSource = lstCountries.ToList();
lbCountries.DataBind();
Cache.Add("countries", lstCountries, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);
}
The issue came from:
Cache.Add("countries", lstCountries, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);
When it should have been:
Cache.Add("countries", lstCountries.ToList(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);
I got this error today, turns out that it had nothing to do with DataSourceID, and had everything to do with the DatasSource itself.
I had a problem in my DatasSource , and instead of getting a DatasSource related error, I got this meaningless error.
Make sure you're DatasSource is good, and this error should go away.
always bind dataset with table index to gridview...
ex. gridgrdCommunication.Table[0]; as metioned above by Tsilb
second way you intentionally write..
gridgrdCommunication.DataSourceID = String.Empty;
gridgrdCommunication.DataSource=ds;
gridgrdCommunication.DataBind();
Check you database structure.... if you are acceding your data throw a dbml file, the table structure in your database it's different of the dbml file structure
If you are using the Object Data Source and want to conditionally reload the grid in code behind you can successfully do this:
Dim datatable As DataTable = dataset.Tables(0)
Dim dataSourceID As String = gvImageFiles.DataSourceID
gvImageFiles.DataSourceID = Nothing
gvImageFiles.DataSource = datatable.DefaultView
gvImageFiles.DataBind()
gvImageFiles.DataSource = Nothing
gvImageFiles.DataSourceID = dataSourceID
You need to chose one way to bind the grid
if it is from code behind means using c# code then remove the datasourceid property from grid view from design view of grid
like this
//you have to make it like this
Please try this:
gvCustomerInvoiceList.DataSourceID = "";
gvCustomerInvoiceList.DataSource = ci_data;
gvCustomerInvoiceList.DataBind();
I got this error today. It turns out that my stored procedure did not return neither any record nor a structure. This was because I had an empty try catch without a raiserror.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Page.DataBind()
End Sub
Function GetData()
Dim dt As New DataTable
Try
dt.Columns.Add("ROOM_ID", GetType(String))
dt.Columns.Add("SCHED_ID", GetType(String))
dt.Columns.Add("TIME_START", GetType(Date))
dt.Columns.Add("TIME_END", GetType(Date))
Dim dr As DataRow = dt.NewRow
dr("ROOM_ID") = "Indocin"
dr("SCHED_ID") = "David"
dr("TIME_START") = "2018-01-03 09:00:00.000"
dr("TIME_END") = "2018-01-03 12:00:00.000"
dt.Rows.Add(dr)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Return dt
End Function
and add this to your item DataSource="<%# GetData() %>"
In my case the connection string to the database was not working. Fixing the connection string got rid of this error.