Zdravim, mam taky problem ktory ma trapi dlhsi cas a ani googlenie mi moc nepomohlo tak mi neostava nic ine ako sa spytat... Mam dve tabulky - Articles a Categories, pricom v tabulke Articles mam okrem ineho aj stlpec ArticleCategoryId ktory je foreign key z tabulky Categories. V admin sekcii stranky mam formular na pridavanie clankov (Articles). Potom co mi VWD vygeneroval do InsertItemTemplate same textboxy tak som jeden nahradil DropDownListu do ktoreho som nacital nazvy kategorii z tabulky Categories. A teraz sa snazim dosiahnut toho aby sa mi do tabulky Articles, do stlpca ArcticleCategoryId dosadilo ID kategorie ktoru vyberiem z dropdownlistu lenze aj napriek tomu ze som to skusal asi milionom sposobou cez parametre s bindovanim mi to stale hadze hodnotu NULL do DB. Este na zaver dodam ze ked som namiesto DropDownListu dal spat TextBox a zadal hodnotu ArticleCategoryId rucne tak vsetko slapalo OK takze problem urcite nie je na strane DB. Este prihodim zdrojaky : takto vyzeraju tabulky articles a categories resp. su to queries s ktorymi som ich vytvoril :
CREATE TABLE dbo.Categories
(
CategoryId int IDENTITY(1,1) NOT NULL,
CategoryName nvarchar(100) NOT NULL,
CONSTRAINT PK_Categories PRIMARY KEY CLUSTERED (CategoryId ASC)
)
CREATE TABLE dbo.Articles
(
ArticleId int IDENTITY(1,1) NOT NULL,
ArticleTitle nvarchar(100) NOT NULL,
ArticleText ntext NOT NULL,
ArticleReleaseDate datetime NOT NULL,
ArticleAuthorId int NOT NULL,
ArticleCategoryId int NULL,
ArticleGalleryId int NULL,
ArticleVideoId int NULL,
CONSTRAINT PK_Articles PRIMARY KEY CLUSTERED (ArticleId ASC),
CONSTRAINT [FK_Articles_Categories] FOREIGN KEY([ArticleCategoryId]) REFERENCES [dbo].[Categories] ([CategoryId]) ON DELETE SET NULL ON UPDATE
SET NULL,
CONSTRAINT [FK_Articles_Authors] FOREIGN KEY([ArticleAuthorId]) REFERENCES [dbo].[Users] ([UserId]) ON DELETE NO ACTION ON UPDATE NO ACTION
)
ALTER TABLE [dbo].[Categories] WITH CHECK ADD CONSTRAINT PK_Categories PRIMARY KEY CLUSTERED (CategoryId ASC),
ALTER TABLE [dbo].[Articles] WITH CHECK ADD CONSTRAINT PK_Articles PRIMARY KEY CLUSTERED (ArticleId ASC),
ALTER TABLE [dbo].[Articles] WITH CHECK ADD CONSTRAINT [FK_Articles_Categories] FOREIGN KEY([ArticleCategoryId]) REFERENCES [dbo].[Categories]
([CategoryId]) ON DELETE SET NULL ON UPDATE SET NULL,
ALTER TABLE [dbo].[Articles] WITH CHECK ADD CONSTRAINT [FK_Articles_Authors] FOREIGN KEY([ArticleAuthorId]) REFERENCES [dbo].[Users] ([UserId]) ON DELETE
NO ACTION ON UPDATE NO ACTION
a takto vyzera sqldatasource :
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DeathstarsConnectionString %>"
DeleteCommand="DELETE FROM [Articles] WHERE [ArticleId] = @ArticleId"
InsertCommand="INSERT INTO [Articles] ([ArticleTitle], [ArticleText], [ArticleReleaseDate], [ArticleAuthorId], [ArticleCategoryId], [ArticleGalleryId], [ArticleVideoId]) VALUES (@ArticleTitle, @ArticleText, @ArticleReleaseDate, @ArticleAuthorId, @ArticleCategoryId, @ArticleGalleryId, @ArticleVideoId)"
SelectCommand="SELECT [ArticleId], [ArticleTitle], [ArticleText], [ArticleReleaseDate], [ArticleAuthorId], [ArticleCategoryId], [ArticleGalleryId], [ArticleVideoId], [CategoryId], [CategoryName] FROM [Articles] LEFT JOIN [Categories] ON [ArticleCategoryId] = [CategoryId]"
UpdateCommand="UPDATE [Articles] SET [ArticleTitle] = @ArticleTitle, [ArticleText] = @ArticleText, [ArticleReleaseDate] = @ArticleReleaseDate, [ArticleAuthorId] = @ArticleAuthorId, [ArticleCategoryId] = @ArticleCategoryId, [ArticleGalleryId] = @ArticleGalleryId, [ArticleVideoId] = @ArticleVideoId WHERE [ArticleId] = @ArticleId"
ProviderName="<%$ ConnectionStrings:DeathstarsConnectionString.ProviderName %>">
<DeleteParameters>
<asp:Parameter Name="ArticleId" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ArticleTitle" Type="String" />
<asp:Parameter Name="ArticleText" Type="String" />
<asp:Parameter Name="ArticleReleaseDate" Type="DateTime" />
<asp:Parameter Name="ArticleAuthorId" Type="Int32" />
<asp:Parameter Name="ArticleCategoryId" Type="Int32" />
<asp:Parameter Name="ArticleGalleryId" Type="Int32" />
<asp:Parameter Name="ArticleVideoId" Type="Int32" />
<asp:Parameter Name="ArticleId" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ArticleTitle" Type="String" />
<asp:Parameter Name="ArticleText" Type="String" />
<asp:Parameter Name="ArticleReleaseDate" Type="DateTime" />
<asp:Parameter Name="ArticleAuthorId" Type="Int32" />
<asp:Parameter Name="ArticleCategoryId" Type="Int32" />
<asp:Parameter Name="ArticleGalleryId" Type="Int32" />
<asp:Parameter Name="ArticleVideoId" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
a takto vyzera insertItemTemplate vo FormView :
<InsertItemTemplate>
ArticleTitle:
<asp:TextBox ID="ArticleTitleTextBox" runat="server"
Text='<%# Bind("ArticleTitle") %>' />
<br />
ArticleText:
<asp:TextBox ID="ArticleTextTextBox" runat="server"
Text='<%# Bind("ArticleText") %>' />
<br />
ArticleReleaseDate:
<asp:TextBox ID="ArticleReleaseDateTextBox" runat="server"
Text='<%# Bind("ArticleReleaseDate") %>' />
<br />
ArticleAuthorId:
<asp:TextBox ID="ArticleAuthorIdTextBox" runat="server"
Text='<%# Bind("ArticleAuthorId") %>' />
<br />
ArticleCategoryId:
<asp:TextBox ID="ArticleCategoryIdTextBox" runat="server"
Text='<%# Bind("ArticleCategoryId") %>' />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CategoryName" DataValueField='<%# Bind("ArticleCategoryId") %>' AppendDataBoundItems="True"></asp:DropDownList>
<br />
ArticleGalleryId:
<asp:TextBox ID="ArticleGalleryIdTextBox" runat="server"
Text='<%# Bind("ArticleGalleryId") %>' />
<br />
ArticleVideoId:
<asp:TextBox ID="ArticleVideoIdTextBox" runat="server"
Text='<%# Bind("ArticleVideoId") %>' />
<br />
CategoryName:
<asp:TextBox ID="CategoryNameTextBox" runat="server"
Text='<%# Bind("CategoryName") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" onclick="InsertButton_Click" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
Za pomoc velke dakujem ;)
|