Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi,
in Page_Load Event some information store in textboxes.in fact he see his profile information for editing.when i change some textboxes information for example i change jack to alex and click save changes button jack were save in DataBase.i do not know what is wrong.

C#
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;
using System.Data;
using System.Drawing;
 
namespace Sanjesh_begin_8_farvardin
{
    public partial class EditProfile : System.Web.UI.Page
    {
        public string constring = "server=localhost;database=SanjeshOrganizationDB;Trusted_Connection=true";
        protected void Page_Load(object sender, EventArgs e)
        {
            
            SqlConnection con0 = new SqlConnection(constring);
            con0.Open();
            SqlCommand com0 = new SqlCommand("select * from Profile_Information where Username='"+Session["Username"].ToString()+"'",con0);
            SqlDataReader rdr = com0.ExecuteReader();
            rdr.Read();
            TextBox1.Text = rdr["Username"].ToString();
            TextBox2.Text = rdr["FName"].ToString();
            TextBox3.Text = rdr["LName"].ToString();
            DropDownList1.SelectedItem.Value = "1391";
            DropDownList2.SelectedItem.Value = "01";
            DropDownList3.SelectedItem.Value = "01";
            TextBox4.Text = rdr["LiveLocation"].ToString();
            TextBox5.Text = rdr["Email_Address"].ToString();
            TextBox6.Text = rdr["Reason_For_Registering"].ToString();
            TextBox7.Text = rdr["Liking_Field"].ToString();
            TextBox8.Text = rdr["Optional_Comment"].ToString();
            con0.Close();
        }
 
        protected void Button1_Click(object sender, EventArgs e)//Save changes
        {   
            SqlConnection con0 = new SqlConnection(constring);
            con0.Open();
            SqlCommand com0 = new SqlCommand("Update Profile_Information Set FName='" + TextBox2.Text + "',LName='" + TextBox3.Text + "',BirthDate='"+DropDownList1.SelectedItem+"-"+DropDownList2.SelectedItem+"-"+DropDownList3.SelectedItem+"',LiveLocation='"+TextBox4.Text+"',Email_Address='"+TextBox5.Text+"',Reason_For_Registering='"+TextBox6.Text+"',Liking_Field='"+TextBox7.Text+"',Optional_Comment='"+TextBox8.Text+"' where Username='" + TextBox1.Text + "'", con0);
            try
            {
                com0.ExecuteNonQuery();
                Label12.Visible = true;
                Label12.Text = "Successfull";
                Label12.ForeColor = Color.Green;
            }
            catch
            {
                Label12.Visible = true;
                Label12.Text = "Error";
                Label12.ForeColor = Color.Red;
            }
        }
    }
}
Posted
Comments
kishore sharma 3-Apr-13 3:34am    
Are you using update panel? if yes does your save button is also in the same update panel?
_Starbug_ 3-Apr-13 3:38am    
no i have not any update panel.in one page i have few textbox and a save button
kishore sharma 3-Apr-13 4:43am    
look into my solution Hope help you.
_Starbug_ 3-Apr-13 4:54am    
thank you my friend.solved.
raeeschaudhary 3-Apr-13 3:52am    
perhaps closing connection in button solves....... try that, all else looks fine

please look in to your own code ,i think you missed
if (!IsPostBack){}
C#
if (!IsPostBack)
           {
//put you code to bind the control values  
SqlConnection con0 = new SqlConnection(constring);
            con0.Open();
            SqlCommand com0 = new SqlCommand("select * from Profile_Information where Username='"+Session["Username"].ToString()+"'",con0);
            SqlDataReader rdr = com0.ExecuteReader();
            rdr.Read();
            TextBox1.Text = rdr["Username"].ToString();
            TextBox2.Text = rdr["FName"].ToString();
            TextBox3.Text = rdr["LName"].ToString();
            DropDownList1.SelectedItem.Value = "1391";
            DropDownList2.SelectedItem.Value = "01";
            DropDownList3.SelectedItem.Value = "01";
            TextBox4.Text = rdr["LiveLocation"].ToString();
            TextBox5.Text = rdr["Email_Address"].ToString();
            TextBox6.Text = rdr["Reason_For_Registering"].ToString();
            TextBox7.Text = rdr["Liking_Field"].ToString();
            TextBox8.Text = rdr["Optional_Comment"].ToString();
            con0.Close();
}


Happy coding
 
Share this answer
 
Comments
[no name] 3-Apr-13 4:46am    
yes....in my opinion it should work....
perhaps closing connection in button solves....... try that, all else looks fine
 
Share this answer
 
Comments
_Starbug_ 3-Apr-13 3:57am    
No my friend,that code just close current connection and do not do any thing.
_Starbug_ 3-Apr-13 3:59am    
i do not know why browser do not accept new values?
raeeschaudhary 3-Apr-13 4:24am    
then try using parameterized query. if all is good then why it isnt working. try step by step
Use

if (!IsPostBack)
{
}
in page load
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900