Solved

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

Posted on 2008-06-22
6
2,590 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
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:kGenius
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Expert Comment

by:kGenius
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now