By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,238 Members | 1,485 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,238 IT Pros & Developers. It's quick & easy.

Sql insert statement, how to generate a primary key?

P: 5
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).

Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
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

P: 5
@r035198x
I'm using sql server 2012. I tried it that way once and the program broke.
Apr 22 '13 #3

100+
P: 158
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

P: 5
Thank you, it seems to work fine now.
Apr 22 '13 #5

Post your reply

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