Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.


C#
<asp:TextBox ID="txtName" runat="server">
    
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />


code behind is like this.
C#
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.

C#
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
        SelectCommand="SELECT * FROM [User]">


and my grid view is like this.

C#
<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
Updated 30-Nov-11 1:42am
v2

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.
 
Share this answer
 
Comments
Tejas Vaishnav 30-Nov-11 8:12am    
GridView1.DataBind() i tried this but its not work...
thanks for reply...
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
 
Share this answer
 
v3
base in my own experience.

Call the data from the database again after your statement. that should work.

If you use this:
C#
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. :)
 
Share this answer
 
v2
Try this one at the end of your code :
C#
dataGridView1.Refresh();//To refresh gridview

Or another way you can use Update Panel(Ajax) for partial Page refreshing.
I hope it will help you. :)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900