Link to home
Start Free TrialLog in
Avatar of zhshqzyc
zhshqzyc

asked on

Edit or Add database

Hello,
I want to insert two fields into or update database accordingly. It depends, if there are not such ones existing in the database, then insert them. If they are already there, it may be updated.

The two fields come from two text boxs.
I only finish insert part, can we put them together?
 protected void Add_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MembershipDB"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            
            cmd.CommandText = "Insert MedOrganization(OrgCode, OrgName) values(@OrgCode,@OrgName)";
            cmd.Parameters.Add("OrgCode", SqlDbType.VarChar).Value = TextBox1.Text;
            cmd.Parameters.Add("OrgName", SqlDbType.VarChar).Value = TextBox2.Text;
            cmd.ExecuteNonQuery();
            Response.Redirect("ManageUsers.aspx");
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of s___k
s___k
Flag of Georgia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or maybe use OrgCode as identifier
Avatar of zhshqzyc
zhshqzyc

ASKER

Can it prevent the duplication recodes because of mistakenly inserting?
@@rowcount shows the number of rows updated by the previous update statement, so if @@rowcount = 0 this means that no rows were updated and you can do Insert