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

Parameter with auto increment

Hello,

I have an autoincrement field and I'm not sure how to use a parameter
with it to insert data:

this.sqlCommand1.CommandText = "INSERT INTO SickAndTired (String_ID,
Input_string) VALUES (@Parameter1,@Parameter2)";

this.sqlCommand1.Connection = this.sqlConnection1;

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter1",
System.Data.SqlDbType.UniqueIdentifier, 0, "String_ID"));

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter2",
System.Data.SqlDbType.NVarChar, 0, "Input_String"));

this.sqlConnection1.Open();

string TB1 = this.textBox1.Text;

sqlCommand1.Parameters["@Parameter1"].Value= (What do I put
here???????????????????)

sqlCommand1.Parameters["@Parameter2"].Value=TB1;

sqlCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

Thanks

Mike
Nov 17 '05 #1
4 7189
Mike,

For something like this, you aren't going to get it through a parameter
in a select. Rather, you have to issue a select on the same open connection
on the @@IDENTITY value, which will return the last inserted identity value
for that session.

You have to be careful, since the identity can be changed by triggers
(if you insert into other tables with auto-generated identity fields then
that value will be returned).

Generally, this is a good argument for managing the id generation
yourself. Personally, I prefer to use GUIDs, since I can generate them
before the operation starts, and know exactly what my id will be (before and
after).

If you want to get this in a parameter, then create a stored procedure
which will select the @@IDENTITY value and return that in a parameter.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike" <am******@verizon.net> wrote in message
news:N6_Re.121$tx.8@trndny02...
Hello,

I have an autoincrement field and I'm not sure how to use a parameter
with it to insert data:

this.sqlCommand1.CommandText = "INSERT INTO SickAndTired (String_ID,
Input_string) VALUES (@Parameter1,@Parameter2)";

this.sqlCommand1.Connection = this.sqlConnection1;

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter1",
System.Data.SqlDbType.UniqueIdentifier, 0, "String_ID"));

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter2",
System.Data.SqlDbType.NVarChar, 0, "Input_String"));

this.sqlConnection1.Open();

string TB1 = this.textBox1.Text;

sqlCommand1.Parameters["@Parameter1"].Value= (What do I put
here???????????????????)

sqlCommand1.Parameters["@Parameter2"].Value=TB1;

sqlCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

Thanks

Mike

Nov 17 '05 #2
scope_identity() is probably safer to use. @@identity can potentially
produce very hard to find bugs in your app.

"Mike" wrote:
Hello,

I have an autoincrement field and I'm not sure how to use a parameter
with it to insert data:

this.sqlCommand1.CommandText = "INSERT INTO SickAndTired (String_ID,
Input_string) VALUES (@Parameter1,@Parameter2)";

this.sqlCommand1.Connection = this.sqlConnection1;

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter1",
System.Data.SqlDbType.UniqueIdentifier, 0, "String_ID"));

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter2",
System.Data.SqlDbType.NVarChar, 0, "Input_String"));

this.sqlConnection1.Open();

string TB1 = this.textBox1.Text;

sqlCommand1.Parameters["@Parameter1"].Value= (What do I put
here???????????????????)

sqlCommand1.Parameters["@Parameter2"].Value=TB1;

sqlCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

Thanks

Mike

Nov 17 '05 #3
All I did was left that parameter out and it incremented by 1.Should I have
made it more complicated?

"carion1" <ca*****@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
scope_identity() is probably safer to use. @@identity can potentially
produce very hard to find bugs in your app.

"Mike" wrote:
Hello,

I have an autoincrement field and I'm not sure how to use a parameter
with it to insert data:

this.sqlCommand1.CommandText = "INSERT INTO SickAndTired (String_ID,
Input_string) VALUES (@Parameter1,@Parameter2)";

this.sqlCommand1.Connection = this.sqlConnection1;

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter1",
System.Data.SqlDbType.UniqueIdentifier, 0, "String_ID"));

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter2",
System.Data.SqlDbType.NVarChar, 0, "Input_String"));

this.sqlConnection1.Open();

string TB1 = this.textBox1.Text;

sqlCommand1.Parameters["@Parameter1"].Value= (What do I put
here???????????????????)

sqlCommand1.Parameters["@Parameter2"].Value=TB1;

sqlCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

Thanks

Mike

Nov 17 '05 #4
Were you trying to insert a value into the identity column? If you need to
do that for what ever reason you can use set identity_insert <table name> on.
You don't have to increment the identity column yourself unless your app
depends on the identity field not having gaps from deleted records.

"Mike" wrote:
All I did was left that parameter out and it incremented by 1.Should I have
made it more complicated?

"carion1" <ca*****@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
scope_identity() is probably safer to use. @@identity can potentially
produce very hard to find bugs in your app.

"Mike" wrote:
Hello,

I have an autoincrement field and I'm not sure how to use a parameter
with it to insert data:

this.sqlCommand1.CommandText = "INSERT INTO SickAndTired (String_ID,
Input_string) VALUES (@Parameter1,@Parameter2)";

this.sqlCommand1.Connection = this.sqlConnection1;

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter1",
System.Data.SqlDbType.UniqueIdentifier, 0, "String_ID"));

this.sqlCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Parameter2",
System.Data.SqlDbType.NVarChar, 0, "Input_String"));

this.sqlConnection1.Open();

string TB1 = this.textBox1.Text;

sqlCommand1.Parameters["@Parameter1"].Value= (What do I put
here???????????????????)

sqlCommand1.Parameters["@Parameter2"].Value=TB1;

sqlCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

Thanks

Mike


Nov 17 '05 #5

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

Similar topics

8
by: Bruce Stockwell | last post by:
the setup: Webservice/WinClient application/SQL server. VS.Net (visual basic) winform wizard creates a simple form with load cancel cancelall and datagrid bound to a simple Dataset with one...
2
by: Tom | last post by:
I am trying to store information into a table that has an auto increment field. There is currently no data in the table. Using the code below I cannot insert data into the table. I get an error...
5
by: Paulovič Michal | last post by:
hi all, I have problem with SERIAL field type (or sequence functionality). I have table with three columns - ID, IDS, NAME. I want auto-increment IDS grouped by ID. Example: 1, 1, Ferdo 1, 2,...
5
by: vul | last post by:
In VB6 there is Auto Increment check box in Project Properties, which allow you have a new version every time you compile the project. Is there any easy way to have this feature in VB 2005? Some...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
1
by: rhepsi | last post by:
hii all, i have created a database with a table with number of fields....in msaccess ive set primary key as SNo: and its datatype as auto number... How to set the auto increment ???? ...
0
chumlyumly
by: chumlyumly | last post by:
Hello scripters - OS: Mac OSX Language: PHP w/ MySQL database I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The...
1
by: jimilives | last post by:
I forgot to turn on auto increment when I reinstalled this database and now I have a bunch of NULL values in my ID field for last few hundred inserts, how can I update this table to replace the NULLS...
13
by: BobLewiston | last post by:
Using Visual C# 2008 Express and SQL Server 2008 Express, I would like to insert new records into database "AdventureWorks", table "Person.Contact". To my surprise, this table's int-value identity...
3
by: paulyXvpf | last post by:
Hello, all... Just finished a handy tutorial that has helped me understand how to auto increment and post to a web page... but its only a Web User Control passing values from VB.NET method...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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.