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
I have two buttons showUserInfo and SaveUserInfo..ok?
Clicking on showUserInfo retrieves data from db and displays it on the form.
then say i edit age or address of user and click on saveUserInfo button.it returns me success.
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.
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.
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