DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

dynamically created dropdowns insert to sql

This is a discussion on dynamically created dropdowns insert to sql within the .Net Development forums, part of the Programming & Scripting category; Hey everyone its peebman2000, currently still building the questionnaire vb.net app for my client. I've never worked with creating dynamic ...

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

  #1  
Old May 16th, 2008, 10:27 AM
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 207
Rep Power: 2
peebman2000 is on a distinguished road
Question 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  
Old May 16th, 2008, 10:32 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Couldn't you just use: ddlDynamic1.SelectedIndex.Value to get the value and then append it to your query?
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

  #3  
Old May 16th, 2008, 11:09 AM
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 207
Rep Power: 2
peebman2000 is on a distinguished road
Question 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  
Old May 16th, 2008, 11:11 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

From looking at this line:

Code:
MyDDL.ID = "ddlDynamic" & I
ddlDynamic will have an integer after it...so:
Code:
Response.Write(ddlDynamic1.selectedindex.value)
  #5  
Old May 16th, 2008, 11:18 AM
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 207
Rep Power: 2
peebman2000 is on a distinguished road
Question reply

Thanks, I tried that and it still says:
Quote:
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  
Old May 16th, 2008, 11:25 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

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)
  #7  
Old May 16th, 2008, 12:47 PM
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 207
Rep Power: 2
peebman2000 is on a distinguished road
Question 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  
Old May 16th, 2008, 01:02 PM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

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?
  #9  
Old May 16th, 2008, 01:27 PM
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 207
Rep Power: 2
peebman2000 is on a distinguished road
Question 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:
Quote:
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  
Old May 16th, 2008, 01:43 PM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

That's odd. I just tested it and it works perfectly for me.
Closed Thread

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
update gridview dynamically peebman2000 .Net Development 27 May 8th, 2008 10:03 PM


All times are GMT -4. The time now is 05:15 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2