value is save in database but is not showing in dropdownlist

autoXP

New member
Joined
Apr 26, 2015
Messages
1
Programming Experience
Beginner
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


VB.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
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


UPDATE : ALready figure it out. Just need to create a function for dropdownlist.

VB.NET:
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
 
Last edited:

Latest posts

Back
Top