locked
CAnnot update a sql database from asp.net!! RRS feed

  • Question

  • User-1400214080 posted

    i found a way from codeproject using datareader. I used that in the page load event and the details are displayed correctly into the textboxes . But i wrote the button click event in which i have used an update statement. I have opened a new connection and exexutenonquery() has been used. The updation doesnt take place .Can some one point out why the button click doesnt fire. The code is given below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    
    public partial class view_profile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string str = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;";
            SqlConnection conn = new SqlConnection(str);
            string query="select fname,lname,userid,phone_no,address,email_id from users where userid='" +Session["fname"].ToString()+ "'";
            SqlCommand comm = new SqlCommand(query, conn);
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            while (dr.Read())
            {
               
                fname_tb.Text = dr.GetValue(0).ToString();
                lname_tb.Text = dr.GetValue(1).ToString();
                userid_tb.Text = dr.GetValue(2).ToString();
                phno_tb.Text = dr.GetValue(3).ToString();
                address_tb.Text = dr.GetValue(4).ToString();
                mail_tb.Text = dr.GetValue(5).ToString();
    
    
            }
            
    
    
    
        }
        protected void update_btn_Click(object sender, EventArgs e)
        {
            string str1 = "Data Source=MUKUNDANPC\\SQLEXPRESS;Initial Catalog=lms_db;Integrated Security=True;";
            SqlConnection conn1 = new SqlConnection(str1);
            string query1 = " update users set fname='" +fname_tb.Text+ "',lname='" +lname_tb.Text+ "',userid='" +userid_tb.Text+ "',phone_no='" +phno_tb.Text+ "',address='" +address_tb.Text+ "',email_id='" +mail_tb.Text+ "' where  userid='" +Session["fname"].ToString()+ "'";
            conn1.Open();
            SqlCommand comm1 = new SqlCommand(query1, conn1);
             comm1.ExecuteNonQuery();
            conn1.Close();
    
    
        }
    }


    Also after i click the button, the page refreshes and the old details are shown , which indicates that no updation has taken place . If possible please clarify that too.. Thanx in advance Smile

    Wednesday, May 9, 2012 12:46 PM

Answers

  • User-439404275 posted

    If you put a breakpoint in the button click event code, say on the line

    SqlConnection conn1 = new SqlConnection(str1);

    Does it come to a stop here ? If it doesn't, you need to post your asp code to see what might be the problem with your button.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 9, 2012 1:30 PM
  • User-1400214080 posted

    The solution was to enclose the code in the page load event in if(!IPostBack) . Otherwise whenver i click the button the page would be loaded with the old values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 9, 2012 11:14 PM

All replies

  • User-439404275 posted

    If you put a breakpoint in the button click event code, say on the line

    SqlConnection conn1 = new SqlConnection(str1);

    Does it come to a stop here ? If it doesn't, you need to post your asp code to see what might be the problem with your button.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 9, 2012 1:30 PM
  • User-1400214080 posted

    Yes it does come to a stop.

    Wednesday, May 9, 2012 1:32 PM
  • User-439404275 posted

    After it runs through the btn_click event, the page load event should run again getting the new values.  Does it ?

    Wednesday, May 9, 2012 1:42 PM
  • User-1400214080 posted

    Right. After the button click event there should be an updation in the database and those new values must be displayed in the page.

    Wednesday, May 9, 2012 8:28 PM
  • User-1400214080 posted

    Any answers?????

    Wednesday, May 9, 2012 9:04 PM
  • User-1400214080 posted

    The solution was to enclose the code in the page load event in if(!IPostBack) . Otherwise whenver i click the button the page would be loaded with the old values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 9, 2012 11:14 PM