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

Updating a table with an auto-increment key

I'm using the following code to add a row to a table (for holding
images) in my database (obtained from
http://www.codeproject.com/aspnet/im...lect=1038401):

public string AddImage(byte[] buffer, string contentType)
{
string strSql = "SELECT * FROM WebImages";
DataSet ds = new DataSet("Image");
OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql, this._objConn);
OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
tempAP.Fill(ds, "WebImages");
try
{
this._objConn.Open();
DataRow objNewRow = ds.Tables["WebImages"].NewRow();
objNewRow["ContentType"] = contentType;
objNewRow["Data"] = buffer;
ds.Tables["WebImages"].Rows.Add(objNewRow);
tempAP.Update(ds, "Table");
}
catch(Exception e){return e.Message;}
finally{this._objConn.Close();}
return null;
}

Now, the problem is that this table also contains a numeric primary key
field set to auto-increment. Whenever I use a regular INSERT statement,
the field does its job and automatically enters the next value.
However, the above method does not. I can just as easily add a field in
the above code, but what value would I assign it? I tried just
assigning it "null" but that didn't work, nor did an empty string. I
could grab the current highest value from the table, add one, and use
that. But wouldn't that risk a race condition if two users are
performing the operation simultaneously?

What do you recommend to solve this? Any help would be much
appreciated. Thank you.
Regards,
David P. Donahue
dd******@ccs.neu.edu
Jul 21 '05 #1
2 2175
Sure: change the data type of the primary key from an INT to a GUID. In the
code below, create your own Guid value ( see Guid.NewGuid() ), and place the
value into the table.

That pretty much solves it. I'm sure that there may be ways to do this with
INT as well, but honestly, once you are spoiled with GUIDS, it's kinda hard
to go back :-).

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
I'm using the following code to add a row to a table (for holding images)
in my database (obtained from
http://www.codeproject.com/aspnet/im...lect=1038401):

public string AddImage(byte[] buffer, string contentType)
{
string strSql = "SELECT * FROM WebImages";
DataSet ds = new DataSet("Image");
OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql, this._objConn);
OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
tempAP.Fill(ds, "WebImages");
try
{
this._objConn.Open();
DataRow objNewRow = ds.Tables["WebImages"].NewRow();
objNewRow["ContentType"] = contentType;
objNewRow["Data"] = buffer;
ds.Tables["WebImages"].Rows.Add(objNewRow);
tempAP.Update(ds, "Table");
}
catch(Exception e){return e.Message;}
finally{this._objConn.Close();}
return null;
}

Now, the problem is that this table also contains a numeric primary key
field set to auto-increment. Whenever I use a regular INSERT statement,
the field does its job and automatically enters the next value. However,
the above method does not. I can just as easily add a field in the above
code, but what value would I assign it? I tried just assigning it "null"
but that didn't work, nor did an empty string. I could grab the current
highest value from the table, add one, and use that. But wouldn't that
risk a race condition if two users are performing the operation
simultaneously?

What do you recommend to solve this? Any help would be much appreciated.
Thank you.
Regards,
David P. Donahue
dd******@ccs.neu.edu

Jul 21 '05 #2
Nick Malik [Microsoft] wrote:
Sure: change the data type of the primary key from an INT to a GUID. In the
code below, create your own Guid value ( see Guid.NewGuid() ), and place the
value into the table.


In the database itself? I'm seeing no GUID data type. FYI, I'm using
MySQL Control Center to manage a MySQL database. Is the interface just
not showing me this type, or is it something specific to another database?
Regards,
David P. Donahue
dd******@ccs.neu.edu
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas | last post by:
Hello list, I suspect, this is a common issue for newbies. Is there a simple way to have an auto-updating timestamp like mysql has ? create table something ( id int4, sometext...
1
by: Chris Jackson | last post by:
I'm a novice Access user and am not sure how to solve the following problem. Any help with the following would be greatly appreciated! I have two tables with identical structures, the first holds...
3
by: Tc | last post by:
Hi, I was curious, I am thinking of writing an application that loads a dataset from a database that resides on a server. The question I have is this, if multiple copies of the app will be...
11
by: Eych | last post by:
I get a VB error when I try to update a table whose field I change with the following statement: dcUpdate.Dataset.Tables(0).Rows(0)("User Name") = Me.textbox1.Text if I change the field name...
0
by: Dan | last post by:
I have an access database that contains a linked dbf table that is based on a shapefile in ARC-Editor. I would like to auto-update a table based on the new records that have been entered into GIS....
0
by: OldStd | last post by:
Updating data using 2 data sets I am having some problems in updating the database using two datasets as suggested by someone. 1. Data is displayed in a data grid from a dataset generated using...
33
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range...
2
by: rpjd | last post by:
I am trying to submit entries in a form to a database. I am using " within my submission form and $_POST I am getting the connection to the database but I my script is not executing,...
6
by: Dave | last post by:
I want to put the information that the user selects in my combo boxes into a subform that lies on the same form as the combo boxes. Thanks for your help already, Dave
19
peeaurjee
by: peeaurjee | last post by:
hi Its my first post to e-scripts. i hope i will get help soon for my problem. I have 2 tables Employees and the second one is Training. The relation between them is 1 to 1. i have...
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.