how to get value inserted using insertcommand in Formview out to be used in code behind

Last post 07-07-2009 8:38 AM by superguppie. 7 replies.

Sort Posts:

  • how to get value inserted using insertcommand in Formview out to be used in code behind

    07-05-2009, 9:43 AM
    • Member
      point Member
    • hkaspnet
    • Member since 07-05-2009, 1:24 PM
    • Posts 3

    Hi There,

    I have two related questions below.  Please help.  Many thanks in advance. 

     

    1)  I use an insert template in a formview to add new data( ID, Name, Address) to a table.  After I click the add button, new data adds to the table sucessfully.  However, after that, I need to add a few more rows in a different table automatically using the ID of the new data.  

    How can I obtain/retain the ID to be used in asp.vb code behind file?  

     

    2)  In edit/insert template of a formview, I would love to have a dropdownlist instead of just a plain textfield for user to select pre-defined choices.  How can I do that? 

     

    I tried just putting a dropdownlist control in the template and access it in asp.vb with an event handler that handles iteminserted/itemupdated, but I just can't find the dropdownlist control in asp.vb.     

     

    Best,

    Aaron

  • Re: how to get value inserted using insertcommand in Formview out to be used in code behind

    07-05-2009, 11:19 AM
    • Contributor
      7,023 point Contributor
    • superguppie
    • Member since 05-19-2009, 7:42 AM
    • Posts 1,240
    1) In the handler for ItemInserting the event arguments will have a Dictionary holding the value of each field. You can read it from there and store it for later use.

    2) You can put a DropDownList in the Template just like anywhere else. To bind the selection to a field in the data, use SelectedValue='<%# Bind("fieldname") %>
    The trick to finding it is that it won't be directly accessible with a generated member. Instead, you will have to use FormViewID.FindControl("DropDownListID").
    Superguppie.

    Please remember to click “Mark as Answer” on the post that helps you.
    This can be beneficial to other community members reading the thread.

    When all you've got is a Hammer,
    Every Problem looks like a Nail.
    Michael Swain.
  • Re: how to get value inserted using insertcommand in Formview out to be used in code behind

    07-06-2009, 12:32 AM
    • Member
      point Member
    • hkaspnet
    • Member since 07-05-2009, 1:24 PM
    • Posts 3

    Thanks so much Superguppie!! 

    1)  I tried to use figure out how to use the dictionary, but not getting anywhere.  Can you provide an example please?

    My Code:

    Protected Sub New_Student(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ItemInserting

    Dim StudentCourseAdaptor As New DataSet1TableAdapters.StudentCourseTableAdapter

    ID = ???????????????

    StudentCourseAdaptor.Insert( ID , " ", " " , " ") End Sub

    What should ???????????  be?  

     

    2)  If I have a dropdownlist that let user select day of week, then the dropdownlist control should have " SelectedValue='<%# Bind("@DayofWeek") %> ??

    And where should I put FormViewID.FindControl("DropDownListID")

     

    Thanks so much again.  I think I can understand asp much better after solving these 2 problems.

     

    Sincerely,

    Aaron

     

     

     

     

     

     

  • Re: how to get value inserted using insertcommand in Formview out to be used in code behind

    07-06-2009, 1:10 PM
    Answer
    • Contributor
      7,023 point Contributor
    • superguppie
    • Member since 05-19-2009, 7:42 AM
    • Posts 1,240
    The signature of your handler for ItemInserting is not exactly what it should be. And that is probably causing a lot of the confusion for 1).
    The most specific type of e is FormViewInsertEventArgs (see msdn) This object has a Values member (see more msdn) This is the Dictionary I was talking about.
    The value you are looking for should be in e.Values("ID").

    For 2): Binding isn't done to parameters, but to fields. So SelectedValue='<%# Bind("DayofWeek") %>' should do it. This can be accessed as @DayofWeek in the InsertCommand.

    I think this should do the trick already. The line about FindControl was in response to your question on how to find a DropDownList. If you really want to find that for some reason or other, you should use FindControl. However, I seem to have missed a bit on my last post. You can find the DropDownList for ID with FormViewID
    .Row.FindControl("DropDownListID"). This can be used in many places. However, personaly, I would avoid using it, in favor of more generic code. There are many events (such as ItemCommand) where there is a direct link from the event-arguments to the row. Once you are used to that, you will likely find it easier because other Controls do similar things in similar ways.
    Superguppie.

    Please remember to click “Mark as Answer” on the post that helps you.
    This can be beneficial to other community members reading the thread.

    When all you've got is a Hammer,
    Every Problem looks like a Nail.
    Michael Swain.
  • Re: how to get value inserted using insertcommand in Formview out to be used in code behind

    07-06-2009, 1:16 PM
    • Star
      8,262 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,419

    superguppie:
    The most specific type of e is FormViewInsertEventArgs (see msdn) This object has a Values member (see more msdn) This is the Dictionary I was talking about.
    The value you are looking for should be in e.Values("ID").
     

     

    I think we're dealing with a Identity column. A solution on how to get the ID of the newly created record can be found at:

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=54

     

  • Re: how to get value inserted using insertcommand in Formview out to be used in code behind

    07-06-2009, 5:59 PM
    • Contributor
      7,023 point Contributor
    • superguppie
    • Member since 05-19-2009, 7:42 AM
    • Posts 1,240
    Could be. However, the phrase "add new data( ID, Name, Address) to a table" does give me the impression ID is actualy a field that will be entered by the user.
    Superguppie.

    Please remember to click “Mark as Answer” on the post that helps you.
    This can be beneficial to other community members reading the thread.

    When all you've got is a Hammer,
    Every Problem looks like a Nail.
    Michael Swain.
  • Re: how to get value inserted using insertcommand in Formview out to be used in code behind

    07-07-2009, 4:40 AM
    • Member
      point Member
    • hkaspnet
    • Member since 07-05-2009, 1:24 PM
    • Posts 3

    ID is a field that needs user's input. 

    Thanks so much, Superguppie !!   e.value works brilliantly.  My biggest problem has been solved.

     

    About the dropdownlist problem, I keep getting the following error msg

    'DropDownListTerm' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

     In fact, intellisense does not give me the property selectedvalue. I just hardcoded it in.

    <asp:DropDownList ID="DropDownListTerm" runat="server" selectedvalue= '<%# Eval("Term") %>' >

    <asp:ListItem Value="Select">Select</asp:ListItem> 

    <asp:ListItem Value="Fall">Fall</asp:ListItem>

    <asp:ListItem Value="Winter">Winter</asp:ListItem> 

    <asp:ListItem Value="Spring">Spring</asp:ListItem>

    <asp:ListItem Value="Summer">Summer</asp:ListItem> 

    </asp:DropDownList>

    UpdateCommand="UPDATE [StudentCourse] SET [CourseID] = @CourseID, [Term] = @Term, [Year] = @Year, [Grade] = @Grade WHERE [StudentID] = @StudentID">

     <UpdateParameters>

    <asp:Parameter Name="CourseID" Type="String" /> 

    <asp:Parameter Name="Term" Type="String" /> 

    <asp:Parameter Name="Year" Type="String" /> 

    <asp:Parameter Name="Grade" Type="String" /> 

    <asp:Parameter Name="Elective" Type="String" /> 

    <asp:Parameter Name="StudentID" Type="String" />

     

    Then, I try to add the selected value of the dropdownlist to the database in asp.vb by getting value from dropdown list using 

     Term.Text =CType(FormView2.FindControl("Term"), DropDownList).SelectedItem.Value

     

    But, it also gives me error msg about instances . 

    I have seen quite a few example and reading some books and still have no clue. 

    Can you help me with this as well?

     

    All I want is to use a dropdownlist in a formview.  It should be very straight forward.  I am very surprise Visual Web Developer doesn't handle it automatically. 

     

    Gratefully,

    Aaron  

     

     

     

  • Re: how to get value inserted using insertcommand in Formview out to be used in code behind

    07-07-2009, 8:38 AM
    • Contributor
      7,023 point Contributor
    • superguppie
    • Member since 05-19-2009, 7:42 AM
    • Posts 1,240
    Intellisense doesn't pick this up well. But when you set it by typing, it should work fine.
    My best guess for the exception about SelectedValue being invalid is that in some row is a value that is not one from the list. Check your data for any such value. (maybe there is a row with null-value in it?)

    If you use Bind instead of Eval, you should get the SelectedValue in e.Values, just like the others. This would make it unnecessary to use FindControl.
    And as I posted earlier, I messed up the FindControl a bit. FormView2.Row.FindControl is likely to work better. Also, the ID to look for has to be the ID of the DropDownList, not that of the Field. So in this case that would be DropDownListTerm, not Term.

    Hope that does it.

    The problems with DropDownList and DataBinding Controls is not really a VWD problem. It's an ASP.NET thing. And for FromView it isn't really a difference. (In for instance DetailsView, there is an easier way to declare fields to use. Works very easy for TextBoxes and CheckBoxes. But DropDownList still has to be done manualy.) DataBinding should work pretty much the same way as it does for other Controls. So I don't really see in what way DropDownList is the exception.
    Superguppie.

    Please remember to click “Mark as Answer” on the post that helps you.
    This can be beneficial to other community members reading the thread.

    When all you've got is a Hammer,
    Every Problem looks like a Nail.
    Michael Swain.
Page 1 of 1 (8 items)