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

Sql insert statement, how to generate a primary key?

Okay lets say I want to generate a primary key for organization_ID instead of manually entering it in a textbox I created.
When this program runs the user is not suppose to see the primary key when submitting this information. Basically what I want is a random unique idenfier generated.

My idea was to use some kind of count function?




Expand|Select|Wrap|Line Numbers
  1.  Public Shared Function Insert_Organization() As DataSet
  2.  
  3.         Dim _DBConnector As New DBConnector()
  4.         Dim ds As New DataSet
  5.         Dim query As String
  6.         Try
  7.             query = "INSERT INTO Organization (Organization_Name, Organization_Info, Organization_Statistics, Organization_Total_Donated, Organization_ID) VALUES ('" & DataBaseProject_Hunger.AddOrganization.OrgNameTextBox.Text & "', '" & DataBaseProject_Hunger.AddOrganization.TextBox1.Text & "','" & DataBaseProject_Hunger.AddOrganization.TextBox2.Text & "','" & DataBaseProject_Hunger.AddOrganization.TextBox3.Text & "','" & DataBaseProject_Hunger.AddOrganization.TextBox4.Text & "' )"
  8.             ds = _DBConnector.GetDataSet(query)
  9.         Catch err As Exception
  10.             MessageBox.Show(err.Message)
  11.         Finally
  12.             MsgBox("Added New Organization sucessfully.", MsgBoxStyle.Information, "")
  13.         End Try
  14.         Return ds
Apr 22 '13 #1

✓ answered by vijay6

Hey jsand03z, if "Organization_ID" is an integer type then the simple way is set "Is Identity" to 'Yes' in your SQL Server 2012 database. Then don't insert value for "Organization_ID" column whenever you're inserting a new row in your table (The database will do it for you automatically).

4 2345
r035198x
13,262 8TB
Don't do it with your code. Let the database do it for you. How to make your database do it for you depends on which database you are using.
Apr 22 '13 #2
@r035198x
I'm using sql server 2012. I tried it that way once and the program broke.
Apr 22 '13 #3
vijay6
158 100+
Hey jsand03z, if "Organization_ID" is an integer type then the simple way is set "Is Identity" to 'Yes' in your SQL Server 2012 database. Then don't insert value for "Organization_ID" column whenever you're inserting a new row in your table (The database will do it for you automatically).
Apr 22 '13 #4
Thank you, it seems to work fine now.
Apr 22 '13 #5

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

Similar topics

4
by: soni29 | last post by:
hi, i have a small question regarding sql, there are two tables that i need to work with on this, one has fields like: Table1: (id, name, street, city, zip, phone, fax, etc...) about 20 more...
1
by: Cliff | last post by:
I'm trying to do multiple insert statements. The table looks like this: CREATE TABLE $table (CNTY_CNTRY_CD char(3),ST char(2), CNTY_CNTRY_DESCR varchar(50),CNTY_CNTRY_IND char(1),...
10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
2
by: eric dugal | last post by:
Hi all!! I need your help.... i'm working since 2 hours on a simple insert statement, but couldn't handle it. Here is my code : public int ExecQuery(string SqlString) {
7
by: Cindy H | last post by:
Hi I'm having a problem getting the insert statement correct for an Access table I'm using. The Access table uses an autonumber for the primary key. I have tried this: INSERT INTO Tournaments...
2
by: John | last post by:
The ASP application inserts transaction records in transaction table with the system time as the primary key. However, it is possible to have primary key violation because the records in...
1
by: filip1150 | last post by:
I'm trying to find if there is any performance diference between explicitly using a sequence in the insert statement to generate values for a column and doing this in an insert trigger. I...
2
by: ksenthilbabu | last post by:
Hey All, I am using MSSQL -2005 with VB6. I have created a master table tblCompany and detail Table tblDetail having foreign key relationship. When i try to insert a value within a TRANSACTION I...
1
by: bougie | last post by:
i have two tables projects and proposals in project I have column ID and in proposal I have column ID too and there is a one to one realtionship between these tables the primary key in this realtion...
2
by: Good Guy | last post by:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Customers_Contact". The conflict occurred in database "BreakAway", table "dbo.Contact", column 'ContactID'. The statement has...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.