473,386 Members | 1,830 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,386 software developers and data experts.

Insert, else Update, Access DB

Sl1ver
196 100+
i have a Access DB table there is only one row allowed so if no row exist it shouls create one else it should just update it. What should i put in the if statement?
Expand|Select|Wrap|Line Numbers
  1. if ()
  2.             {
  3.                 int i = 1;
  4.                 string UpdateStr = "Insert into qx_Company " +
  5.                                           "(AST__ID, AST__NAME, AST__STREET, AST__TOWN, AST__CITY, AST__TEL, AST__FAX, AST__MAIL, AST__SITE, AST__POST )" +
  6.                                           "values(" +i.ToString()+ ",'"+ txtComp_Name.Text +"','"+ txtComp_Street.Text + "','"+ txtComp_Town.Text +"','"+ txtComp_City.Text +"','"+ txtComp_Tel.Text +"','"+ txtComp_Fax.Text + "','"+ txtComp_Email.Text +"','"+ txtComp_Website.Text +"',"+ txtComp_Box.Text +")";
  7.  
  8.             }else{
  9.                 string UpdateStr = "UPDATE qx_Company SET AST__NAME = '" + txtComp_Name.Text +"', " +
  10.                                " AST__STREET = '" + txtComp_Street.Text + "', " +
  11.                                " AST__TOWN = '" + txtComp_Town.Text + "', " +
  12.                                " AST__CITY = '" + txtComp_City.Text + "', " +
  13.                                " AST__TEL = '" + txtComp_Tel.Text + "', " +
  14.                                " AST__FAX = '" + txtComp_Fax.Text + "', " +
  15.                                " AST__MAIL = '" + txtComp_Email.Text + "', " +
  16.                                " AST__SITE = '" + txtComp_Website.Text +"', " +
  17.                                " AST__POST = '" + txtComp_Box.Text + "', " +
  18.  
  19.              }
  20.  
May 8 '09 #1
1 3583
balame2004
142 100+
First you get no.of records from the table and use the count in the if condition.

Eg:
Expand|Select|Wrap|Line Numbers
  1. string Query= "select count(*) from qx_Company" 
  2. SqlCommand cmd=new SqlCommand(---)
  3. cmd.CommandText(Query);
  4. int count=Convert.ToInt32(cmd.ExecuteScalar());
  5. if(count=0)
  6. {
  7.   //Your insert code
  8. }
  9. else
  10. {
  11.   //Your update code
  12. }
  13.  
May 8 '09 #2

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

Similar topics

6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
0
by: Suler Abou | last post by:
Hi, I'm having a problem with an SQL statement, I have a statement that goes like this: "INSERT INTO table VALUES('TransID','CID',etc...);" it basically adds new data to a table. When the...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
6
by: Tom Allison | last post by:
I seemed to remember being able to do this but I can't find the docs. Can I run a sql query to insert new or update existing rows in one query? Otherwise I have to run a select query to see if...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
5
by: djsdaddy | last post by:
Good Day All, I have some EEO data in an old dBase4 database that I have converted to an Access table. Since dBase was not a relational database, I didn't create any key fields. I linked all of the...
3
by: Dilruba | last post by:
asp, vbscript, Ms Access I am using vbscript to insert & update ms accees. Insert operation is working , but update operation is not working. I have used the following codes: ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.