Closed Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 15 of 15

Thread: dynamically created dropdowns insert to sql

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

    reply

    I don't know, here's my code from the top to the bottom. I don't know what i'm doing wrong.

    code:
    Code:
    Imports System.Data.SqlClient
    Imports System.Web.Configuration
    Imports System.Configuration.Configuration
    Imports System.IO
    Imports System.Text
    Imports System.IO.stream
    Imports System.data
    Imports System.Net.Mail
    Imports System.Net.NetworkCredential
    Imports System.Text.regularexpressions
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.UserControl
    Imports System.Web.UI.WebControls
    Imports System.Web.Management
    Imports System.Diagnostics.Process
    Partial Class dropdownlistdynmc
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        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("ddldynamic" & i), DropDownList)
    
                Response.Write(dropdownlist1.SelectedValue)
    
    
                'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            Next
        End Sub
    End Class
    

    Quote Originally Posted by jmurrayhead View Post
    That's odd. I just tested it and it works perfectly for me.

  2. #12
    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
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Comment out all of the literal stuff and see if it works.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


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

    reply

    Thanks, I commented out the literal code and i still get the error.

    Code:
    Imports System.Data.SqlClient
    Imports System.Web.Configuration
    Imports System.Configuration.Configuration
    Imports System.IO
    Imports System.Text
    Imports System.IO.stream
    Imports System.data
    Imports System.Net.Mail
    Imports System.Net.NetworkCredential
    Imports System.Text.regularexpressions
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.UserControl
    Imports System.Web.UI.WebControls
    Imports System.Web.Management
    Imports System.Diagnostics.Process
    Partial Class dropdownlistdynmc
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        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("ddldynamic" & i), DropDownList)
    
                Response.Write(dropdownlist1.SelectedValue)
    
    
                'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            Next
        End Sub
    End Class
    

    Quote Originally Posted by jmurrayhead View Post
    Comment out all of the literal stuff and see if it works.

  4. #14
    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
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Try this, instead:

    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(form1.FindControl("ddldynamic" & i), DropDownList)
            If DropDownList1 Is Nothing Then
                Response.Write("Dropdown was not found<br />")
            Else
                Response.Write(dropdownlist1.SelectedValue)
            End If
     
                'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            Next
        End Sub
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


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

    reply

    thanks jmurray, I got this code below that worked. It somehow captures the dropdownlist values. I added the database code or insert statement this morning and it stores the dropdown values in sql. I'm going to use this, but as always dude, thanks for your help.


    code i got:
    Code:
    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    
            'CODEGEN: This method call is required by the Web Form Designer
    
            'Do not modify it using the code editor.
    
            InitializeComponent()
    
            Me.createDDLs()
    
        End Sub
    
        Public Sub MyDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim ddl As DropDownList = TryCast(sender, DropDownList)
    
            'Response.Write(ddl.SelectedItem.Text.ToString())
    
            ddl.DataBind()
    
        End Sub
    
        Public Sub createDDLs()
    
            Dim num As Integer = 0
    
            If Not IsNothing(Request.Item("txtNumber")) Then
    
                num = CType(Request.Item("txtNumber"), Integer)
    
            End If
    
            Me.Panel1.Controls.Clear()
    
            Dim I As Integer
    
            For I = 1 To num
    
                Dim MyDDL As New DropDownList
    
                MyDDL.ID = "ddlDynamic" & I
    
                AddHandler MyDDL.SelectedIndexChanged, AddressOf MyDDL_SelectedIndexChanged
    
                'MyDDL.AutoPostBack = True
    
                Panel1.Controls.Add(MyDDL)
    
                MyDDL.DataSourceID = SqlDataSource1.ID
    
                MyDDL.DataValueField = "agency_name"
    
                MyDDL.DataBind()
    
                Dim MyLiteral = New LiteralControl
    
                MyLiteral.Text = "<BR><BR>"
    
                Panel1.Controls.Add(MyLiteral)
    
            Next
    
            IterateThroughChildren(Panel1)
    
            'Dim dynDDL As String() = Session("controls").ToString.Split(",")
    
    
    
        End Sub
    
    
        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    
            If Trim(txtNumber.Text) <> "0" And Not String.IsNullOrEmpty(txtNumber.Text) Then
    
                Dim i As Integer
    
                Dim numOfDDL As Integer
    
                numOfDDL = txtNumber.Text
    
                Dim ddlSelected As String
    
                For i = 1 To numOfDDL
    
                    ddlSelected = Request.Item("ddlDynamic" & i).ToString
                    'Dim dropdownlist1 As DropDownList = CType(Me.FindControl("ddldynamic" & i), DropDownList)
                    'dropdownlist1.Items.FindByText(ddlSelected).Selected = True
                    Dim dataadapter As SqlDataAdapter
                    Dim datacommand As SqlCommand
    
                    dataadapter = New SqlDataAdapter
                    datacommand = New SqlCommand
    
                    datacommand.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings("peebman").ToString)
                    datacommand.CommandText = "Insert into dbo.title (testname, testtile) values('" & txtNumber.Text & "','" & ddlSelected & "')"
                    'Response.Write(Split(ddlSelected))
                    'Response.Write("Insert Into Table(column) values('" & ddlSelected & "')")
    
                    datacommand.Connection.Open()
                    Try
                        datacommand.ExecuteNonQuery()
                    Catch ex As Exception
                        Response.Write(ex.Message.ToString() + " not working")
                        Response.End()
    
                    Finally
                        datacommand.Connection.Close()
                    End Try
    
    
                Next
    
            End If
    
        End Sub
    
        Private Sub IterateThroughChildren(ByVal parent As Control)
    
            For Each c As Control In parent.Controls
    
                ' ...do something...
    
                'If c.ID.Substring(0, 2) = "gv" Then
    
                'Response.Write(c.ID.ToString.Substring(0, 1))
    
                If c.GetType.ToString.Equals("System.Web.UI.WebControls.DropDownList") Then
    
                    Session("controls") += c.ID + ","
    
                End If
    
                'End If
    
                If c.[GetType]().ToString().Equals("System.Web.UI.WebControls.TextBox") AndAlso c.ID Is Nothing Then
    
                End If
    
                If c.Controls.Count > 0 Then
    
                    IterateThroughChildren(c)
    
                End If
    
            Next
    
        End Sub
    
        Protected Sub txtNumber_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNumber.TextChanged
    
            btnSave.Visible = True
    
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            txtNumber.Focus()
    
        End Sub
    
    Quote Originally Posted by jmurrayhead View Post
    Try this, instead:

    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(form1.FindControl("ddldynamic" & i), DropDownList)
            If DropDownList1 Is Nothing Then
                Response.Write("Dropdown was not found<br />")
            Else
                Response.Write(dropdownlist1.SelectedValue)
            End If
     
                'Response.Write(form1.Controls(ddlDynamic1.selectedindex.value))
            Next
        End Sub
    

Closed Thread
Page 2 of 2 FirstFirst 1 2

Similar Threads

  1. update gridview dynamically
    By peebman2000 in forum .NET Development
    Replies: 27
    Last Post: May 8th, 2008, 11: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