Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have create a table "StudentsInformation" in ms access 2007 database.I successfully established the connection also with WPF and ms access 2007.
I am able to add new records in to the table and i am able to view the records also .

The problem is i am unable to update one row.

If i make one change in one field in one row ,it gets reflected in all the rows.

For ex:

if update last name for a particular record ,the last name gets updated in all records.

Please help.

Please find the below mentioned code

My class name is Students.cs

public void Update()
{
OleDbConnection con = new OleDbConnection ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\SR0150\\Documents\\Students.accdb");
con.Open();
OleDbCommand cmd = new OleDbCommand("Update StudentInformation set Fname=@Fname,Lname=@Lname,Clgname=@Clgname,Mark=@Mark Where RollNo=@RollNo",con);
//assign the values and add parameters
cmd.Parameters.AddWithValue("@Fname", _FirstName);
cmd.Parameters.AddWithValue("@Lname", _LastName);
cmd.Parameters.AddWithValue("@Clgname", _CollegeName);
cmd.Parameters.AddWithValue("@Mark", _MarksObtained);
cmd.Parameters.AddWithValue("@Exam", _Exam);
cmd.ExecuteNonQuery();
}

My MainWindow.xaml.cs code

private void btnUpDate_Click(object sender, RoutedEventArgs e)
{
stud.RollNo = Convert.ToInt32(txtRollNo.Text);
stud.FirstName = txtFirstName.Text;
stud.LastName = txtLastName.Text;
stud.CollegeName = txtClgName.Text;
stud.MarksObtained = txtMarks.Text;
stud.Exam = txtExamResults.Text;
stud.Update();//call object for update record
MessageBox.Show("Update Record", "Update");
}

Thanks
Sophia
Posted

Hi sophia,
In update statement add the @RollNo parameter in your query.
 
Share this answer
 
Comments
Sophia Ranjani Elango 13-Dec-12 8:47am    
yeah got it rite thanks a lot
Check your variables _FirstName, _LastName, _CollegeName etc are updated with recent values and RollNo field is not added to the parameters list

C#
cmd.Parameters.AddWithValue("@Fname", _FirstName);
cmd.Parameters.AddWithValue("@Lname", _LastName);
cmd.Parameters.AddWithValue("@Clgname", _CollegeName);
cmd.Parameters.AddWithValue("@Mark", _MarksObtained);
cmd.Parameters.AddWithValue("@Exam", _Exam);
cmd.Parameters.AddWithValue("@RollNo;, ?????); // Modify this line


what is stud variable doing here? and you are not using stud var value to update statement. check that too.
 
Share this answer
 
Comments
Sophia Ranjani Elango 13-Dec-12 8:48am    
thank you got it rite thank you

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