I'm basically building a classified ad. When I insert an ad, it automatically creates the AdID (Identity Specification = Yes), but I'm also uploading some photos at the same time, into a photos table. How can I get this newly generated AdID from my classifieds
table and pass it to the AdID column of the photos table all during the same insert button click?
I'm using two separate insert statements, because the photos insert loops through for multiple photos. I have a SqlDataSource setup in the aspx page for the classified ad details, and another setup in my code behind for the photos table, inside the loop.
Then when the ad is displayed, it searches the photos table for all photos where the AdID matches the ad's AdID.
GO
CREATE TABLE MMImageRotator_Basic (
[ImageRotatorID] [uniqueidentifier] NOT NULL,
[RotatorZone] [int] NOT NULL,
[Image] [nvarchar](100) NOT NULL,
[ThumbImage] [nvarchar](100) NOT NULL,
[Description] [nvarchar](255) NULL,
[DateCreated] [datetime] NOT NULL,
[EntryOrder] [int] NOT NULL,
CONSTRAINT [PK_MMImageRotator_Basic] PRIMARY KEY CLUSTERED (ImageRotatorID))
GO
Best Regards
Primillo
http://www.facebook.com/programandopuntonet
I figured out what you meant I believe and got this to work! Thanks! I added a new column to both tables for UniqueID, then set that string you mentioned at the top of the codebehind (so I could use it throughout the codebehind) and applied that string value
to the ad and all photos uploaded. Then when viewing the ad inside the website, I just had to make a small adjustment to look for that UniqueID to find the matching photos :)
Maybe you can select top (1) from the table order by datecreated desc
That wouldn't have worked in this case, because if another user posted an ad at the same time, their AdID might get applied to the wrong photos.
Primillo's solution worked. It defined a unique string that could be remembered and re-used throughout the codebehind and I applied it to a column (UniqueID) in the Classifieds table and a column (UniqueID) in the ClassifiedsPhotos table. Then I just displayed
them in the website by finding ads and photos with a common string of characters in the UniqueID columns.
srelliott
Member
177 Points
243 Posts
Need to pass dynamic row ID from one table to another table during insert
Jul 04, 2012 08:19 PM|LINK
I'm basically building a classified ad. When I insert an ad, it automatically creates the AdID (Identity Specification = Yes), but I'm also uploading some photos at the same time, into a photos table. How can I get this newly generated AdID from my classifieds table and pass it to the AdID column of the photos table all during the same insert button click?
I'm using two separate insert statements, because the photos insert loops through for multiple photos. I have a SqlDataSource setup in the aspx page for the classified ad details, and another setup in my code behind for the photos table, inside the loop. Then when the ad is displayed, it searches the photos table for all photos where the AdID matches the ad's AdID.
Primillo
Star
8723 Points
1677 Posts
Re: Need to pass dynamic row ID from one table to another table during insert
Jul 04, 2012 08:42 PM|LINK
You can use Guid as ID (uniqueidentifier)
The membership shema use guid data type as ID, the asp.net team chose guid as ID for a reason.
Primillo
http://www.facebook.com/programandopuntonet
Primillo
Star
8723 Points
1677 Posts
Re: Need to pass dynamic row ID from one table to another table during insert
Jul 04, 2012 08:44 PM|LINK
Here is an example:
GO CREATE TABLE MMImageRotator_Basic ( [ImageRotatorID] [uniqueidentifier] NOT NULL, [RotatorZone] [int] NOT NULL, [Image] [nvarchar](100) NOT NULL, [ThumbImage] [nvarchar](100) NOT NULL, [Description] [nvarchar](255) NULL, [DateCreated] [datetime] NOT NULL, [EntryOrder] [int] NOT NULL, CONSTRAINT [PK_MMImageRotator_Basic] PRIMARY KEY CLUSTERED (ImageRotatorID)) GOPrimillo
http://www.facebook.com/programandopuntonet
srelliott
Member
177 Points
243 Posts
Re: Need to pass dynamic row ID from one table to another table during insert
Jul 04, 2012 09:35 PM|LINK
I figured out what you meant I believe and got this to work! Thanks! I added a new column to both tables for UniqueID, then set that string you mentioned at the top of the codebehind (so I could use it throughout the codebehind) and applied that string value to the ad and all photos uploaded. Then when viewing the ad inside the website, I just had to make a small adjustment to look for that UniqueID to find the matching photos :)
oned_gk
All-Star
31017 Points
6352 Posts
Re: Need to pass dynamic row ID from one table to another table during insert
Jul 05, 2012 02:13 AM|LINK
Maybe you can select top (1) from the table order by datecreated desc
srelliott
Member
177 Points
243 Posts
Re: Need to pass dynamic row ID from one table to another table during insert
Jul 05, 2012 02:40 AM|LINK
That wouldn't have worked in this case, because if another user posted an ad at the same time, their AdID might get applied to the wrong photos.
Primillo's solution worked. It defined a unique string that could be remembered and re-used throughout the codebehind and I applied it to a column (UniqueID) in the Classifieds table and a column (UniqueID) in the ClassifiedsPhotos table. Then I just displayed them in the website by finding ads and photos with a common string of characters in the UniqueID columns.
oned_gk
All-Star
31017 Points
6352 Posts
Re: Need to pass dynamic row ID from one table to another table during insert
Jul 05, 2012 02:58 AM|LINK
I think saving data and getting AdID is in milisecond before uploading photo.
other way : is Ad unique? If yes, you can use Ad to get the Adid.
or
filter with some the data that just added. like where name= and datebirth=