need some assistance. I am currently learning vb.net with sql.

I have two textboxes (txtValue and txtText) , a dropdownlist (ddl1) and save button (btnAdd).
I insert value to txtValue and txtText (eg: txtValue: A and txtText: Apple).
when i click button Add, The value will display in dropdownlist (like this; A - Apple) and both data will be save in the sqlserver (db).

im having issue where when i click button Add, the data is not display in the dropdownlist. But when i refresh my browser, the data will display in dropdownlist.

ASP.net

     <asp:DropDownList ID="ddl1" runat="server" CssClass="form-control" AutoPostBack="true"></asp:DropDownList>

      <label class="form-control-static">Value</label>
      <asp:TextBox ID="txtValue" runat="server" CssClass="form-control"></asp:TextBox>
       <label class="form-control-static">Text</label>
       <asp:TextBox ID="txtText" runat="server" CssClass="form-control"></asp:TextBox>

       <button id="btnAdd" runat="server" style="min-width: 80px;">Add</button>

VB.Net

 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Page.IsPostBack = False Then

            Dim rs As DataSet


            ddl1.Items.Clear()
            ddl1.Items.Add("")
            rs = db.ExecuteSelect("SELECT value, text FROM dropdownlist ", Session("CnnStr").ToString)
            For i = 0 To rs.Tables(0).Rows.Count - 1

                ddl1.Items.Add(rs.Tables(0).Rows(i).Item("value").ToString & " - " & rs.Tables(0).Rows(i).Item("text").ToString)

            Next
        End If
    End Sub



    Protected Sub btnAdd_ServerClick(sender As Object, e As EventArgs) Handles btnAdd.ServerClick
        db.ExecuteInsert("INSERT INTO dropdownlist (value, text ) VALUES( " & _
                             "'" & db.sqlstr(txtValue.Text) & "'," & _
                            "'" & db.sqlstr(txtText.Text) & "') ", _
                             Session("CnnStr").ToString)

    End Sub

From my opinion to load data to the dropdownlist ddl1 do this job in seperate sub procedure and call it from where you desire.

The sub procedure to load data is following

Private sub LoadDropdownList()
    Dim rs As DataSet
    ddl1.Items.Clear()
    ddl1.Items.Add("")
    rs = db.ExecuteSelect("SELECT value, text FROM dropdownlist ", Session("CnnStr").ToString)
    For i = 0 To rs.Tables(0).Rows.Count - 1
       ddl1.Items.Add(rs.Tables(0).Rows(i).Item("value").ToString & " - " & rs.Tables(0).Rows(i).Item("text").ToString)
    Next


End Sub

And the codes for page are

 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            Me.LoadDropdownList()
        End If
 End Sub

    Protected Sub btnAdd_ServerClick(sender As Object, e As EventArgs) Handles btnAdd.ServerClick
        db.ExecuteInsert("INSERT INTO dropdownlist (value, text ) VALUES( " & _
                             "'" & db.sqlstr(txtValue.Text) & "'," & _
                            "'" & db.sqlstr(txtText.Text) & "') ", _
                             Session("CnnStr").ToString)

        Me.LoadDropdownList()
    End Sub

Hope it can help you.

Thanks ! It works!
would you mind explain me the Me.

UPDATE:

okie - got the explanation:
From the Me documentation on MSDN:

The Me keyword behaves like either an object variable or a structure variable referring to the current instance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.