468,505 Members | 1,760 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Updating DB Data Else Insert?

Hello,

I'm a fairly new programmer and trying to get some code working, that just doesn't want to work so came for some advice. Basically I'm having this search for a specific user_id. If it exists it should update the row. If it doesn't exist it should insert a new row with the appropriate data.

It appears to work for creating new rows. However, it never updates even if the row is there. I tested the update lines of code by itself and they work. So I'm doing something incorrectly. Take a look at the code below:

Expand|Select|Wrap|Line Numbers
  1. OleDbCommand cmd = new OleDbCommand("SELECT * FROM users WHERE user_id=?");
  2.                 cmd.Connection = con;
  3.                 cmd.CommandType = CommandType.Text;
  4.                 cmd.Parameters.AddWithValue("user_id", userIDHiddenField.Value.ToString());
  5.                 using (OleDbDataReader Reader = cmd.ExecuteReader())
  6.                 {
  7.                     if (Reader.HasRows)
  8.                     {
  9.                         Reader.Close();
  10.                         cmd.CommandText = @"UPDATE users SET user_first_name = @user_first_name, " +
  11.                                           "user_last_name = @user_last_name, user_email = @user_email " +
  12.                                           "WHERE user_id = @user_id";
  13.                         cmd.Connection = con;
  14.                         cmd.CommandType = CommandType.Text;
  15.                         cmd.Parameters.AddWithValue("@user_first_name", userFirstNameTextBox.Text);
  16.                         cmd.Parameters.AddWithValue("@user_last_name", userLastNameTextBox.Text);
  17.                         cmd.Parameters.AddWithValue("@user_email", userEMailTextBox.Text);
  18.                         cmd.Parameters.AddWithValue("@user_id", userIDHiddenField.Value.ToString());
  19.                         cmd.ExecuteNonQuery();
  20.                     }
  21.                     else
  22.                     {
  23.                         Reader.Close();
  24.                         cmd.CommandText = @"INSERT INTO users (user_id, user_first_name, user_last_name, user_email) " +
  25.                                           "VALUES (@user_id, @user_first_name, @user_last_name, @user_email)";
  26.                         cmd.Connection = con;
  27.                         cmd.CommandType = CommandType.Text;
  28.                         cmd.Parameters.AddWithValue("@user_first_name", userFirstNameTextBox.Text);
  29.                         cmd.Parameters.AddWithValue("@user_last_name", userLastNameTextBox.Text);
  30.                         cmd.Parameters.AddWithValue("@user_email", userEMailTextBox.Text);
  31.                         cmd.Parameters.AddWithValue("@user_id", userIDHiddenField.Value.ToString());
  32.                         cmd.ExecuteNonQuery();
  33.                     }
  34.                 }
Any help would be much appreciated.
Jan 25 '10 #1
0 926

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Paige | last post: by
2 posts views Thread by Irvin | last post: by
4 posts views Thread by Darrel | last post: by
4 posts views Thread by Geoff | last post: by
3 posts views Thread by RSH | last post: by
5 posts views Thread by JimmyKoolPantz | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.