Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i am newbie so dont angry on my question also i am weak in english
i have edited my query which was before an insert query and i have edited to become a uodate query but the last portion is not correct as data is updated successfull but i want to show a messagebox if updation was success then it will be suceess and if updation was failed then a message box will show which will contain message of error.
here is my crazy code
 

  SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project\Projects\labelnameinsecondformtry\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
            try
            {
                conn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Error with the databse connection");
            }
            string qry2 = "UPDATE Table1 SET Password =@Password WHERE UserName=@username";
            SqlCommand com = new SqlCommand(qry2, conn);
            com.Parameters.AddWithValue("@username", this.textBox1.Text);
            com.Parameters.AddWithValue("@Password", this.textBox2.Text);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    MessageBox.Show("updated sucesfull");
                }
            }
            if (dr.HasRows == false)
            {
                MessageBox.Show("updation failed ");
            }
Posted 15-Nov-12 0:26am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi Adil,
 
Please update your code as below.
 
SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source = .\SQLEXPRESS;AttachDbFilename=E:\project\Projects\labelnameinsecondformtry\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
       try
       {
            conn.Open();
            string qry2 = "UPDATE Table1 SET Password =@Password WHERE UserName=@username";
            SqlCommand com = new SqlCommand(qry2, conn);
            com.Parameters.AddWithValue("@username", this.textBox1.Text);
            com.Parameters.AddWithValue("@Password", this.textBox2.Text);
            int result = com.ExecuteNonQuery();
            if (result > 0)
            {
                MessageBox.Show("updated sucesfull");
            }
            else
                 MessageBox.Show("updated failed");
         }
         catch (Exception)
         {
                MessageBox.Show("Error with the databse connection");
         }
 
Hope this will help you.
  Permalink  
v2
Comments
shaikh-adil at 15-Nov-12 7:01am
   
thanks mukhthar bhai :)
shaikh-adil at 15-Nov-12 7:09am
   
but what is this if (result > 0)?? i didint get it can you explain
Mohd. Mukhtar at 15-Nov-12 7:19am
   
yes Adil, Please note that SqlCommand.ExecuteNonQuery() method returns the number of row affected by the insert, delete or update command. Hence here we have declared int result to store the no of affected rows. It will have the value >0 if command executed successfully other wise it will have negative value.
shaikh-adil at 15-Nov-12 7:27am
   
thank you mohammed bhai. for giving knowledge. thank you once again
sariqkhan at 15-Nov-12 7:29am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try the following:
 
int x = cmd.ExecuteNonQuery();
if (x == -2)
{
   //fail and show the message
}
 
please check the following for example:
 
http://stackoverflow.com/questions/2963703/how-to-know-whether-a-sql-update-statement-executed-successfully-or-failed[^]
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 448
1 Sergey Alexandrovich Kryukov 425
2 Abhinav S 408
3 sanket saxena 285
4 Tadit Dash 160
0 Sergey Alexandrovich Kryukov 8,482
1 OriginalGriff 4,830
2 Peter Leow 3,794
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid