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

zhshqzycAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

s___kCommented:
CREATE PROCEDURE someprocedure @id int, @text1 as varchar(max), @text2 as varchar(max)
as
begin
update MedOrganization set OrgCode=@text1, OrgName=@text2 where id=@id
if (@@rowcount=0)
begin
Insert MedOrganization(OrgCode, OrgName) values(@OrgCode,@OrgName)
end
end
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
s___kCommented:
or maybe use OrgCode as identifier
0
zhshqzycAuthor Commented:
Can it prevent the duplication recodes because of mistakenly inserting?
0
s___kCommented:
@@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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.