Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Gridview VB.NET , +
I have one ASPX page and inside that one user control and that user control contains one grid to display the data, the data will load into grid view using DatasourceID and one sqldatasource control.
 
now my question is that when i add one record in that table that record is not shown in my grid, but when i refresh the page or reload the page at that time that record is displayed in grid.
 
so how to refresh the grid while we use a sqldatasource control.
 

<asp:TextBox ID="txtName" runat="server">
    
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
 
code behind is like this.
protected void btnSave_Click(object sender, EventArgs e)
{   
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString.ToString());
    SqlDataAdapter da = new SqlDataAdapter();
    con.Open();
    da.InsertCommand = new SqlCommand();
    da.InsertCommand.Connection = con;
    da.InsertCommand.CommandType = CommandType.Text;
    da.InsertCommand.CommandText = "INSERT INTO [User] ([NAME]) VALUES (@NAME)";
    da.InsertCommand.Parameters.AddWithValue("@NAME", txtName.Text);            
    da.InsertCommand.ExecuteNonQuery();
    con.Close();
}
 
i have configured my sqldata source like this.
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
        SelectCommand="SELECT * FROM [User]">
 
and my grid view is like this.
 
 <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
        
 
In short... I want to refresh my grid when i insert the record to my grid.
I am using Oracle as my database.
Posted 30-Nov-11 2:39am
Edited 30-Nov-11 2:42am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The problem is that the SqlDataSource is unaware of the changes so they do not propagate through to the grid.
 
One option is to perform the insert via the SqlDataSource[^] which will do the job with no code-behind in some [limited] circumstances.
 
A simpler method is to call GridView1.DataBind() after the insert code you have supplied.
  Permalink  
Comments
Tejas_Vaishnav at 30-Nov-11 8:12am
   
GridView1.DataBind() i tried this but its not work...
thanks for reply...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Rebind the datasource after the save method.
 
private void SaveRecord()
{ 
   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString.ToString());
    SqlDataAdapter da = new SqlDataAdapter();
    con.Open();
    da.InsertCommand = new SqlCommand();
    da.InsertCommand.Connection = con;
    da.InsertCommand.CommandType = CommandType.Text;
    da.InsertCommand.CommandText = "INSERT INTO [User] ([NAME]) VALUES (@NAME)";
    da.InsertCommand.Parameters.AddWithValue("@NAME", txtName.Text);            
    da.InsertCommand.ExecuteNonQuery();
    con.Close();
}
 
protected void btnSave_Click(object sender, EventArgs e)
{   
   SaveRecord();
   GridView1.DataSource = SqlDataSource1;
   GridView1.DataBind();
}
 
Regards,
Eduard
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

base in my own experience.
 
Call the data from the database again after your statement. that should work.
 
If you use this:
protected void btnSave_Click(object sender, EventArgs e)
{   
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString.ToString());
    SqlDataAdapter da = new SqlDataAdapter();
    con.Open();
    da.InsertCommand = new SqlCommand();
    da.InsertCommand.Connection = con;
    da.InsertCommand.CommandType = CommandType.Text;
    da.InsertCommand.CommandText = "INSERT INTO [User] ([NAME]) VALUES (@NAME)";
    da.InsertCommand.Parameters.AddWithValue("@NAME", txtName.Text);            
    da.InsertCommand.ExecuteNonQuery();
    con.Close();
}
 
Try adding Some select command after it so logically speaking if you click the button save you will call again the data and now together with data just you inserted.
 
i hope it can help you. Smile | :)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Try this one at the end of your code :
dataGridView1.Refresh();//To refresh gridview
Or another way you can use Update Panel(Ajax) for partial Page refreshing.
I hope it will help you. Smile | :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 BillWoodruff 159
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100