1

I have written the following function meant to update a student in my database:

public bool UpdateStudent(Student stu)
{
   Console.WriteLine("StudentManger.UpdateStudent): Called");

   int rowsChanged = 0;

   using (var cnn = new SqlConnection(
          Properties.Settings.Default.universityConnectionString))
   {
      using (var cmd = new SqlCommand("UPDATE Student " +
              "SET FirstName = @FirstName, " +
              "lastName = @LastName, " +
              "birth = @birth " +
              "WHERE id = @id", cnn))
      {
        cmd.Parameters.Add(new SqlParameter("@FirstName", stu.FirstName));
        cmd.Parameters.Add(new SqlParameter("@LastName", stu.LastName));
        cmd.Parameters.Add(new SqlParameter("@id", stu.ID));
        cmd.Parameters.Add(new SqlParameter("@birth", stu.Birth));

        cnn.Open();
        rowsChanged = (int)cmd.ExecuteNonQuery();
        Properties.Settings.Default.Save();
      }
    }
    Properties.Settings.Default.Save();
    return (rowsChanged != 0);
  }

But when I call function no data is actually getting saved to the Database

Can someone tell me why?

dplante
  • 2,445
  • 3
  • 21
  • 27
AYKHO
  • 516
  • 1
  • 7
  • 20
  • You're leaving out a lot of helpful information for people to be able to help you. Can you provide more details? – Jim D'Angelo May 24 '12 at 01:18
  • 2
    I don't think it's a bad question. Just might be sooo many different things going wrong with your app that it'll be hard to troubleshoot IMO. – frenchie May 24 '12 at 01:25
  • 1
    (I agree with @frenchie.) Is an exception being thrown? Are you connecting to the correct database? Is it loading the correct connection string? What have you tried? – Jim D'Angelo May 24 '12 at 01:26
  • 1
    Why are you typecasting an int to an int? You also forgot to close the connection. I'm with frenchie on this one. – mawburn May 24 '12 at 01:26
  • 1
    I think you should try your connection first with a simple read query. What happens when you load everything from the table? If that works, then at least we'll know it's not a connection problem. – frenchie May 24 '12 at 01:29
  • I can Getdata by using the same connection and show it to GridView in Form and when I call updateStudent The data in GrideView well Changed But doesn't save to DataBase – AYKHO May 24 '12 at 01:29
  • 2
    Is this wrapped in a `try...catch` that is swallowing exceptions or something? Can you step through it and see that nothing blows up? Does the account under which the update is being run have permissions to update the db? – Jim D'Angelo May 24 '12 at 01:30
  • what database engine are you using? sometimes when you use sql express for example it gets copied into bin and gets updated when you execute your app. while you are looking at your project's database file. Also, have you got any transaction scope overt this function call. It might have got rolled back at some stage. – Ahmadreza May 24 '12 at 01:31
  • How I can explain ?? No save to Physical file in dataBase just in Propertis........ – AYKHO May 24 '12 at 01:33
  • We get that it's not updating the database, we're trying to figure out *why* it's not updating the database. Can you try stepping through the code and reporting back what happens? – Jim D'Angelo May 24 '12 at 01:34
  • Is there any another Code to Connect to dataBase without using Properties. – AYKHO May 24 '12 at 01:38
  • So, finally rowsChanged is equals to 0? – Ivo May 24 '12 at 01:39
  • That means the update happened. Are you reading and writing with the same database? Can you profile the database and see the transactions going through? Are you saving the old data and then updating the in-memory object? – Jim D'Angelo May 24 '12 at 01:43
  • yes i can update it in memory object and insert and retrive it but doesn't save to database – AYKHO May 24 '12 at 01:48
  • I'm asking about the order of operations, not *if* you can do it. If you're updating the database with old values, it will appear to not update. Can you step through and make sure the right values are making it to your method? – Jim D'Angelo May 24 '12 at 01:50
  • I'm not sure what you mean by your comment. If the values are correct when you step through, can you explain what makes you think your updates aren't going through? What have you tried? – Jim D'Angelo May 24 '12 at 02:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11666/discussion-between-james-dangelo-and-aahn) – Jim D'Angelo May 24 '12 at 02:08

1 Answers1

3

With the entire solution and information you provided in chat, your code is fine. The problem is that the .mdf database file is set to "Copy to Output Directory": "Always" in your project. Change this property to "Copy if newer" (or "Do not copy" and move it to the bin folder yourself) and it will not overwrite your changes when you re-run the application. Importantly, you will not see the changes you make in your application reflected in your .mdf database file in the project's root directory. It actually gets copied to the /bin folder and that's where the changes are persisted. So, if you don't change the "Copy to Output Directory" property, it will copy from your root to your /bin folder every time you build. Thus it appears that the changes aren't being persisted, when they actually are.

Community
  • 1
  • 1
Jim D'Angelo
  • 3,952
  • 3
  • 25
  • 39