سؤال

Whene i try to add a record to the dataset the previous one crush,i have a form and I add data to a dataset after every click on the button1 and every time i find my last record only here is my code


    public partial class _Default : System.Web.UI.Page 
    {

        private DataTable dt=new DataTable("manager");
        private DataSet data_set=new DataSet("Test");
        private static int _stagiaireid = 0;
        public static int StagiaireID { get { return _stagiaireid++; } }

        protected void Page_Load(object sender, EventArgs e)
        {

            //dt = new DataTable("manager");

            dt.Columns.Add("id ", typeof(int));
            dt.Columns.Add("electro ", typeof(string));
            dt.Columns.Add("sn", typeof(string));
            dt.Columns.Add("date ", typeof(string));
            dt.Columns.Add("email", typeof(string));
            dt.Columns.Add("marque ", typeof(string));
            DropDownList1.Items.Add(new ListItem("Siera", "Siera"));
            DropDownList1.Items.Add(new ListItem("AB","AB"));
            DropDownList1.Items.Add(new ListItem("CD","CD"));
            data_set.Tables.Add(dt);
           GridView1.DataSource = data_set.Tables["manager"];


        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            String electro = TextBox1.Text;
            String sn = TextBox2.Text;
            String date = TextBox3.Text;
            String email = TextBox4.Text;
            String marque = DropDownList1.SelectedValue.ToString() ;
            if (aide.nom_pre(email)==true)
            {
                Label1.Text = "Email Juste";
            }
            else
            {
                Label1.Text = "Email Faux";
                return;
            }
            data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);
           // dt.Rows.Add(StagiaireID,electro, sn, date, email, marque);
            GridView1.DataBind();
     }
هل كانت مفيدة؟

المحلول

Of course you will get only the last record because you are creating and populating the DataSet and the DataTable in every postback and you didn't store the DataSet in a session state also you didn't bind the GridView change your code to:

    private DataTable dt = new DataTable("manager");
    private DataSet data_set = new DataSet("Test");
    private static int _stagiaireid = 0;
    public static int StagiaireID { get { return _stagiaireid++; } }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt.Columns.Add("id ", typeof(int));
            dt.Columns.Add("electro ", typeof(string));
            dt.Columns.Add("sn", typeof(string));
            dt.Columns.Add("date ", typeof(string));
            dt.Columns.Add("email", typeof(string));
            dt.Columns.Add("marque ", typeof(string));
            data_set.Tables.Add(dt);
            GridView1.DataSource = data_set.Tables["manager"];

            this.ViewState["ds"] = data_set;
            GridView1.DataBind();
        }
        else
        {
            data_set = (DataSet)ViewState["ds"];
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        String electro = TextBox1.Text;
        String sn = TextBox2.Text;
        String date = TextBox3.Text;
        String email = TextBox4.Text;
        String marque = DropDownList1.SelectedValue.ToString();
        if (aide.nom_pre(email) == true)
        {
            Label1.Text = "Email Juste";
        }
        else
        {
            Label1.Text = "Email Faux";
            return;
        }

        data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);       
        GridView1.DataSource = data_set.Tables["manager"];
        GridView1.DataBind();
        this.ViewState["ds"] = data_set;
    }

It's good to understand ASP.NET Page Life Cycle and this tutorial

نصائح أخرى

The problem is you are not persisting the dataset across the post backs . so keep your dataset in session . refer about state management mechanism in ASP.NET

Before binding data table to grid view, call the AcceptChanges() method on the data table in which you have added the rows, then again assign the DataSource to grid view with this table.

data_set.Tables["manager"].Rows.Add(StagiaireID, electro, sn, date, email, marque);
data_set.Tables["manager"].AcceptChanges();
GridView1.DataSource = data_set.Tables["manager"];
GridView1.DataBind();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top