Hi,
I have a data entry form with a local
.sdf
database file attached and there are tab pages.
The
tab1
is the data entry form and
tab2
is the search with datagrid.
There are two additional buttons, edit and delete.
Requirement
When I search the name of a person, I must be able to click the edit button and all the data will be filled to the textbox in a new form, in which I can update any detail.
Problem
I am able to enter data and save it and show it on data grid view.
I am also able to search, but
the problem is the edit and update part.
When i search a name and click the edit button, nothing appears on the edit form and also facing problem with deleting an entire row. It does not get saved permanently.
Code tried
I tried This Code But it does not work, the data gets filled into the text box in frmUpdate, but when i click update button it does not update but instead creates a copy of all the rows...??!!
This is the code in
Form1
, which contains the
datagridview
.
private void Form1_Load(object sender, EventArgs e) { loadEmployee();
this.mainformTableAdapter.Fill(this.databaseDataSet.Mainfo rm);
this.mainformTableAdapter.Fill(this.databaseDataSet.Mainfo rm);
this.mainformTableAdapter.Fill(this.databaseDataSet.Mainfo rm); }
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.Fill(databaseDataSet, "Mainform"); this.dataGridView2.DataSource = databaseDataSet.Tables["Mainform"];
} catch (Exception) { }
}
private void btoedit_Click(object sender, EventArgs e) { frmUpdate f2 = new frmUpdate(this); f2.Name = dataGridView2.CurrentRow.Cells[0].Value.ToString(); f2.Age = dataGridView2.CurrentRow.Cells[1].Value.ToString(); f2.Gender = dataGridView2.CurrentRow.Cells[2].Value.ToString(); f2.Family_Member = dataGridView2.CurrentRow.Cells[3].Value.ToString(); f2.Family_Member_Name = dataGridView2.CurrentRow.Cells[4].Value.ToString(); f2.Shnong = dataGridView2.CurrentRow.Cells[5].Value.ToString(); f2.Dong = dataGridView2.CurrentRow.Cells[6].Value.ToString(); f2.Grand = dataGridView2.CurrentRow.Cells[7].Value.ToString(); f2.Amount = dataGridView2.CurrentRow.Cells[8].Value.ToString(); f2.Phone_Number = dataGridView2.CurrentRow.Cells[9].Value.ToString(); f2.Date_Registered = dataGridView2.CurrentRow.Cells[10].Value.ToString(); f2.Date_Recieved = dataGridView2.CurrentRow.Cells[11].Value.ToString(); f2.Status = dataGridView2.CurrentRow.Cells[12].Value.ToString(); f2.passDgvValueToForm2(); f2.ShowDialog(); }
And this is the whole code in frmUpdate:
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 btnUpdate_Click(object sender, EventArgs e) { update(); f1.loadEmployee(); }
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) {
[Edit] by
@Tadit
1. Formatted and organised text.
2. Added code blocks.
3. Added proper headings for easy reading.
[/Edit]