473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1025

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

Similar topics

7
by: Paige | last post by:
I have a database on my local machine that I make entries and corrections on. I'd like to be able to upload that to my server and have that update the database that's on the server. What I've been...
6
by: Hennie de Nooijer | last post by:
Hi, Currently we're a building a metadatadriven datawarehouse in SQL Server 2000. We're investigating the possibility of the updating tables with enormeous number of updates and insert and the...
3
by: Robin Tucker | last post by:
Hi there, I have a database on my test machine that will need to be installed on users machines. I would like to create the database with the given schema on the users machine and also with...
2
by: Irvin | last post by:
I new to ASP.net and am using the following code to attempt to update an Access 2000 mdb. The code does make it through the code following "try". NO rows are updated. There is a row with the...
4
by: Darrel | last post by:
I'm creating a table that contains multiple records pulled out of the database. I'm building the table myself and passing it to the page since the table needs to be fairly customized (ie, a...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
4
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following problem that has occurred to me. Suppose I have two tables in an SQL Server database. Let's call these tables A and B. Assume that A has...
3
by: RSH | last post by:
Hi, I have a situation in where i have two instances of SQL server, the first is our Production Environment, the second is our Development environment. Both servers contain the same databases...
5
by: JimmyKoolPantz | last post by:
Situation: I am writing a program that opens up a data file (file ext .dbf), imports the information into a dataset. The program also, searches through the dataset for key words ("company...
12
lotus18
by: lotus18 | last post by:
Hi everyone I have a little problem in updating a record. I know how add a new record and check if the inputted new data is existing or not as what they have discussed in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.