can anyone tell me what's the wrong in the code.............when i run this........the updation is not working

string sq1;
        SqlCommand cmd1;
        SqlDataReader rd1;
        SqlConnection cn;

            cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=J:\\Rcar\\motion\\db_image.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            cn.Open();

            sq1= "SELECT * FROM Customers";  
            cmd1 = new SqlCommand(sq1,cn);
            rd1 = cmd1.ExecuteReader();

            //while (rd1.Read())
           // {
                //Console.WriteLine(String.Format("{0} {1}", dataReader["CompanyName"], dataReader["ContactName"]));
           // }
            rd1.Close();

            sq1 = "UPDATE users SET username = '" + Box.Text + "',userid='" + Box2.Text + "',password='" + Box3.Text + "',sex='" + Box4.Text + "',email='" + Box5.Text + "' WHERE userid = '" + Box1.Text + "'";
            cmd1 = new SqlCommand(sq1, cn);
            cmd1.ExecuteNonQuery();

            cn.Close();

Edited 5 Years Ago by Jessurider: n/a

here i could'nt update the username ....................

string sq1;
                SqlCommand cmd1;
                SqlDataReader rd1;
                SqlConnection cn;
                String usrname = Convert.ToString(Box.Text);
                String usrid = Convert.ToString(Box2.Text);
                String pass = Convert.ToString(Box3.Text);
                String sx = Convert.ToString(Box4.Text);
                String mail = Convert.ToString(Box5.Text);
                String usr = Convert.ToString(Box1.Text);


                cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=J:\\Rcar\\motion\\db_image.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
                // cn.Open();
                if (cn.State == ConnectionState.Closed)
                {
                    cn.Open();
                }

                sq1 = "SELECT * FROM users where username='"+usr+"'";  //string commandString =sq1
                cmd1 = new SqlCommand(sq1, cn);
                rd1 = cmd1.ExecuteReader();

                //while (rd1.Read())
                //{
                // Console.WriteLine(String.Format("{0}", rd1["username"]));
               // }
                rd1.Close();

                sq1 = "update users set username = '" + usrname + "',userid='" + usrid + "',password='" + pass + "',sex='" + sx + "',email='" + mail + "' WHERE username = '" + usr + "'";
               // sq1 = "UPDATE users SET username = '" + Box.Text + "',userid='" + Box2.Text + "',password='" + Box3.Text + "',sex='" + Box4.Text + "',email='" + Box5.Text + "' WHERE userid = '" + Box1.Text + "'";
            
                cmd1 = new SqlCommand(sq1, cn);
                cmd1.ExecuteNonQuery();
                MessageBox.Show("Updated", "User craetion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cn.Close();

Lines 5-10: Convert.ToString on string variables doesn't do anything.
Lines 20-28: These lines do nothing you can remove them.

What makes you think the user name wasn't updated?

directly use BOX.Text (all same ones from 5-10 instead of converting a string to string again) in sql command. Try this sql command instead

"UPDATE Users set username = '" + Box.Text + "',userid='" + Box2.Text + "',password='" +Box3.Text + "',sex='" + Box4.Text + "',email='" + Box5.Text+ "' WHERE username = '" + Box1.Text + "'"

... A part of these useless lines of code (5-10 and 20-28) your code seems to be alright what exactly you are looking for?.... Also remove

Connect Timeout=30

from your connection string

Edited 5 Years Ago by abelLazm: n/a

This article has been dead for over six months. Start a new discussion instead.