Hameed
Hameed

Reputation: 85

How to update the page without reload using update panel ?AP.NET C#

I have a gridview which contains an edit and a delete command.

I would like that when I click on edit, update the value, and click on delete that the value is deleted from database and the page is not reloaded.

Now, I use update panel and script manager but the page is reloaded again, the Update panel is not working. And my other problem is when I put <form runat="server"></form> form tag before gridview then it's working fine, the gridview shows, but when I remove this tag then an error appears:

object reference not set instance object.

My aspx code is :

<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate>
<form runat="server"></form>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="Database_id" Height="184px" 
    onrowcancelingedit="GridView1_RowCancelingEdit" 
    onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
    onrowupdating="GridView1_RowUpdating" style="margin-left: 181px" Width="361px" 
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField HeaderText="Database Name">
            <EditItemTemplate>
                <asp:TextBox ID="txtDatabaseName" runat="server" Height="22px" 
                    Text='<%# Eval("Database_Name") %>' Width="192px"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Database_Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
            <EditItemTemplate>
                <asp:TextBox ID="txtdescription" runat="server" Height="24px" 
                    Text='<%# Eval("Description") %>' Width="209px"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("Date") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField HeaderText="Operations" ShowDeleteButton="True" 
            ShowEditButton="True" />
    </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</ContentTemplate></asp:UpdatePanel>
</asp:Content>

and my aspx.cs code is :

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            binddata();
        }
    }
    private void binddata()
    {
    string con = "Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True";

     DataTable dt = new DataTable();
     using (SqlConnection cnn = new SqlConnection(con))
     {
            string user = Session["name"].ToString();
            SqlCommand cmd2 = new SqlCommand("SELECT User_ID from tbl_user WHERE User_Name='" + user + "'", cnn);
            cnn.Open();
            string id = cmd2.ExecuteScalar().ToString();
            int ID = Int32.Parse(id);
            SqlDataAdapter da = new SqlDataAdapter("SELECT Database_id,Database_Name,Description,Date FROM Create_db WHERE User_ID='" + ID + "'", cnn);
            da.Fill(dt);
            if (dt.Rows.Count > 0)
     {
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        delete(id);
        GridView1.EditIndex = -1;
   }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox txtDatabaseName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDatabaseName");
        TextBox txtdescription = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdescription");
        updatedb(txtDatabaseName.Text, txtdescription.Text, id);
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
      GridView1.EditIndex = e.NewEditIndex;
     binddata();
     }
    private void updatedb(string dbname, string Dis, int id)
    {
    string con = "Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True";
     using (SqlConnection cnn = new SqlConnection(con))
     {
     string query = "UPDATE Create_db SET Database_Name='" + dbname + "',Description='" + Dis + "' WHERE Database_id='" + id + "' ";
     SqlCommand cmd = new SqlCommand(query, cnn);
     cnn.Open();
     cmd.ExecuteNonQuery();
     }
     }
    private void delete(int id)
    {
        string con = "Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True";
        using (SqlConnection cnn = new SqlConnection(con))
        {
        string query = "DELETE FROM Create_db WHERE Database_id='" + id + "' ";     
      SqlCommand cmd = new SqlCommand(query, cnn);
       cnn.Open();
       cmd.ExecuteNonQuery();
     }
     }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

Upvotes: 0

Views: 1147

Answers (2)

Alex Kudryashev
Alex Kudryashev

Reputation: 9480

First things first:

<%-- Your code (fragment) --%>
  <EditItemTemplate>
     <asp:TextBox ID="txtDatabaseName" runat="server" Height="22px" 
         Text='<%# Eval("Database_Name") %>' Width="192px"></asp:TextBox>
  </EditItemTemplate>

Eval is ReadOnly (OneWay) binding. It should be

     <asp:TextBox ID="txtDatabaseName" runat="server" Height="22px" 
         Text='<%# Bind("Database_Name") %>' Width="192px"></asp:TextBox>

Next. I would recommend you to use <asp:SqlDataSource... which takes all dirty work to itself (select, update, and delete). You can sent your UserID parameter to the parameters like this:

<asp:Literal ID="userId" runat="server" Visible="false"></asp:Literal>
...
<asp:SqlDataSource ID="sqlDS"...
    <SelectParameters>
       <asp:ControlParameter ControlID="userId" PropertyName="Text" Name="ID" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
...

In GridView add DataSourseID="sqlDS"
In Page_load set value userId.Text=ID.ToString();

Try this way.

Upvotes: 0

Aristos
Aristos

Reputation: 66649

Your code is not well formatted, the UpdatePanel is outside the form, and gridview also is outside the form. It must be like that...

<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView>
...

 </asp:GridView>
 </ContentTemplate>
 </asp:UpdatePanel>
</form>

Upvotes: 3

Related Questions