Trouble Inserting with a Foreign Key

Last post 07-05-2008 3:17 PM by GrandpaB. 7 replies.

Sort Posts:

  • Trouble Inserting with a Foreign Key

    07-04-2008, 11:18 AM
    • Loading...
    • GrandpaB
    • Joined on 09-17-2006, 2:51 AM
    • Dayton, OH
    • Posts 80

    Good Morning,

    This I'm sure is another newbie question.  I'm attempting to insert a record into a table that has both a primary and foreign key.  The ASP web page includes a DropDownList and a DetailsView.  The user selects an item in the DropDownList and its value should become the foreign key (IDc) to be inserted into the new record. How do I modify the InsertCommand of the DetailsView so that the value of the foreign key is set to the value of the DropDownList?  I hide the keys from users so the DetailsView does not display either key.  Cuttently, inserting procuces an error that says that the foreign key IDc cannot be null.  Here is the InsertCommand.

            InsertCommand="INSERT INTO [Tees] ([IDc], [Color], [Rating], [Slope], [FwdTee]) VALUES (@IDc, @Color, @Rating, @Slope, @FwdTee)" 
    

    Thanks, for your help. 

    GrandpaB
  • Re: Trouble Inserting with a Foreign Key

    07-04-2008, 1:40 PM
    Answer
    • Loading...
    • limno
    • Joined on 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 3,430
    • Moderator
      TrustedFriends-MVPs

    You need to work on your InserParameter for IDc which comes from the DropDownList on your DetailsView.

    Try this:

    <InsertParameters>
    <asp:ControlParameter Name="IDc" Type="Int32" ControlID="DetailsView1$ddl1" PropertyName="SelectedValue" />
    </InsertParameters>
     
    Limno

  • Re: Trouble Inserting with a Foreign Key

    07-04-2008, 2:56 PM
    • Loading...
    • GrandpaB
    • Joined on 09-17-2006, 2:51 AM
    • Dayton, OH
    • Posts 80

    Limno,

    Again you came to the rescue, thanks!  I did have to make several attempts before I got the code right.  Here is what I ended up with.

        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConflictDetection="CompareAllValues" 
            ConnectionString="<%$ ConnectionStrings:CS-GolfSQL %>" 
            DeleteCommand="DELETE FROM [Tees] WHERE [IDt] = @original_IDt" 
            InsertCommand="INSERT INTO [Tees] ([IDc], [Color], [Rating], [Slope], [FwdTee]) VALUES (@IDc, @Color, @Rating, @Slope, @FwdTee)" 
            OldValuesParameterFormatString="original_{0}" 
            SelectCommand="SELECT * FROM [Tees] WHERE ([IDc] = @IDc) ORDER BY [Color]" 
            UpdateCommand="UPDATE [Tees] SET [Color] = @Color, [Rating] = @Rating, [Slope] = @Slope, [FwdTee] = @FwdTee WHERE [IDt] = @original_IDt">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="IDc" 
                    PropertyName="SelectedValue" Type="Int16" />
            </SelectParameters>
            <InsertParameters>
                <asp:ControlParameter Name="IDc" Type="Int16" ControlID="DropDownList1" PropertyName="SelectedValue" />
                <asp:Parameter Name="Color" Type="String" />
                <asp:Parameter Name="Rating" Type="Single" />
                <asp:Parameter Name="Slope" Type="Int16" />
                <asp:Parameter Name="FwdTee" Type="Boolean" />
            </InsertParameters>
        </asp:SqlDataSource>

    For the sake of brevity I deleted the  Delete & Update parameters.  It took me a couple of tries before changed ControlID="DetailsView1$ddl1" to ControlID="DropDownList1" and deleted the original <asp:Parameter Name="IDc" Type="Int16" />.  With my current lack of expertise I don't feel as if I'm designing code; I feel as if I'm taking a random walk in a code/mine field and any step could cause the whole thing to blowup.  Once I have a basic understanding of a code topic I find that the MSDN help and turorials are useful.  But, when I'm at the beginning of ASP and SQL I need a text with examples.  Can you or others recommend such a text?

     

    GrandpaB
  • Re: Trouble Inserting with a Foreign Key

    07-04-2008, 5:13 PM
    • Loading...
    • limno
    • Joined on 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 3,430
    • Moderator
      TrustedFriends-MVPs

    Is your code working now?

    Limno

  • Re: Trouble Inserting with a Foreign Key

    07-04-2008, 5:43 PM
    • Loading...
    • lberan
    • Joined on 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 516

     

    make sure you have set autopostback true for the dropdownlist
    Lynn

  • Re: Trouble Inserting with a Foreign Key

    07-04-2008, 10:32 PM
    • Loading...
    • GrandpaB
    • Joined on 09-17-2006, 2:51 AM
    • Dayton, OH
    • Posts 80

    Thanks everyone,

    In my response to Limno I should have been more direct.  The application is working!  I am early in my ASP.NET learning curve.  I would appreciate getting a recommentation from you for an ASP.NET book to help my on my journey.

    GrandpaB
  • Re: Trouble Inserting with a Foreign Key

    07-04-2008, 11:50 PM
    • Loading...
    • limno
    • Joined on 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 3,430
    • Moderator
      TrustedFriends-MVPs

    I do have a bunch of ASP.NET books, but it is not easy to pick one or two for you. I have the ASP.NET Unleashed (1.0/1.1, 2.0,3.5) series and it is helpful to get a jump in the learning process. You can also try to visit some website with ASP.NET articles such as  4guysfromrolla.com, aspnet101.com and more. You can always come back with questions and I am pretty it will help us to learn new things too. I hope you can enjoy your ASP.NET journey.

    Limno

  • Re: Trouble Inserting with a Foreign Key

    07-05-2008, 3:17 PM
    • Loading...
    • GrandpaB
    • Joined on 09-17-2006, 2:51 AM
    • Dayton, OH
    • Posts 80

    Limno,

    Thanks for the recommendation on books.  I subscribe to Safari books online and I now have access to the ASP.NET Unleashed 2.0 book online.  So far, so good!  It is nice to have the forums available, they're indispensible, but I’d also like to gain enough expertise so that I can help someone else too.

    GrandpaB
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter