2014年7月13日 星期日

[RESOLVED] Gridview data view


in my web page there is text boxes to insert data to database and a gridview below that to view the insertde data. the data gets inserted correctly but the data is not immediately viewed in the gridview it needs refresh page or reload how can it be rectified



Make sure you are calling GridView.DataBind() after you insert the data and update the GridView.DataSource.



On the same event you have for inserting data you need to rebind your grid.


So if you have


cmdSave_Click{


 // code o add data



gvGridviewName.DataSource = dsName;

gvGridviewName.DataBind(); 


}



Make sure you're binding the grid at the right time/event. 



i am using sqldatasource with select command in .aspx page and wrote the code like this after  .vb code page 


 GridView1.DataBind()


still that last entry is not viewed in the grid the second last entry is shown


when enter one more row then the previous data is viewed



Try calling SqlDataSource1.DataBind() before GridView1.DataBind().  Also make sure  EnableCaching = "False" on the SqlDataSource control.



Call the BindGridView() function as the last statement in the  save button click.


For reference check the article Bind,Save,Edit,Update,Cancel,Delete,Paging
example in GridView in asp.net C#


if you are using VB language then read How
to Bind,Insert,Edit,Update,Delete in GridView in asp.net VB.Net





not yet solved


i am using a code behind to insert new data to gridview


and the databinding is done by sqldatasource update,edit code





Baiju EP



not yet solved


i am using a code behind to insert new data to gridview


and the databinding is done by sqldatasource update,edit code





Could you post the code behind here where you insert the new data and databind, so we can see the actual code calls?



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New OleDbConnection
con.ConnectionString = ConfigurationManager.ConnectionStrings("baijuepConnectionString").ConnectionString
con.Open()
Dim oledb_ins As String
oledb_ins = "insert into Tasks (TaskDescription, startdate, enddate, Notes) values (@a,@b,@c,@d)"
Dim oledbcom As New OleDbCommand(oledb_ins, con)
oledbcom.Parameters.Add(New OleDbParameter("@a", OleDbType.VarChar, 50)).Value = tasktxt.Text
oledbcom.Parameters.Add(New OleDbParameter("@b", OleDbType.VarChar, 50)).Value = startdt.Text
oledbcom.Parameters.Add(New OleDbParameter("@c", OleDbType.VarChar, 50)).Value = enddt.Text
oledbcom.Parameters.Add(New OleDbParameter("@d", OleDbType.VarChar, 50)).Value = notestxt.Text
oledbcom.ExecuteNonQuery()
tasktxt.Text = ""
startdt.Text = ""
enddt.Text = ""
notestxt.Text = ""
SqlDataSource1.DataBind()
GridView1.DataBind()
Page.ClientScript.RegisterStartupScript(Me.GetType, "Forms", "")

ens usb

sqldatasource


                    ConnectionString="<%$ ConnectionStrings:baijuepConnectionString %>" 

SelectCommand="SELECT * FROM [Taskss]"
DeleteCommand="delete [Tasks] where id=@id"

UpdateCommand="update [Tasks] set TaskDescription=@TaskDescription,StartDate=StartDate,EndDate=@EndDate,Notes=@Notes where id=@id"
ProviderName="<%$ ConnectionStrings:baijuepConnectionString.ProviderName %>">













the data get inserted into the database but not displying in the gridview at once


it gets viewed when one more entry is made how can it be possible


and with the edit/delete in grid it is also not working





i was not able to solve the problem



I noticed in your SqlDataSource you have


 SelectCommand="SELECT * FROM [Taskss]" 

it should be


SelectCommand="SELECT * FROM [Tasks]"

Other than that it should work. If that was just a copy paste typo into this forum and it is [Tasks] in your actual code already then maybe it's some kind of screen refresh issue. Are you using an Ajax UpdatePanel or something?  After you click the button
and the record is not there does it show up if you hit ctrl-F5?


For Gridview edit make sure you have DataKeyNames="id" in your GridView.




I  had corrected it still it is not displaying when the new record is added . when one more record is added then the previous recod gets displayed





Baiju EP



I  had corrected it still it is not displaying when the new record is added . when one more record is added then the previous recod gets displayed





When you add a new record and the previous record gets displayed, does the new record show if you press ctrl+F5 in your browser?



yes when i add a record and press refresh the dtata gets displayed



In your markup are you using Ajax or an UpdatePanel?  If so make sure your insert Button and GridView are in the same UpdatePanel or Ajax call.



i am using accessdatasource to all actions. select, insert,update,delete code all are in accessdatasource



Hi Baiju EP,


Based on your description I have created a sample,  I find it can work fine, I also find a mistake in your code:

the “SelectCommand="SELECT * FROM [Taskss]"”  T-SQL statement which select data from “Taskss”, but in behind code, you have insert data into “Tasks”, so please check you table name again.


The following is my sample code, please try to refer to it.


In the .aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridViewDataView.WebForm1" %>






















ConnectionString="<%$ ConnectionStrings:DemoDBConnectionString %>"

SelectCommand="SELECT [Id], [name], [age], [sex] FROM [Student]"

UpdateCommand="Update Student set name = 'Mr. OK'"

DeleteCommand="Delete Student">





























In the .aspx.cs:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Configuration;

using System.Data.SqlClient;



namespace GridViewDataView

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}



protected void Insert_Click(object sender, EventArgs e)

{

string connStr = ConfigurationManager.ConnectionStrings["DemoDBConnectionString"].ToString();

SqlCommand cmd = new SqlCommand("insert into Student values(@Id,@name,@age,@sex)", new SqlConnection(connStr));

SqlParameter[] parameters = new SqlParameter[] {

new SqlParameter("@Id",5),

new SqlParameter("@name","Andy"),

new SqlParameter("@age",24),

new SqlParameter("@sex","male")

};

foreach (SqlParameter p in parameters)

{

cmd.Parameters.Add(p);

}

if (cmd.Connection.State == System.Data.ConnectionState.Closed)

{

cmd.Connection.Open();

}

if (cmd.ExecuteNonQuery() > 0)

{

SqlDataSource2.DataBind();

GridView1.DataBind();

Page.ClientScript.RegisterStartupScript(this.GetType(), "Forms", "");

}

if (cmd.Connection.State == System.Data.ConnectionState.Open)

{

cmd.Connection.Close();

}

}

}

}

Best Regards,

Terry Guo


沒有留言:

張貼留言