locked
VB: refresh drop down list after new data added to database - div visible true/false RRS feed

  • Question

  • User1444011213 posted

    Hi,

    I have on web page dropdown list that needs to be refreshed width data from MS SQL table after I add new value to the table.

    In short, when you click on "Add" next to the current ddl list current div becomes visible=false and new div with adding new list item becomes visible=true, here I add a new item in the database to be used in drop down list.

    After the new item is added in the database, div that is visible (the one for adding new value to the database) becomes visible=false and the first div with ddl list becomes visible=true, but in ddl list, there is no new value I just added to the database. I need to reload page completely to get new ddl value visible, can I do this without reloading a page?

    APSX
    <div id="divNewGuest" runat="server" visible="true" >
     <asp:Label ID="Label7" runat="server" Text="Document type" Width="150"/>
     <asp:DropDownList ID="ddlDocumentType" runat="server" Width="200" />
     <asp:LinkButton ID="lbAddDucumentType" runat="server" Text="add" />
    </div>
    <div id="divAddDocumentType" runat="server" visible="false">
     <asp:Label ID="Label2" runat="server" Text="New document type" Width="150"/>
     <asp:TextBox ID="tBoxAddNewDocumentType" runat="server" Width="200" />
     <asp:LinkButton ID="lbAddNewDocumentTypeConfirm" runat="server" Text="confirm" /> 
     <asp:LinkButton ID="lbAddNewDocumentTypeCancel" runat="server" Text="cancel" />
    </div>
    
    VB
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
     If Me.IsPostBack = True Then
      subDdlDocumentType()
     End If
     
     subDdlDocumentType()
    End Sub
    Private Sub subDdlDocumentType()
     Dim connectionString As String =  WebConfigurationManager.ConnectionStrings("XXX").ConnectionString
     Dim selectSQL As String = "SELECT ID_DOCUMENT_TYPE, NAME FROM tblGuestsDocumentType ORDER BY NAME"
     Dim con As New SqlConnection(connectionString)
     Dim cmd As New SqlCommand(selectSQL, con)
    
     Try
      con.Open()
    
      ddlDocumentType.DataSource = cmd.ExecuteReader()
      ddlDocumentType.DataValueField = "ID_DOCUMENT_TYPE"
      ddlDocumentType.DataTextField = "NAME"
      ddlDocumentType.DataBind()
     
    Catch Err As Exception
      lblGreska.Text = "Error reading list of names. subDdlDocumentType: "
      lblGreska.Text &= Err.Message
     Finally
      con.Close()
     End Try
    End Sub

    Thanks for helping :)

    Saturday, November 2, 2019 1:12 PM

Answers

  • User61956409 posted

    Hi westgatezagreb,

    If you debug your code, you would find that you do not rebind the DropDownList 'ddlDocumentType' after you insert new item(s) into your database. You can modify the code like below to fix it.

    Private Sub subClickLbAddNewDocumentTypeConfirm() Handles lbAddNewDocumentTypeConfirm.Click
        'code logic here
    
        Try
            con.Open()
            added = cmd.ExecuteNonQuery()
        Catch err As Exception
            lblGreska.Text &= "Error reading list of names. subClickLbAddNewDocumentTypeConfirm: "
            lblGreska.Text &= err.Message
        Finally
            con.Close()
    
            subDdlDocumentType()
        End Try
    
        divAddDocumentType.Visible = False
        divNewGuest.Visible = True
    End Sub

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 7, 2019 7:39 AM

All replies

  • User475983607 posted

    I need to reload page completely to get new ddl value visible, can I do this without reloading a page?

    Either reload the page or use an Update Panel.  You'll get a validation error if you try to add items to a data bound control using JavaScript.  You should use the tools that come with Web Forms.

    Saturday, November 2, 2019 4:40 PM
  • User-1780421697 posted

    Maybe this help you by declaring  web method and bind ddl by jquery ,so it will be easy to refresh the control by JS.

    https://surajdeshpande.wordpress.com/2013/09/28/fill-a-dropdownlist-using-jquery-in-asp-net/

    Saturday, November 2, 2019 7:42 PM
  • User61956409 posted

    Hi westgatezagreb,

    After the new item is added in the database, div that is visible (the one for adding new value to the database) becomes visible=false and the first div with ddl list becomes visible=true, but in ddl list, there is no new value I just added to the database.

    Do you rebind/repopulate DropDownList 'ddlDocumentType' after you insert new item(s) into your database? If you can share the relevant code snippet that you are using to adding new item to the database, so that we can understand and troubleshoot the issue better.

    With Regards,

    Fei Han

    Wednesday, November 6, 2019 9:49 AM
  • User1444011213 posted

    Hi,

    Full simplified code below:

    .aspx
    ==================
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:Label ID="lblGreska" runat="server" Text="" />
        <div class="container" style="margin: 0 auto; margin-bottom: 10px;" runat="server">
            <div id="divNewGuest" runat="server" visible="true" >
                <div class="col-sm-12" style="background-color: #949494; font-size: 1.5em; font-weight: bold; text-align: center;">
                    NEW GUEST
                </div>
                <div class="col-sm-6">
                    <asp:Label ID="Label7" runat="server" Text="Document type" Width="150"/>
                    <asp:DropDownList ID="ddlDocumentType" runat="server" Width="200" />
                    <asp:LinkButton ID="lbAddDucumentType" runat="server" Text="add" />
                </div>
            </div>
            <div id="divAddDocumentType" runat="server" visible="false">
                <div class="col-sm-12" style="background-color: #949494; font-size: 1.5em; font-weight: bold; text-align: center;">
                    NEW DOCUMENT TYPE
                </div>
                <div class="col-sm-6">
                    <asp:Label ID="Label2" runat="server" Text="New document type" Width="150"/>
                    <asp:TextBox ID="tBoxAddNewDocumentType" runat="server" Width="200" />
                    <asp:LinkButton ID="lbAddNewDocumentTypeConfirm" runat="server" Text="confirm" />
                    <asp:LinkButton ID="lbAddNewDocumentTypeCancel" runat="server" Text="cancel" ForeColor="#FAA41A"/>
                </div>
            </div>
        </div>
    </asp:Content>
    
    .vb
    ==================
    
    Imports System.Data.SqlClient
    Imports System.Web.Configuration
    
    Partial Class _test
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            subDdlDocumentType()
        End Sub
        Private Sub subClickLbAddNewDucumentType() Handles lbAddDucumentType.Click
            divNewGuest.Visible = False
            divAddDocumentType.Visible = True
        End Sub
        Private Sub subClickLbAddNewDucumentTypeCancel() Handles lbAddNewDocumentTypeCancel.Click
            divNewGuest.Visible = True
            divAddDocumentType.Visible = False
        End Sub
        Private Sub subDdlDocumentType()
            If Page.IsPostBack = True Then
                Dim connectionString As String = WebConfigurationManager.ConnectionStrings("xxx").ConnectionString
                Dim selectSQL As String = "SELECT ID_DOCUMENT_TYPE, NAME FROM tblGuestsDocumentType ORDER BY NAME"
                Dim con As New SqlConnection(connectionString)
                Dim cmd As New SqlCommand(selectSQL, con)
    
                Try
                    con.Open()
    
                    ddlDocumentType.DataSource = cmd.ExecuteReader()
                    ddlDocumentType.DataValueField = "ID_DOCUMENT_TYPE"
                    ddlDocumentType.DataTextField = "NAME"
                    ddlDocumentType.DataBind()
                Catch Err As Exception
                    lblGreska.Text = "Error reading list of names. subDdlDocumentType: "
                    lblGreska.Text &= Err.Message
                Finally
                    con.Close()
                End Try
            End If
        End Sub
        Private Sub subClickLbAddNewDocumentTypeConfirm() Handles lbAddNewDocumentTypeConfirm.Click
            Dim connectionString As String = WebConfigurationManager.ConnectionStrings("xxx").ConnectionString
            Dim insertSQL As String = ""
            insertSQL &= " INSERT INTO tblGuestsDocumentType"
            insertSQL &= " (NAME)"
            insertSQL &= " VALUES"
            insertSQL &= " (@NAME)"
            Dim con As New SqlConnection(connectionString)
            Dim cmd As New SqlCommand(insertSQL, con)
    
            cmd.Parameters.AddWithValue("@NAME", tBoxAddNewDocumentType.Text)
    
            Dim added As Integer = 0
    
            Try
                con.Open()
                added = cmd.ExecuteNonQuery()
            Catch err As Exception
                lblGreska.Text &= "Error reading list of names. subClickLbAddNewDocumentTypeConfirm: "
                lblGreska.Text &= err.Message
            Finally
                con.Close()
            End Try
    
            divAddDocumentType.Visible = False
            divNewGuest.Visible = True
        End Sub
    End Class
    

    Wednesday, November 6, 2019 10:04 AM
  • User288213138 posted

    Hi westgatezagreb,

    westgatezagreb

    there is no new value I just added to the database. I need to reload page completely to get new ddl value visible, can I do this without reloading a page?

    I have already answered you the same question in this thread. https://forums.asp.net/t/2161273.aspx

    If you have other questions, please let me know.

    Best regards,

    sam

    Thursday, November 7, 2019 7:11 AM
  • User61956409 posted

    Hi westgatezagreb,

    If you debug your code, you would find that you do not rebind the DropDownList 'ddlDocumentType' after you insert new item(s) into your database. You can modify the code like below to fix it.

    Private Sub subClickLbAddNewDocumentTypeConfirm() Handles lbAddNewDocumentTypeConfirm.Click
        'code logic here
    
        Try
            con.Open()
            added = cmd.ExecuteNonQuery()
        Catch err As Exception
            lblGreska.Text &= "Error reading list of names. subClickLbAddNewDocumentTypeConfirm: "
            lblGreska.Text &= err.Message
        Finally
            con.Close()
    
            subDdlDocumentType()
        End Try
    
        divAddDocumentType.Visible = False
        divNewGuest.Visible = True
    End Sub

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 7, 2019 7:39 AM
  • User1444011213 posted

    Hi,

    Thanks for this, I can belive the solution was so simple!

    Saturday, November 9, 2019 5:50 AM