0

Using Text Area I'm sending Values to database by clicking button, but I want postID to auto generate how I can do it?

protected void btnPost_Click(object sender, EventArgs e) { string PostQuery = "INSERT INTO [Post] (PostID,PostDet,Votes,UserID) VALUES('<auto Genatare post ID>','" + TxtPost.Text + "','12','Us001')"; dbClass.ConnectDataBaseToInsert(PostQuery); Response.Redirect("~/computing.aspx"); } 
8
  • Is PostID created as an autogenerated column in DB? Commented Mar 18, 2014 at 13:44
  • This is a db question. Make your PostId field an identity (if using SQL Server, unsure about other dbs) and it will autogenerate the value for you. Commented Mar 18, 2014 at 13:45
  • please add a tag that specifies Database you use Commented Mar 18, 2014 at 13:45
  • If PostID is auto-generated in your DB anything you try to save to that column will be discarded Commented Mar 18, 2014 at 13:47
  • My Post ID look like "PO0001" like that, when i insert one record and before another record insert i want to get last ID and make it like "PO002" when click on button Commented Mar 18, 2014 at 13:49

5 Answers 5

1

You could make PostID a UNIQUEIDENTIFIER column and then pass in a newly generated GUID (Guid.NewGuid()).

Also, please use parameterized queries to avoid SQL injection. Especially if the inputs come directly from WEB users.

To do so, change your ConnectDataBaseToInsert method to not take SQL text, but an SqlCommand which you prepare with the respective parameters.


From your comment to the question: The PostID should be like PO0001. Then the only way to do it properly and to respect for concurrency is to generate a stored procedure that takes the value to insert, which generates the ID itself.

To do so, create a new table that contains the last post ID. Then, use an UPDATE ... OUTPUT statement to increment and return in one go. This is the only way to do an atomic update of the post ID so that no two users create the same ID.

Example Table PostIDTable

Current ======= 0 

Example SELECT to update and retrieve the current post ID:

-- We need a temp table, because OUTPUT can not output into a single variable -- This increments Current by one and outputs the value that was set in one go. -- This prevents simultaneous calls to get the same ID DECLARE #postID (ID INT) UPDATE PostIDTable OUPUT INSERTED.Current INTO #postID SET Current = Current + 1 -- Get the value from the temp table and convert it into the desired format DECLARE @pID INT = (SELECT TOP 1 ID FROM #postID) DECLARE @id NVARCHAR(6) = 'PO' + RIGHT('0000' + CONVERT(NVARCHAR, @pID), 4) -- Do the actual INSERT INSERT INTO (PostDet, Votes,UserID) VALUES (@id, ..., ...) 
Sign up to request clarification or add additional context in comments.

2 Comments

sir i new to this field so how can i use this Code in and i earlier post my current code that i coded, please help me . . thank you
1) Create the table I mentioned. 2) Create a stored procedure that contains the code I wrote. 3) In C# use an SQL Command to call the stored procedure with the values you want to insert as parameter (CommandType = StoredProcedure). If you have troubles with that, please ask a new question.
0

You should make the PostID column to be an IDENTITY(1,1) column (in case of using MSSQL. server). So when you will insert new rows to your database the corresponding PostID values will be autogenerated by your database. The same holds for each other RDBMS you may use.

Having done the above change you code will change to the following:

protected void btnPost_Click(object sender, EventArgs e) { string PostQuery = "INSERT INTO [Post] (PostDet,Votes,UserID) VALUES("TxtPost.Text + "','12','Us001')"; dbClass.ConnectDataBaseToInsert(PostQuery); Response.Redirect("~/computing.aspx"); } 

Comments

0

If PostId is autogenerated, only do this:

protected void btnPost_Click(object sender, EventArgs e) { string PostQuery = "INSERT INTO [Post] (PostDet,Votes,UserID) VALUES('" + TxtPost.Text + "','12','Us001')"; dbClass.ConnectDataBaseToInsert(PostQuery); Response.Redirect("~/computing.aspx"); } 

Removing PostId from your query, it will generate the next Id

Comments

0

the best way is to use auto increment column ID for your table. Please, see w3school tutorial where is described construction for all main db.

Comments

0

Try this..

select * from tablename order by id desc limit 1

where id would be your primary key attribute name.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.