Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I refresh a dropdownlist after inserting data into a database

Posted on 2008-06-22
6
Medium Priority
?
2,707 Views
Last Modified: 2011-09-20
A have a tab panel in asp.net (w/vb.net backend) that has a dropdownlist of employer names and disabled text boxes with employer information (address, etc.) beneath it. This is bound to an SQL Server 2005 Express table "Employer". There is also a button labeled "New Employer".

If the user checks the dropdownlist and their employer is not listed:
1. The user clicks the "New Employer" button and a modal dialog appears.
2. The user enters the information (employer name and address information) and clicks "Submit".
3. The information is written to the database and the dialog is closed.
4. The dropdownlist is updated and the dropdownlist has the new item included in the list.

I am using a third party control (WestWind wwModalDialog) and callback. The problem is that while the
data is being written to the database and I am calling DataBind() after, the dropdownlist is not updated. I added breaks and traced the data and found that my call to rebind the dropdownlist does include the new data, but the original data is still loaded in.

If I add a button called "Refresh" that clears/binds the dropdwon, clicking that updates the dropdownlist correctly. I am thinking that since it is in an updatepanel, that may be stopping it from updating, but I don't see how that would affect it since the "Refresh" button does it. Is there a way to perhaps add the button and have it called after the database update is complete? I do not need to use the wwModalDialog control; I can use a modal webpage as well.

The code is what I believe are the relevant pieces. Thank you.

-----------------------------------------
<script>
    function OnMessageBox2Click(arg, context)
    {
        if (arg == 1) 
        {
            var Name = document.getElementById("<%=txtNewEmpName.ClientID%>").value;
	    var Addr = document.getElementById("<%=txtNewEmpAddress.ClientID%>").value
	    var City = document.getElementById("<%=txtNewEmpCity.ClientID%>").value
	    var Zip = document.getElementById("<%=txtNewEmpPostalCode.ClientID%>").value
	    var x = document.getElementById("<%=ddlNewEmpState.ClientID%>")
            var State = x.options[x.selectedIndex].value;
            Proxy.ReturnNewEmployer(Name,Addr,City,State,Zip,ReturnNewEmployer_Callback,OnPageError);
        }
        else
        {
            //alert ("ok");
        }
    }    
        
    function ReturnNewEmployer_Callback(Result)
    {
        alert(Result);
    }    
    
    function OnPageError(Result)
    {
        alert("*** OnPageError captured a CallbackMethod error:\r\n\r\n" + Result.message);
    }
</script>
 
-----------------------------------------
            <ajaxToolkit:TabPanel runat="server" ID="tabpnlEmployer" Enabled="true" HeaderText="Employer"
                Width="806px">
                <ContentTemplate>
                    <asp:UpdatePanel ID="upPnlEmployer" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <table border="0" cellpadding="2" cellspacing="0" style="width: 100%; background-color: #ffffff;">
                                <tr style="background-color: Blue; width: 100%">
                                    <td align="center" colspan="3">
                                        <font style="color: White"><b>Employment Information</b></font>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" valign="middle">
                                        Employer:
                                    </td>
                                    <td valign="top">
                                        <asp:TextBox ID="txtCompanyID" runat="server" Style="visibility: hidden; display: none;" />
                                        <asp:TextBox ID="txtEmpNameNew" runat="server" Style="visibility: hidden; display: none;" />
                                        <asp:TextBox ID="txtEmpAddressNew" runat="server" Style="visibility: hidden; display: none;" />
                                        <asp:TextBox ID="txtEmpCityNew" runat="server" Style="visibility: hidden; display: none;" />
                                        <asp:TextBox ID="txtEmpStateNew" runat="server" Style="visibility: hidden; display: none;" />
                                        <asp:TextBox ID="txtEmpZipNew" runat="server" Style="visibility: hidden; display: none;" />
                                        <asp:DropDownList ID="ddlEmployer" runat="server" DataSourceID="dsEmployer" DataTextField="Name"
                                            DataValueField="ID" AutoPostBack="True" AppendDataBoundItems="True" />
                                        <asp:SqlDataSource ID="dsEmployer" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>"
                                            SelectCommand="SELECT ID, Name FROM Employer WHERE (CompanyID = @CompanyID) or (@userType = 1 or @userType2  = 2 ) union select 0 as ID, '' as Name  ORDER BY Name"
                                            InsertCommand="INSERT INTO [Employer] ([CompanyID], [Name], [Address], [City], [State],[Zip]) VALUES (@CompanyID, @Name, @Address, @City, @State, @Zip)">
                                            <SelectParameters>
                                                <asp:SessionParameter Name="CompanyID" SessionField="CompanyID" Type="Int32" />
                                                <asp:SessionParameter Name="userType" SessionField="UserType" />
                                                <asp:SessionParameter Name="userType2" SessionField="UserType" />
                                            </SelectParameters>
                                            <InsertParameters>
                                                <asp:SessionParameter Name="CompanyID" SessionField="txtCompanyID" Type="Int32" />
                                                <asp:SessionParameter Name="Name" SessionField="txtEmpNameNew" Type="String" />
                                                <asp:SessionParameter Name="Address" SessionField="txtEmpAddressNew" Type="String" />
                                                <asp:SessionParameter Name="City" SessionField="txtEmpCityNew" Type="String" />
                                                <asp:SessionParameter Name="State" SessionField="txtEmpStateNew" Type="String" />
                                                <asp:SessionParameter Name="Zip" SessionField="txtEmpZipNew" Type="String" />
                                            </InsertParameters>
                                        </asp:SqlDataSource>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" style="height: 75px" valign="top">
                                        Emp. Address:
                                    </td>
                                    <td valign="top">
                                        <asp:TextBox ID="txtEmpAddress" runat="server" Columns="33" CssClass="tupper" MaxLength="2000"
                                            Rows="4" TextMode="MultiLine" Width="188px" Enabled="false"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" valign="middle">
                                        Emp. City:
                                    </td>
                                    <td valign="top">
                                        <asp:TextBox ID="txtEmpCity" runat="server" Columns="40" CssClass="tupper" MaxLength="50"
                                            Width="188px" Enabled="false"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" valign="middle">
                                        Emp. State:
                                    </td>
                                    <td valign="top">
                                        <asp:DropDownList ID="ddlEmpState" runat="server" DataSourceID="dsEmpState" DataTextField="Name"
                                            DataValueField="Abbreviation" Enabled="false">
                                        </asp:DropDownList>
                                        <asp:SqlDataSource ID="dsEmpState" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>"
                                            SelectCommand="SELECT [Abbreviation], [Name] FROM [State] ORDER BY [Name]"></asp:SqlDataSource>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" valign="middle">
                                        Emp. Postal Code:
                                    </td>
                                    <td valign="top">
                                        <asp:TextBox ID="txtEmpPostalCode" runat="server" Columns="10" CssClass="tupper"
                                            MaxLength="10" Enabled="false"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" valign="middle">
                                        DOT#:
                                    </td>
                                    <td valign="top">
                                        <asp:TextBox ID="txtDOT" runat="server" Columns="45" CssClass="tupper" Rows="5" Width="188px" />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" style="height: 75px" valign="top">
                                        Job Function:
                                    </td>
                                    <td valign="top">
                                        <asp:TextBox ID="txtJobFunction" runat="server" Columns="45" CssClass="tupper" Rows="5"
                                            TextMode="MultiLine" Width="188px"></asp:TextBox>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                    <td>
                                        <input type="button" id="btnNewEmployer" name="btnNewEmployer" value="New Employer"
                                            onclick="MessageBox2.showDialog();" />
                                        <ww:wwModalDialog ID="MessageBox2" runat="server" ContentId="MessageBox2Content"
                                            HeaderId="MessageBox2Content" OverlayId="overlay" BackgroundOpacity=".70" OkButtonId="MessageBox2Ok"
                                            CancelButtonId="MessageBox2Cancel" OnClientDialogClick="OnMessageBox2Click" FadeinBackground="true"
                                            DragHandleID="MessageBox2Header" Draggable="true" Closable="true" ShadowOffset="6"
                                            ShadowOpacity=".60" Style="background: white; display: none;" CssClass="blackborder"
                                            Width="400px">
                                            <div id='MessageBox2Header' class='gridheader' style="padding: 2px;">
                                                New Employer</div>
                                            <div style='padding: 10px;'>
                                                <div id='MessageBox2Content'>
                                                    <table border="0" cellpadding="2" cellspacing="0" style="width: 100%; background-color: #ffffff;">
                                                        <tr>
                                                            <td>
                                                                &nbsp;
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right" valign="middle">
                                                                Employer:
                                                            </td>
                                                            <td valign="top">
                                                                <asp:TextBox ID="txtNewEmpName" Name="txtNewEmpName" runat="server" Columns="45"
                                                                    CssClass="tupper" Rows="5" Width="188px" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right" valign="middle">
                                                                Emp. Address:
                                                            </td>
                                                            <td valign="top">
                                                                <asp:TextBox ID="txtNewEmpAddress" Name="txtNewEmpAddress" runat="server" Columns="33"
                                                                    CssClass="tupper" MaxLength="2000" Rows="4" TextMode="MultiLine" Width="188px" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right" valign="middle">
                                                                Emp. City:
                                                            </td>
                                                            <td valign="top">
                                                                <asp:TextBox ID="txtNewEmpCity" Name="txtNewEmpCity" runat="server" Columns="40"
                                                                    CssClass="tupper" MaxLength="50" Width="188px" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right" valign="middle">
                                                                Emp. State:
                                                            </td>
                                                            <td valign="top">
                                                                <asp:DropDownList ID="ddlNewEmpState" Name="ddlNewEmpState" runat="server" DataSourceID="dsNewEmpState"
                                                                    DataTextField="Name" DataValueField="Abbreviation" />
                                                                <asp:SqlDataSource ID="dsNewEmpState" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStr %>"
                                                                    SelectCommand="SELECT [Abbreviation], [Name] FROM [State] ORDER BY [Name]" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right" valign="middle">
                                                                Emp. Postal Code:
                                                            </td>
                                                            <td valign="top">
                                                                <asp:TextBox ID="txtNewEmpPostalCode" Name="txtNewEmpPostalCode" runat="server" Columns="10"
                                                                    CssClass="tupper" MaxLength="10" />
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </div>
                                                <hr />
                                                <input id='MessageBox2Ok' type='button' value='Save Employer' />
                                                <input id='MessageBox2Cancel' type='button' value='Cancel' />
                                            </div>
                                        </ww:wwModalDialog>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                            </table>
                            </td> </tr>
                            <tr>
                                <td>
                                    &nbsp;
                                </td>
                            </tr>
                            </table>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="ddlEmployer" EventName="SelectedIndexChanged" />
                        </Triggers>
                    </asp:UpdatePanel>
                </ContentTemplate>
            </ajaxToolkit:TabPanel>
 
-----------------------------------------
    <CallbackMethod()> _
    Public Function ReturnNewEmployer(ByVal Name As String, ByVal Addr As String, ByVal City As String, ByVal State As String, ByVal Zip As String) As String
        Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connStr").ToString)
        Dim cmd As New SqlClient.SqlCommand()
        Dim dr As SqlClient.SqlDataReader
        Dim strMsg As String = ""
 
        Try
            txtCompanyID.Text = Session("CompanyID")
            txtEmpNameNew.Text = Trim(Name)
            txtEmpAddressNew.Text = Trim(Addr)
            txtEmpCityNew.Text = Trim(City)
            txtEmpStateNew.Text = Trim(State)
            txtEmpZipNew.Text = Trim(Zip)
 
            dsEmployer.InsertParameters(0).DefaultValue = txtCompanyID.Text
            dsEmployer.InsertParameters(1).DefaultValue = txtEmpNameNew.Text
            dsEmployer.InsertParameters(2).DefaultValue = txtEmpAddressNew.Text
            dsEmployer.InsertParameters(3).DefaultValue = txtEmpCityNew.Text
            dsEmployer.InsertParameters(4).DefaultValue = txtEmpStateNew.Text
            dsEmployer.InsertParameters(5).DefaultValue = txtEmpZipNew.Text
            dsEmployer.Insert()
 
            ddlEmployer.Items.Clear()
            ddlEmployer.SelectedValue = Nothing
            ddlEmployer.DataBind()
 
            strMsg = "Employer added to table."
        Catch ex As Exception
            strMsg = ex.Message.ToString
        Finally
            ReturnNewEmployer = strMsg
        End Try
    End Function

Open in new window

0
Comment
Question by:meegbear
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:kGenius
ID: 21840997
Try to put the DataBind() method n the SqlDataSource Inserted event
This MSDN sample shows how:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.inserted.aspx

HTH,
Karl
0
 

Author Comment

by:meegbear
ID: 21845152
I called DataBind() in dsEmployer_Inserted (dsEmployer being the SQLDataSource) and seeing the new entry was included in the bind, it was not listed in the dropdownlist.
0
 

Author Comment

by:meegbear
ID: 21846847
I did some mucking around in the code. I removed all the controls except the dropdownlist and the button to bring up the modal dialog and the UpdatePanel from the Employer tab and the same problem occurred. I then removed the tabs so everything displayed on one page and it still happens.

I then modified the SelectedIndexChanged event so it would clear the dropdownlist and rebind. That worked properly. Prehaps what I need to do is determine when the database insert is complete and call a sub to rebind.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:kGenius
ID: 21852518
Your SqlDatasource fires dsEmployer_Inserted() when the insert is complete.
If you can see (when debugging) that the new data is in your datasource, you can bind it to your dropdownlist.
Is there any function that redraws the dropdownlist (ie. ddlEmployer.Update() ) after the binding,  as you're using AJAX ... ?
0
 

Author Comment

by:meegbear
ID: 21854839
The dropdownlist is not redrawn after binding. A Page_Load is called when the dialog is closed, and this is called before the Inserted() call.

It seems like while the data to the database by the server, to the client, no events occurred.
0
 

Accepted Solution

by:
meegbear earned 0 total points
ID: 21868049
I solved it myself. I added a hidden button that would refresh the dropdownlist when clicked. I then added code to click the button in the ReturnNewEmployer_Callback.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question