Click here to Skip to main content
15,923,222 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have a Data entry Form with Datagridview, I use a local database.sdf file. When i Search a particular name of a person, Click the Edit Button a new Update Form is Displayed. Data is Succesfully Passed to the Update Form

Requirement: To succesfully Update data of a Person from the update Form and the data will be Refreshed and Shown in the Datagridview When i hit the Update button and the Update form Closes

Error: I first tried SqlceDataAdapter ADAP.Fill(databaseDataSet, "Mainform"); //Rest of the code will be shown Down//But when i Click the Update Button it Updates the Data but Also Creates an Entire Copy of the Whole Data Present in the DatagridView. So i Tried SqlCeDataAdapter ADAP.Update(databaseDataSet, "Mainform"); and this Code Updates The data Succesfully and Does not Create any copies but the Datagridview does not refresh, The Data is Only Shown when I Close and Open the Application Again

Code Tried: In Form 1:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        BindDataToGrid();
        this.dataGridView1.ReadOnly = true;
        this.dataGridView2.ReadOnly = true;
        this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        loadEmployee(); // TODO: This line of code loads data into the 'databaseDataSet.Mainform' table. You can move, or remove it, as needed.

        this.mainformTableAdapter.Fill(this.databaseDataSet.Mainform); // TODO: This line of code loads data into the 'databaseDataSet.Mainform' table. You can move, or remove it, as needed.

        this.mainformTableAdapter.Fill(this.databaseDataSet.Mainform); // TODO: This line of code loads data into the 'databaseDataSet.Mainform' table. You can move, or remove it, as needed.
    }

    private void BindDataToGrid()
    {
        try
        {
            dataGridView1.DataSource = databaseDataSet.Tables[0].DefaultView;
            dataGridView2.DataSource = databaseDataSet.Tables[0].DefaultView;
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("Seems like the database file is not where it should... \n\nError details: {0}", ex.Message), "Error loading sample data", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    public void loadEmployee()
    {
        SqlCeConnection con = new SqlCeConnection("Data Source=Database.sdf");
        try
        {
            SqlCeDataAdapter ADAP = new SqlCeDataAdapter("Select Name, Age, Gender, Family_Member, Family_Member_Name, Shnong, Dong, Grand, Amount, Phone_Number, Date_Registered, Date_Recieved, Status from Mainform", con);

            ADAP.Update(databaseDataSet, "Mainform");
            this.dataGridView2.DataSource = databaseDataSet.Tables["Mainform"];
            databaseDataSet.GetChanges();
            dataGridView2.RefreshEdit();
        }
        catch (Exception)
        {
        }
    } 
}


And In Form 2 //frmUpdate:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;

namespace HDR2
{
    public partial class frmUpdate : Form
    {
        public string Name, Age, Gender, Family_Member, Family_Member_Name, Shnong, Dong, Grand, Amount, Phone_Number, Date_Registered, Date_Recieved, Status;
        private Form1 f1;

        public frmUpdate(Form1 f2)
        {
            InitializeComponent();
            f1 = f2;
        }

        public void passDgvValueToForm2()
        {
            NametxtUpdate.Text = Name;
            AgetxtUpdate.Text = Age;
            GendertxtUpdate.Text = Gender;
            FamilymembertxtUpdate.Text = Family_Member;
            FamilymembernametxtUpdate.Text = Family_Member_Name;
            ShnongtxtUpdate.Text = Shnong;
            DongtxtUpdate.Text = Dong;
            GrandtxtUpdate.Text = Grand;
            AmounttxtUpdate.Text = Amount;
            PhonenumbertxtUpdate.Text = Phone_Number;
            DateregisteredUpdate.Text = Date_Registered;
            daterecievedupdate.Text = Date_Recieved;
            StatuscbUpdate.Text = Status;
        }

        private void frmUpdate_Load(object sender, EventArgs e)
        {

        }

        private void update()
        {
            SqlCeConnection con = new SqlCeConnection("Data Source=Database.sdf");
            con.Open();
            using (SqlCeCommand cmd = new SqlCeCommand("UPDATE Mainform SET Name=@Name, Age=@Age, Gender=@Gender, Family_Member=@Family_Member, Family_Member_Name=@Family_Member_Name, Shnong=@Shnong, Dong=@Dong, Grand=@Grand, Amount=@Amount, Phone_Number=@Phone_Number, Date_Registered=@Date_Registered, Date_Recieved=@Date_Recieved, Status=@Status WHERE Name=@Name", con))
            {
                cmd.Parameters.AddWithValue("@Name", NametxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Age", AgetxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Gender", GendertxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Family_Member", FamilymembertxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Family_Member_Name", FamilymembernametxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Shnong", ShnongtxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Dong", DongtxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Grand", GrandtxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Amount", AmounttxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Phone_Number", PhonenumbertxtUpdate.Text);
                cmd.Parameters.AddWithValue("@Date_Registered", DateregisteredUpdate.Text);
                cmd.Parameters.AddWithValue("@Date_Recieved", daterecievedupdate.Text);
                cmd.Parameters.AddWithValue("@Status", StatuscbUpdate.Text);
                cmd.ExecuteNonQuery();
                con.Close();
            }
            con.Close();
        }

        private void frmUpdate_FormClosed(Object sender, FormClosedEventArgs e)
        {
            f1.loadEmployee(); 
        }

        private void btnUpdate_Click_1(object sender, EventArgs e)
        {
            update();
            f1.loadEmployee();
            this.Close();
        }
    }
}


// Note that i Already Tried the ADAP.Fill But it Does Not Work.
Posted
Updated 8-Feb-13 19:21pm
v2
Comments
Muthuraja Irullandi 10-Feb-13 5:47am    
Hi,
Where is the code for Edit button click to show the frmUpdate?
r5five 10-Feb-13 11:17am    
The code is really not necessary but i will paste it, i have no problem with the edit button to pass data, the only problem is after i update the data, it gets saved but the data in the DataGridView is not Refreshed, I have to close the whole app and open again to see the edited data

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