Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i want to insert data in gridview.as n when i insert it,it should be updated in gridview i mean to say row should be added at the end of the gridview.prblem is that data is getting inserted but not at last,what should i do,the code is given below...

aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
     AllowPaging="True" PageSize="5" 
     onpageindexchanging="GridView1_PageIndexChanging" >
        <columns>
             <asp:CommandField ShowSelectButton="True" ShowEditButton="True" />
             <asp:BoundField DataField="Id" HeaderText="Id" />
             <asp:BoundField DataField="Name" HeaderText="Name" />
             <asp:BoundField DataField="Phone_No" HeaderText="Phone_No" />
        </columns>
     
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label5" runat="server" Text="Id">
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server">
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Name">
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server">
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Phone No">
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" >
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                   </td>
            </tr>
        </table>
    </div>
    
    </div>
    </form>
</body>
</html>

code file
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
    {
        DataSet ds;
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                fillgrid();


            }
        }

        public void fillgrid()
        {
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conname1"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from Crud", conn);

            SqlDataAdapter da = new SqlDataAdapter(cmd);


            ds = new DataSet();


            da.Fill(ds, "Crud");

            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

            conn.Close();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conname1"].ConnectionString;

            conn.Open();

            cmd.Connection = conn;

            cmd.CommandText = "INSERT INTO Crud (Id,Name,Phone_No) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";

            cmd.ExecuteNonQuery();
            conn.Close();

            fillgrid();
            //DataTable dtable = new DataTable();
            //dtable.Columns.Add(new DataColumn("Id"));
            //dtable.Columns.Add(new DataColumn("Name"));
            //dtable.Columns.Add(new DataColumn("Phone_No"));
            //object[] RowValues = { "", "", "" };
            //RowValues[0] = TextBox1.Text;  //chaged here....
            //RowValues[1] = TextBox2.Text;
            //RowValues[2] = TextBox3.Text;
            //DataRow dRow;
            //dRow = dtable.Rows.Add(RowValues);
            //dtable.AcceptChanges();
            //GridView1.DataSource = dtable;//added here....
            //GridView1.DataBind();

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            fillgrid();
        }
    }
Posted
Updated 16-Oct-12 1:24am
v2

In your aspx page i am didn't found closing gridview tag add it first.
If i am not wrong ,in your code first you are binding gridview at page load at first post back.After this, on add button click there is firstly data updated to the database and again data has fetched and then bind to the gridview.When you are fetching the data i think data in the dataset is in particular order or while filling data to the gridview it insert in particular order which may be default order.To resolve it use order by clause with ascending direction on identity column in your table if you have other wise use the column which gives data in the order in which you are inserting it in the table like datetime column or column with autoincrement data n all.i hope this will helps you.
 
Share this answer
 
v2
 
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