Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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(); // 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.Mainfo rm); // 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.Mainfo rm); // 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.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) { //f1.loadEmployee(); } } }
 
[Edit] by @Tadit
1. Formatted and organised text.
2. Added code blocks.
3. Added proper headings for easy reading.
[/Edit]
Posted 4-Feb-13 20:58pm
r5five392
Edited 5-Feb-13 4:03am
(no name)118.3K
v2
Comments
jibesh at 5-Feb-13 2:10am
   
have you wrote enough code to fetch the data and display it on edit form??
r5five at 5-Feb-13 3:17am
   
Thats the problem, ive tried using some codes i found from the net but none of them work so the codings now are blank
jibesh at 5-Feb-13 3:26am
   
can you share the code you did to display on the EditForm. how you are passing the griddata to edit form? how many control on the edit form?
CHill60 at 6-Feb-13 9:20am
   
If you step through this in debug mode up to the point where you are about to run cmd.ExecuteNonQuery what are the contents of cmd.CommandText?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 398
1 DamithSL 255
2 OriginalGriff 230
3 Zoltán Zörgő 149
4 Dave Kreskowiak 120
0 OriginalGriff 7,575
1 DamithSL 5,529
2 Sergey Alexandrovich Kryukov 5,279
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100