Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;

public partial class _Default : System.Web.UI.Page
{
        SqlConnection conn = new SqlConnection("server=DIPESH-PC\\SQLEXPRESS;uid=sa;pwd=rerf;database=Employee");
        SqlConnection con1 = new SqlConnection("server=DIPESH-PC\\SQLEXPRESS;uid=sa;pwd=rerf;database=Employee");
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select * from tbl_emp", con1);
        SqlDataReader reader;
        con1.Open();
        reader = cmd.ExecuteReader();
        reader.Read();
        TextBox1.Text = reader["email"].ToString();
        TextBox2.Text = reader["password"].ToString();
        TextBox3.Text = reader["Address"].ToString();
        
        reader.Close();
        con1.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
{
    this.TextBox1.ReadOnly = true;
    this.TextBox2.ReadOnly = false;
    this.TextBox3.ReadOnly = false;

}
    protected void Button2_Click(object sender, EventArgs e)
    {

        conn.Open();
        SqlCommand cmd1= new SqlCommand("UPDATE tbl_emp SET password=@password, Address=@Address WHERE email=@email",conn);
        cmd1.Parameters.AddWithValue("@password",TextBox2.Text);
        cmd1.Parameters.AddWithValue("@Address", TextBox3.Text);
        cmd1.Parameters.AddWithValue("@email", TextBox1.Text);
        cmd1.ExecuteNonQuery();
        conn.Close();            
        Label1.Text = "  Data Updates Succesfully on  " + System.DateTime.Now.ToShortDateString();
             
    }
   
}


Label1 message displayed ' Data Updates Succesfully on................' but database can't be updated. plz help me
Posted
Updated 2-May-14 20:15pm
v3
Comments
PIEBALDconsult 2-May-14 20:55pm    
Check the result of the ExecuteNonQuery.
[no name] 2-May-14 23:15pm    
How can you possibly know if the update is successful or not when you don't check? And you probably need to enclose "password" in brackets, [password]

And why on your Page_Load do you select everything from your table when you only use one single record? Doesn't that seem a bit wasteful to you?
dipesh_karmakar 4-May-14 15:30pm    
conn.Open();
SqlCommand cmd1 = new SqlCommand("UPDATE tbl_emp SET [password]=@password,[Address]=@Address WHERE[email]=@email", conn);
cmd1.Parameters.AddWithValue("@password", TextBox2.Text);
cmd1.Parameters.AddWithValue("@Address", TextBox3.Text);
cmd1.Parameters.AddWithValue("@email", TextBox1.Text);
cmd1.ExecuteNonQuery();
conn.Close();
Label1.Text = " Data Updates Succesfully on " + System.DateTime.Now.ToShortDateString();
dipesh_karmakar 4-May-14 15:32pm    
but still i got error. database can't updated
King Fisher 3-May-14 0:11am    
catch the Exception.

1 solution

Hey,

Id'd put a try catch around the whole statement in Button2_Click and see if any exceptions were thrown.

Also its good coding practice to wrap any classes which implement IDisposable in using statements so your method would become:

using (conn)
{
    conn.Open();

    using (SqlTransaction objTrans = conn.BeginTransaction())
    {
        try
        {
            using (SqlCommand cmd1 = new SqlCommand("UPDATE tbl_emp SET password=@password, Address=@Address WHERE email=@email", conn))
            {
                cmd1.Parameters.AddWithValue("@password", "");
                cmd1.Parameters.AddWithValue("@Address", "");
                cmd1.Parameters.AddWithValue("@email", "");
                cmd1.ExecuteNonQuery();

                objTrans.Commit();
            }
        }
        catch (SqlException ex)
        {
            //Display error here
            objTrans.Rollback();
        }

        Label1.Text " Data Updates Succesfully on " + System.DateTime.Now.ToShortDateString();

    }

}



Also I would wrap the update in a transaction just to be safe, apologies if there are some syntax mistakes as I wrote it free hand but you get the idea.
 
Share this answer
 
v2

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



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