findcontrol

Discussion in 'ASP .Net' started by nicholas, Dec 7, 2004.

  1. nicholas

    nicholas Guest

    I have a page on which several dropdownlists are placed dynamically.
    So, sometimes there are no ddl's and sometimes there a are let's say 10
    ddl's.

    These ddl's are created in my code.

    Now, that is no problem. The problem is that when I want to INSERT values
    INTO a table in my database, I should be able to get the value of the
    selecteditem of the dropdowns.
    And as these controls are not realy on the page, I always get an error.
    (object reference not set to an instance of an object)

    I tried several combinations with findcontrol, but nothing works...

    Any help is appreciated ! THX

    Here is 1 of my tries:

    ############# code ########
    if txt_counter.text > 0 then 'this tells me how
    much dropdownlists were created on page load (they were named: dd_1, dd_2,
    etc)

    Dim counter as integer

    for counter=1 to txt_counter.text

    Dim mydropdown1 As dropdownlist =
    Page.FindControl("dd_" & counter)

    If Not mydropdown1 Is Nothing Then

    'insert the listproductoptions
    Dim dbComm1 As New SQLCommand()
    dbComm1.CommandType = CommandType.Text
    dbComm1.CommandText = "INSERT INTO
    tbl_listproductsoptions(listproductID, optionID) VALUES (@listproductID,
    @optionID)"
    dbComm1.Connection = MyConnection



    dbComm1.Parameters.Add("@listproductID",SqlDbType.int)

    dbComm1.Parameters.Add("@optionID",SqlDbType.int)


    dbComm1.Parameters("@listproductID").Value =
    intNewID
    dbComm1.Parameters("@optionID").Value =
    mydropdown1.selecteditem.value


    'MyConnection.Open()
    dbComm1.ExecuteScalar()

    end if

    counter = counter + 1

    Next

    end if
    nicholas, Dec 7, 2004
    #1
    1. Advertising

  2. nicholas

    nicholas Guest

    problem solved.

    request.form("dd_" & counter)



    "nicholas" <> wrote in message
    news:...
    > I have a page on which several dropdownlists are placed dynamically.
    > So, sometimes there are no ddl's and sometimes there a are let's say 10
    > ddl's.
    >
    > These ddl's are created in my code.
    >
    > Now, that is no problem. The problem is that when I want to INSERT values
    > INTO a table in my database, I should be able to get the value of the
    > selecteditem of the dropdowns.
    > And as these controls are not realy on the page, I always get an error.
    > (object reference not set to an instance of an object)
    >
    > I tried several combinations with findcontrol, but nothing works...
    >
    > Any help is appreciated ! THX
    >
    > Here is 1 of my tries:
    >
    > ############# code ########
    > if txt_counter.text > 0 then 'this tells me

    how
    > much dropdownlists were created on page load (they were named: dd_1, dd_2,
    > etc)
    >
    > Dim counter as integer
    >
    > for counter=1 to txt_counter.text
    >
    > Dim mydropdown1 As dropdownlist =
    > Page.FindControl("dd_" & counter)
    >
    > If Not mydropdown1 Is Nothing Then
    >
    > 'insert the listproductoptions
    > Dim dbComm1 As New SQLCommand()
    > dbComm1.CommandType = CommandType.Text
    > dbComm1.CommandText = "INSERT INTO
    > tbl_listproductsoptions(listproductID, optionID) VALUES (@listproductID,
    > @optionID)"
    > dbComm1.Connection = MyConnection
    >
    >
    >
    > dbComm1.Parameters.Add("@listproductID",SqlDbType.int)
    >
    > dbComm1.Parameters.Add("@optionID",SqlDbType.int)
    >
    >
    > dbComm1.Parameters("@listproductID").Value

    =
    > intNewID
    > dbComm1.Parameters("@optionID").Value =
    > mydropdown1.selecteditem.value
    >
    >
    > 'MyConnection.Open()
    > dbComm1.ExecuteScalar()
    >
    > end if
    >
    > counter = counter + 1
    >
    > Next
    >
    > end if
    >
    >
    nicholas, Dec 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Durzi

    OnEditCommand - .FindControl Returning Null

    George Durzi, Jul 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    474
    George Durzi
    Jul 14, 2003
  2. Ravikanth[MVP]

    FindControl

    Ravikanth[MVP], Jul 18, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    438
    Yan-Hong Huang[MSFT]
    Jul 22, 2003
  3. James G. Beldock
    Replies:
    1
    Views:
    9,293
    Kevin Spencer
    Aug 7, 2003
  4. Ivan Demkovitch
    Replies:
    1
    Views:
    806
    Ivan Demkovitch
    Oct 7, 2003
  5. ani

    FindControl method doesn't work

    ani, Nov 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    358
Loading...

Share This Page