Closed Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15

Thread: dynamically created dropdowns insert to sql

  1. #1
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    dynamically created dropdowns insert to sql

    Hey everyone its peebman2000, currently still building the questionnaire vb.net app for my client.

    I've never worked with creating dynamic controls before, but a functionality the client wants, I feel creating dropdownlist dynamically would work.

    Below is code I found on line and it works, but I need to insert the data or values from each dynamically created dropdownlist into sql.

    I have no clue on how to do that, I know I'll have to creat another for loop, but i'm lost on how to grab or capture the selected values from the dropdownlist that are created.

    Does anyone know how to grab the values from dynamically created dropdownlists? So I may be able to store the data in SQL.

    Thanks for the help.


    aspx.vb page:
    Code:
        Protected Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butOK.Click
            'Dim dt As SqlDataSource
            'dt = Sqldatasource1
            'dt.DataBind()
    
            ''code to dynamically create dropdownlist
            'Dim I As Integer
            'Dim J As Integer
            'For I = 1 To txtNumber.Text
            '    Dim MyDDL = New DropDownList
            '    MyDDL.ID = "ddlDynamic" & I
            '    For J = 1 To 3
            '        Dim MyLI As New ListItem
            '        'MyLI.Text = "Control Number: " & I & "-" & J
            '        'MyLI.Value = I & J
    
            '        MyDDL.Items.Add(MyLI)
            '        ''MyDDL.Items.Add(dt)
            '        'MyDDL.DataSourceID = Sqldatasource1.ID
            '        'MyDDL.DataBind()
            '    Next
            '    form1.Controls.Add(MyDDL)
            '    Dim MyLiteral = New LiteralControl
            '    MyLiteral.Text = "<BR><BR>"
            '    form1.Controls.Add(MyLiteral)
            'Next
    
    
    
            '''''code to dynamically create dropdownlist with data bounded to dropdownlist
            Dim I As Integer
            'Dim J As Integer
            For I = 1 To txtNumber.Text
                Dim MyDDL As New DropDownList
                MyDDL.ID = "ddlDynamic" & I
                form1.Controls.Add(MyDDL)
                MyDDL.DataSourceID = Sqldatasource1.ID
    
                MyDDL.DataValueField = "agency_name"
                MyDDL.DataBind()
                Dim MyLiteral = New LiteralControl
                MyLiteral.Text = "<BR><BR>"
                form1.Controls.Add(MyLiteral)
            Next
    
    
        End Sub
    
    aspx page:

    Code:
     <form id="form1" runat="server">
        <div>
        <B>Enter the number of DropDownList controls you want:</B><BR><BR>
    <asp:TextBox 
        id="txtNumber" 
        runat=server 
    />
    <BR><BR>
    <asp:button 
        id="butOK"
        text="OK"
        
        runat="server"
    />
    <BR><BR>
    
        </div>
        
    <asp:sqldatasource ID="Sqldatasource1" runat="server" ConnectionString="<%$ ConnectionStrings:peebman %>" SelectCommand="SELECT agency_name FROM dbo.agency"></asp:sqldatasource>
        </form>
    

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Couldn't you just use: ddlDynamic1.SelectedIndex.Value to get the value and then append it to your query?
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    Hey thanks, I'll be using a stored procedure to store in sql, but I need to capture the value in the dropdownlists.

    I tried to do a response.write to see if i'm getting the values from the dropdownlists using ddlDynamic1.selectedindex.value

    Code:
    Response.Write(ddlDynamic.selectedindex.value)
    
    and I get a not declared error for ddlDynamic.

    If I can just capture the values in the dropdownlist, I can insert it into Sql.

    Any ideas on how I can capture the selected values, because the ddlDynamic.selectedinex.value didn't work or I may be doing something wrong in how I should use your suggestion. Thanks

    Quote Originally Posted by jmurrayhead View Post
    Couldn't you just use: ddlDynamic1.SelectedIndex.Value to get the value and then append it to your query?

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    From looking at this line:

    Code:
    MyDDL.ID = "ddlDynamic" & I
    
    ddlDynamic will have an integer after it...so:
    Code:
    Response.Write(ddlDynamic1.selectedindex.value)
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  5. #5
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    Thanks, I tried that and it still says:
    Name 'ddlDynamic1' is not delcared
    I'm using:
    Code:
    Response.Write(ddlDynamic1.selectedindex.value)
    
    any idea why is not recognizing the ddlDynamic1?

    Quote Originally Posted by jmurrayhead View Post
    From looking at this line:

    Code:
    MyDDL.ID = "ddlDynamic" & I
    
    ddlDynamic will have an integer after it...so:
    Code:
    Response.Write(ddlDynamic1.selectedindex.value)
    

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    You'll have to create an instance and use FindControl like so:

    Code:
    Dim DropDownList1 As DropDownList = CType(Me.FindControl("ddlDynamic1"), DropDownList)
    Response.Write(DropDownList1.SelectedValue)
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  7. #7
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    Hey Jmurrayhead, that workd, but I need to separate each selectedvalue from each dropdown.

    i tried using the same for loop like in the code below, but that didn't work.

    How can I separate each selected value for each dropdownlist?

    aspx.vb code:
    Code:
    Protected Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butOK.Click
            'Dim dt As SqlDataSource
            'dt = Sqldatasource1
            'dt.DataBind()
    
            ''code to dynamically create dropdownlist
            'Dim I As Integer
            'Dim J As Integer
            'For I = 1 To txtNumber.Text
            '    Dim MyDDL = New DropDownList
            '    MyDDL.ID = "ddlDynamic" & I
            '    For J = 1 To 3
            '        Dim MyLI As New ListItem
            '        'MyLI.Text = "Control Number: " & I & "-" & J
            '        'MyLI.Value = I & J
    
            '        MyDDL.Items.Add(MyLI)
            '        ''MyDDL.Items.Add(dt)
            '        'MyDDL.DataSourceID = Sqldatasource1.ID
            '        'MyDDL.DataBind()
            '    Next
            '    form1.Controls.Add(MyDDL)
            '    Dim MyLiteral = New LiteralControl
            '    MyLiteral.Text = "<BR><BR>"
            '    form1.Controls.Add(MyLiteral)
            'Next
    
    
    
            '''''code to dynamically create dropdownlist with data bounded to dropdownlist
            Dim I As Integer
            'Dim J As Integer
            For I = 1 To txtNumber.Text
                Dim MyDDL As New DropDownList
                MyDDL.ID = "ddlDynamic" & I
                form1.Controls.Add(MyDDL)
                MyDDL.DataSourceID = Sqldatasource1.ID
                'MyDDL.AutoPostBack = True
    
                MyDDL.DataValueField = "agency_name"
                MyDDL.DataBind()
                Dim MyLiteral = New LiteralControl
                MyLiteral.Text = "<BR><BR>"
                form1.Controls.Add(MyLiteral)
    
    
            Next
            'Dim i As Integer
            'For I = 1 To txtNumber.Text
            '    Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddldynamic1"), DropDownList)
            '    'dropdownlist1.AutoPostBack = True
    
            '    Response.Write(dropdownlist1.SelectedValue)
    
    
            '    'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            'Next
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
    
            For i = 1 To txtNumber.Text
                Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddldynamic1"), DropDownList)
    
                Response.Write(dropdownlist1.SelectedValue)
    
    
                'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            Next
        End Sub
    
    Quote Originally Posted by jmurrayhead View Post
    You'll have to create an instance and use FindControl like so:

    Code:
    Dim DropDownList1 As DropDownList = CType(Me.FindControl("ddlDynamic1"), DropDownList)
    Response.Write(DropDownList1.SelectedValue)
    

  8. #8
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Code:
            For i = 1 To txtNumber.Text
                Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddldynamic" & i), DropDownList)
     
                Response.Write(dropdownlist1.SelectedValue)
     
     
                'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            Next
    
    Do you mean like how the above will output?
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  9. #9
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    Kind of, i'm trying to use the button1 click event to display the selectedvalues in each dropdownlist and I need them to be separated.

    aspx.vb code:
    Code:
     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
    
            For i = 1 To txtNumber.Text
                Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddlDynamic" & i), DropDownList)
    
                Response.Write(dropdownlist1.SelectedValue)
    
    
                'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            Next
        End Sub
    
    but when i hit submit I get an error now:
    Server Error in '/questionaire' Application.
    --------------------------------------------------------------------------------

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:


    Line 87: Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddlDynamic" & i), DropDownList)
    Line 88:
    Line 89: Response.Write(dropdownlist1.SelectedValue)
    Line 90:
    Line 91:


    Source File: C:\Documents and Settings\Peebman2000\Desktop\wizard\questionaire\d ropdownlistdynmc.aspx.vb Line: 89

    Stack Trace:


    [NullReferenceException: Object reference not set to an instance of an object.]
    dropdownlistdynmc.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Peebman2000\Desktop\wizard\questionaire\d ropdownlistdynmc.aspx.vb:89
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
    System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +107
    System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +7
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +11
    System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746




    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433
    Any ideas on what i'm doing wrong?

    Quote Originally Posted by jmurrayhead View Post
    Code:
            For i = 1 To txtNumber.Text
                Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddldynamic" & i), DropDownList)
     
                Response.Write(dropdownlist1.SelectedValue)
     
     
                'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            Next
    
    Do you mean like how the above will output?

  10. #10
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    That's odd. I just tested it and it works perfectly for me.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


Closed Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. update gridview dynamically
    By peebman2000 in forum .Net Development
    Replies: 27
    Last Post: May 8th, 2008, 10:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO