views:

57

answers:

2

Here's what I have. User enters values into text boxes (personal information etc.) and then presses a save changes button. The values in these text boxes get stored in an SQL database.

The problem I have is that when updating the database using the values from the text boxes, the page refreshes and the values in the text boxes are lost (or rather they return to the values that are already in the database as the data from the database is loaded into the text boxes on Page_Load).

When I update the database using valuse stored in variables it all works fine. What is the best way to update with the values from the text boxes?

+1  A: 

Use asp:FormView and your bind your asp:TextBox with columns you need.

For example:

<asp:FormView runat="server" ID="FormView1" DataSourceID="SqlDataSource1">
    <InsertItemTemplate>
        <table>
            <tr>
                <td>
                    <asp:TextBox runat="server" ID="txtFoo" Text='<%# Bind("foo") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox runat="server" ID="txtBar" Text='<%# Bind("bar") %>' />
                </td>
            </tr>
        </table>
    </InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" InsertCommand="INSERT INTO table1 (foo, bar) VALUES (@foo, @bar)" ConnectionString="<%$ ConnectionStrings:MyConnStringNameFromWebConfig%>">
     <InsertParameters>
         <asp:FormParameter Name="foo" FormField="foo" DbType="String" />
         <asp:FormParameter Name="bar" FormField="bar" DbType="String" />
     </InsertParameters>
</asp:SqlDataSource>

asp:FormView has a number of strange behavior moments, I will be happy to share my experience, please fill free to ask your questions

abatishchev
can you explain this a bit more please?
Sir Graystar
@Sir just a minute please
abatishchev
+1  A: 

It sounds like that you are loading the data in Page_Load without checking if the page is post back. If this is the case try the following:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindYouDataFromDB();
    }
}
Teddy
hi, when I do this although the datatable is filled on page load when I go to update the datatable it says it has no rows?
Sir Graystar
You have to call the Bind method every time you change the data and it needs to refresh the data from the database.
Teddy