views:

165

answers:

4

Hi all.. I have a question.

I am having problems updating data in an SQL database with Windows Forms (Visual Studio project).

Here is what I do:

  • I get user information in text boxes.
  • I edit some of them.
  • I do 1 back n get to this form again. The info is updated.
  • When I quit and login again, it shows me the old data!

It seems as if it changes the database temporarily. Do I need to set any properties or something?

I m using win application.. and i have kept a back link on it that takes me 2 the previous form. see what happens

  1. I have two buttons showUserInfo and SaveUserInfo..ok?

  2. Clicking on showUserInfo retrieves data from db and displays it on the form.

  3. then say i edit age or address of user and click on saveUserInfo button.it returns me success.

  4. I now again click on ShowUserInfo and it retrieves me the Info that i just updated.even if i go to the previos form and and then again to this form, i can c the updated info. As long as i m in the same run of the application i can c the updated info.

  5. Now when i quit the appliaction by .close() method. and run the application again, It shows me the old info, that means the db was not updated.

  6. when i open the userInfo Table, it is not updated.

my save and show buttons call Data Access Layer functions which in turn call Stored procedures. I have not used any datareaders

This is what i do:

//This the code for my form's save button as u can c....

private void btnSave_Click(object sender, EventArgs e)
{ 
  string fName = txtFirstName.Text;
  string lName = txtLastName.Text;
  string gender;
  if (rdbtnMale.Checked == true)
  {
    gender = "Male";
  }
  else
  {
    gender = "Female";
  }
  string email = txtEmail.Text;
  int age = Convert.ToInt16(txtAge.Text);
  string address = txtAddress.Text;
  int flag;
  // here i call the DAL function.....
  flag = SHSProvider.UpdateUserInfo(userName, fName, lName, gender, email, age, address);
  if (flag == 1)
  {
    MessageBox.Show("Your Account Has been changed successfully");
  }
  else
  {
    MessageBox.Show("Sorry");
  }
}

//........................
// this is the update function in my provider...

public static int UpdateUserInfo(string userName, string fName, string lName, string gender, string email, int age, string address)
{
  string strconn = ConfigurationManager.ConnectionStrings["SHSDatabaseConnectionString"].ToString();
  SqlParameter[] sqlparams = new SqlParameter[7];
  sqlparams[0] = new SqlParameter("@userName", userName);
  sqlparams[1] = new SqlParameter("@firstName", fName);
  sqlparams[2] = new SqlParameter("@lastName", lName);
  sqlparams[3] = new SqlParameter("@gender", gender);
  sqlparams[4] = new SqlParameter("@email", email);
  sqlparams[5] = new SqlParameter("@age", age);
  sqlparams[6] = new SqlParameter("@address", address);
  int flag = SqlHelper.ExecuteNonQuery(strconn, "sp_UpdateUserInfo", sqlparams);
  return flag;
}

// and finally this is my Stored procedure...

ALTER PROCEDURE dbo.sp_UpdateUserInfo 
@userName nvarchar(30), 
@fName nvarchar(30),
@lName nvarchar(30),
@gender nvarchar(10),
@email nvarchar(30),
@age int,
@address nvarchar(30)
AS
update [T_UserInfo] set [FirstName] = @fName, [LastName] = @lName, [Gender] = @gender, [email] = @email, [Age] = @age, [Address] = @address where [UserName] = @userName
RETURN

Please help!

Thank you

A: 

How are you retrieving your data? It sounds like you might not actually be committing the data to the database, but rather, keeping it locally.

If you're using DataReaders, make sure that you are not just updating the retrieved data, but rather, updating the data then committing the changes to the database.

Edit: You say you "do 1 back" it sounds as if you are using an ASP.Net application (even though you stated that it is a windows forms application). Are you possibly not clearing out the date in the textboxes and therefore it appears as if the database is updated, even though you haven't actually committed the changes to the DB?

MunkiPhD
A: 

I think Zim is using automatically generated Datasets, along with Winforms controls. To send the data back to the database, in the automatically generated form, are you clicking the "Save" button, Zim?

virtualmic
what do u mean by automatically generated Datasets?I m using stored procedures.Can u please have a look on the code i have pasted. plz help me out.
+2  A: 

Looks like you are using DataSet. Upon saving it gets saved into DataSet and but DataSet is not yet updated into DB. That's why on next application instance shows the old data.

You need to perfom ds.Update() on DataSet.

One thing to remember Computers are never wrong its human who creates bugs :)

If you can paste your code it would help more.

nils_gate
A: 

I think that you could be modifying the data in the local variables only (maybe the dataset as nils_gate pointed out) and not saving the data to the database at all. What is your code in the save function?

This may also happen if you're using tansactions to update your data and not committing the transaction.

Cyril Gupta