update datagridview using ajax in my asp.net without refreshing the page.(Display real time data)

Posted by kurt_jackson19 on Stack Overflow See other posts from Stack Overflow or by kurt_jackson19
Published on 2010-03-05T03:16:11Z Indexed on 2010/03/20 11:51 UTC
Read the original article Hit count: 393

Filed under:
|
|

I need to display a real time data from MS SQL 2005. I saw some blogs that recommend Ajax to solve my problem. Basically, right now I have my default.aspx page only just for a workaround I could able to display the data from my DB. But once I add data manually to my DB there's no updating made. Any suggestions guys to fix this problem? I need to update datagridview with out refreshing the page.

Here's my code on Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillDataGridView();
    }

    protected void up1_Load(object sender, EventArgs e)
    {
        FillDataGridView();
    }

    protected void FillDataGridView()
    {
        DataSet objDs = new DataSet();
        SqlConnection myConnection = new SqlConnection  (ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString);
        SqlDataAdapter myCommand;
        string select = "SELECT * FROM Categories";
        myCommand = new SqlDataAdapter(select, myConnection);
        myCommand.SelectCommand.CommandType = CommandType.Text;
        myConnection.Open();
        myCommand.Fill(objDs);

        GridView1.DataSource = objDs;
        GridView1.DataBind();
    }
}

Code on my Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Ajax Sample</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            <Scripts>
                   <asp:ScriptReference Path="JScript.js" />      
            </Scripts>
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="up1_Load">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" Height="136px" Width="325px"/>
            </ContentTemplate>
                <Triggers> 
                    <asp:AsyncPostBackTrigger ControlID="GridView1" /> 
                </Triggers> 
        </asp:UpdatePanel>
    </form>
</body>
</html>

My problem now is how to call or use the ajax.js and how to write a code to call the FillDataGridView() in my Default.aspx.cs page.

Thank you guys, hope anyone can help me on this problem.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET