0 Replies - 269 Views - Last Post: 05 February 2013 - 06:33 AM Rate Topic: -----

#1 r5five  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-February 13

Data entry form datagridview and update form error

Posted 05 February 2013 - 06:33 AM

hi, i have a data entry form with a local .sdf
database file attached and there are tab
pages, on tab1 is the data entry form and
tab2 is the search with datagrid, there are
two additional buttons, edit and delete, 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
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

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(); } } }


This post has been edited by Skydiver: 05 February 2013 - 08:51 AM
Reason for edit:: Put code into code tags. Please do this yourself in the future. Of course it won't help much right now because you decided to word wrap your text and your code.


Is This A Good Question/Topic? 0
  • +

Page 1 of 1