Hi i want solution for following problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mayur Vartak

    #1

    Hi i want solution for following problem.

    Hello i am developing an application in which user's data loaded on form load.so i keep that in page_load event. but when i try to save updated data using save_click event page_load event fires & previous data(same data) is inserted in database. so i want some other option to do this task.
    please help me....
    Code:
     protected void btnSave_Click(object sender, EventArgs e)
            {
                var newuser = new UserDetail
                {
                    firstname = txtFName.Text,
                    lastame = txtLName.Text,
                    address = txtAdd.Text,
                    mobno = txtMobNo.Text,
                    aboutme = txtAbtMe.Text,
                    
                };
                db.UserDetails.InsertOnSubmit(newuser);       
              } 
    
     protected void Page_Load(object sender, EventArgs e)
            {
                UN = Session["MySession"].ToString();
    
                var query = from c in db.UserDetails
                            where c.username == UN
                            select new
                            {
                                firstname =c.firstname,
                                lastame=c.lastame,
                                address=c.address,
                                mobno=c.mobno,
                                aboutme=c.aboutme
                            };
    
                foreach (var valuesFromQuery in query)
                {
     txtFName.Text = valuesFromQuery.firstname;
                    txtLName.Text = valuesFromQuery.lastame;
                    txtAdd.Text = valuesFromQuery.address;
                    txtMobNo.Text = valuesFromQuery.mobno.ToString();
                    txtAbtMe.Text = valuesFromQuery.aboutme; 
                } 
            }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Only load the data the first time the page is loaded. You can accomplish this by checking the Page.IsPostback property. This property indicates whether or not a control on the page has "posted back" to the server for server side processing...if it is False then it is the first time the page is loading.

    Try this:
    Code:
     protected void Page_Load(object sender, EventArgs e)
            {
                UN = Session["MySession"].ToString();
                if(IsPostBack == false){
                  var query = from c in db.UserDetails
                            where c.username == UN
                            select new
                            {
                                firstname =c.firstname,
                                lastame=c.lastame,
                                address=c.address,
                                mobno=c.mobno,
                                aboutme=c.aboutme
                            };
    
                  foreach (var valuesFromQuery in query)
                  {
                      txtFName.Text = valuesFromQuery.firstname;
                      txtLName.Text = valuesFromQuery.lastame;
                      txtAdd.Text = valuesFromQuery.address;
                      txtMobNo.Text = valuesFromQuery.mobno.ToString();
                      txtAbtMe.Text = valuesFromQuery.aboutme; 
                  } 
              }
            }

    Comment

    Working...