How to refresh gridview after every insert command automatically? - asp.net

I am inserting data to database when i click submit button data inserted into Database but no changes get reflected in GridView without refreshing the page.
I used DataBind() also.

it is enough to add
YourGridView.DataBind();
in your button onclick event.... no need to bind it also in Page_Load
Do you have any updatepanels?

Just use the Procedure which populates the GridView and then call that Procedure OnClick Event.
Hope it Helps

You can use this code - based on DataBind two times
Nota : you add this DataBind no just in your Page_Load but also in your delegate of click
//Your Insert in your delegate
....
YourGridView.DataBind();

You have to refresh the gridviews data source through whatever means your using, for instance an sql query and then set the datasource to this and then use Gridview1.DataBind();
public void GetData()
{
try
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
String sql = "Your Query";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
GridView1.DataSource = dt;
}
catch (SqlException ex)
{
}
catch (Exception e)
{
}
}
Then just use GetData() before you bind the gridview (possibly in your page load event).

You can also do in your rowCommand:
gridView_RowCommand(object sender, GridViewCommandEventArgs e){
gridView.EditIndex = -1; // to cancel edit mode
}

Related

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

how to reload repeater on button click in asp.net

I have a repeater on my page being populated on page load
con.Open();
SqlDataReader postView = cmd.ExecuteReader();
topicView.DataSource = postView;
topicView.DataBind();
And then I have a section to add a post. What I want it to do is when the user presses add, it reloads the page and displays the updated repeater. However it's not doing this
Everyone has said just reload the databind, except that isn't working as the original databind is in the page load, and the new databind is in a protected void button click.
Can anyone tell me how to do this, or do I just need to place ALL the connection, datasource ad databind info in the button click code?
you can create a function that can be call everytime Button click event fire
Private void BindRepeater()
{
con.Open();
SqlDataReader postView = cmd.ExecuteReader();
topicView.DataSource = postView;
topicView.DataBind();
}
Protected Button_Click(object as sender , EventArgs e)
{
//call the function to populate the repeater with the updated records
this.BindRepeater();
}
you can resolve it like this . create a method
private void Bind()
{
con.Open();
SqlDataReader postView = cmd.ExecuteReader();
topicView.DataSource = postView;
topicView.DataBind();
}
now you can use it anywhere on the page where you need to reload the repeater.

Fields not changing in Formview when moving Pagination

I have a formview that on page load makes a call to a sql server and retrieves 5 records which I want the formview to paginate though.
I have sucessfully connected to the db, filled a dataset, and returned data when the web page renders. The problem is that when I move to a new page, the data does not change in the databound field.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
conn = new SqlConnection(connstr);
ds = new DataSet();
da = new SqlDataAdapter("call to stored proc", conn);
try
{
conn.Open();
da.Fill(ds, "m");
FormView1.DataSource = ds;
FormView1.DataKeyNames = new string[] { "PropKey" };
FormView1.DataBind();
}
catch (Exception ex)
{
Result = ex.Message;
}
finally
{
conn.Close();
}
}
Next when the paginate buttons are clicked I have this:
protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
}
Please help,
Thanks
You will need to bind the data to the FormView just after setting the new page index like below.
protected void FormView1_PageIndexChanging1(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
BindFormViewData();
}
This is required because the FormView only displays the data for the active record and does not store any other records from the datasource, so upon change of the page index, the datasource is required to be bound again. See: Paging in a FormView Web Server Control
From the above link:
If the FormView control is bound to a data source control, or to any
data structure that implements the ICollection interface (including
datasets), the control gets all the records from the data source,
displays the record for the current page, and discards the rest. When
the user moves to another page, the FormView control repeats the
process, displaying a different record.
Hope this helps.

Dropdownlist on gridview updating only first value to the database ASP.Net

I am looking for the solution to this problem from many days, please help.
I have a grid view and have five dropdownlist on it. I have edit update and cancel edit buttons on each row.
Drop down list is updating the database, but only first value in the dropdownlist even when user select second, third or any other value from the dropdownlist.
What could possible be the reason for this. Here is my row Edit/ CancelEdit and Updating Events.
protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
BindData();
}
protected void gvCustomers_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvCustomers.EditIndex = -1;
BindData();
}
protected void gvCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblCustId = (Label)gvCustomers.Rows[e.RowIndex].FindControl("cust_id");
DropDownList ddlCsm = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("CSM");
DropDownList ddlSrCsm = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("SrCSM");
DropDownList ddlDE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("DE");
DropDownList ddlAE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("AE");
DropDownList ddlTE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("TE");
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();
string sql = "Update SOME_TABLE SET CSM= #CSM, SrCSM= #SrCSM ,DE=#DE,AE=#AE,TE=#TE where cust_id=#cust_id";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add("#CSM", SqlDbType.VarChar).Value = ddlCsm.SelectedItem.Value;
cmd.Parameters.Add("#SrCSM", SqlDbType.VarChar).Value = ddlSrCsm.SelectedItem.Value;
cmd.Parameters.Add("#AE", SqlDbType.VarChar).Value = ddlAE.SelectedItem.Value;
cmd.Parameters.Add("#DE", SqlDbType.VarChar).Value = ddlDE.SelectedItem.Value;
cmd.Parameters.Add("#TE", SqlDbType.VarChar).Value = ddlTE.SelectedItem.Value;
cmd.ExecuteNonQuery();
}
}
catch
{ }
gvCustomers.EditIndex = -1;
BindData();
}
}
Any help would be greatly appeciated.
Thanks
just fill the dropdowns on the click of edit button and then use on rowupdating event and use ddl.SelectedValue instead of ddl.SelectedItem.Value. You will find the selected values.
Your Drop Downs are probably getting re-set in your Page_Load event. If they are, load them when the page is not in postback, like so:
if(!Page.IsPostBack) {
//set drop downs
}

DataGrid Paging

I am using VB.Net 2005, with IE7.
I have a datagrid where I have set paging "AllowPaging" = True
However, when I click the link to the next page, the records are still the same.
My code is:
ds = SQLHelper.ExecuteDataset(strConn,
CommandType.StoredProcedure, "GetInventory")
dv = ds.Tables(0).DefaultView
dgInvestoryList.DataSource = dv
dgInvestoryList.DataBind()
What am I missing?
If you are using the Wizard with the SqlDataSource, then paying will be there all ready.
But if you go and place your code in the code behind you will have to do something like this - sorry i dont have the code for VB.NET - Must place code in the PageIndexChanging event. Use this This link to change my C# code to VB.NET, i use it ALOT
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
DataSet ds = new DataSet();
GridView1.DataSource = ds;
GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataBind();
}
catch (Exception)
{
Response.Redirect("Login.aspx");
}
}

Resources