locked
Cannot update data in access database through value entered in text control RRS feed

  • Question

  • User1988952462 posted

    Hi

    I'm new to asp.net so please bear with me. I'm just trying to retrieve information
    from an access database (Access 2010) and allow user to update it through a text control.
    I was able to retrieve the information and display it throug a text control but when i
    update the value on the text and click the save button, it does not update the information
    on the database. I don't know what i'm doing wrong. Any help is greatly appreciated.
    I'm using VS2010 Professional. Please see my code below:

    Default.aspx.cs

    public partial class _Default : System.Web.UI.Page
    {

           private DataSet ds = null;
           private OleDbDataAdapter da = null;
           private OleDbCommandBuilder comBuilder = null;
           DataRow dr = null;
           OleDbConnection con= null;
      
         protected void Page_Load(object sender, EventArgs e)
        {
            OpenDB();
            GetTextValue();
        }

         private void GetTextValue()
         {
               dr = ds.Tables[0].Rows[0];
               txtFName.Text = dr["FName"].ToString();
         }

        private void OpenDB()
        {
            string strCon = ConfigurationManager.ConnectionStrings["OleDbCon"].ToString();
            con = new OleDbConnection(strCon);

            int iID = 1;
         
            string strSQL = "SELECT ID, FName FROM Emptbl " +
                          " WHERE ID =" + iID ;
        
            da = new OleDbDataAdapter(strSQL, con);
            comBuilder = new OleDbCommandBuilder(da);
            ds = new DataSet();
            da.Fill(ds);
       }
      

        protected void btnSave_Click(object sender, EventArgs e)
        {
            dr = ds.Tables[0].Rows[0];
            dr["FName"] = txtFName.Text.ToString();
            da.Update(ds);
        }
    }

     

    Default.aspx


    <head runat="server">
        <title></title>
      
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            
            <table   style="width:100%">
                <tr>
                    <td>
                        <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
                     </td>
                </tr>
            </table>

             <table   style="width:100%">
                <tr>
                    <td style="width:10%"  >
                       First Name :
                     </td>
                     <td>
                         <asp:TextBox ID="txtFName" runat="server" AutoPostBack="True"></asp:TextBox>
                     </td>
                </tr>
            </table>
     
        </div>
        </form>
    </body>
    </html>

     

    Monday, March 28, 2011 10:53 AM

Answers

  • User-1995538749 posted

    Then just leave the 'GetTextValue' method call within the !IsPostBack condition or call the 'OpenDB' method first within your button handler.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 12:16 PM

All replies

  • User-1995538749 posted

    You're loading the TextBox with the value from the database each and every postback instead of just initially. Try enclosing your database method calls inside a !IsPostBack condition.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            OpenDB();
            GetTextValue();
        }
    }

     

    Monday, March 28, 2011 10:58 AM
  • User1988952462 posted

    Hi Ed,

    Thank you for your reply. I did change my code to :

    protected void Page_Load(object sender, EventArgs e) 
    { 
       
    if (!this.IsPostBack) 
       
    { 
           
    OpenDB(); 
           
    GetTextValue(); 
       
    } 
    }

    and it is now updating the value from the text box, but when i click the "Save" button, it seems that the program is not going

    through the "btnSave_Click" event as shown below.

      protected void btnSave_Click(object sender, EventArgs e)
        {
            dr = ds.Tables[0].Rows[0];
            dr["FName"] = txtFName.Text.ToString();
            da.Update(ds);
        }

    Am i missing something?

    Monday, March 28, 2011 11:27 AM
  • User-1995538749 posted

    Then just leave the 'GetTextValue' method call within the !IsPostBack condition or call the 'OpenDB' method first within your button handler.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 12:16 PM
  • User1988952462 posted

     

    Hi Ed,

    Thanks a lot for your reply. I have to delete the btnSave_Click event and retype it again and  is now working. I don't know why it was not working before.

    But i also modify my code as per your advice as shown below and it is now updating the data back to the database.

    protected void btnSave_Click(object sender, EventArgs e)
        {

            OpenDB();
            dr = ds.Tables[0].Rows[0];
            dr["FName"] = txtFName.Text.ToString();
            da.Update(ds);
        }

    Thank you again and i appreciate very much your help.

    Sincerely,

    ALC77

    PS: I also mark this thread as answered.

    Monday, March 28, 2011 12:33 PM