Change your code with the below changes. I assume that you have declare Trans variable somewhere in your code
private void btnsave_Click(object sender, EventArgs e)
{
string sq = null;
try
{
Trans = con.BeginTransaction();
sq = "Insert into unpw (username,password)values(@username,@password)";
cmd = new System.Data.SqlClient.SqlCommand(sq, con,Trans);
cmd.Parameters.AddWithValue("@username", txtun.Text);
cmd.Parameters.AddWithValue("@password", txtpwd.Text);
cmd.Transaction = Trans;
cmd.ExecuteNonQuery();
sq = "Insert into unpw1 (username,password)values(@username,@password)";
cmd = new System.Data.SqlClient.SqlCommand(sq, con,Trans);
cmd.Parameters.AddWithValue("@UserName", txtun.Text);
cmd.Parameters.AddWithValue("@password", txtpwd.Text);
cmd.Transaction = Trans;
cmd.ExecuteNonQuery();
Trans.Commit();
MessageBox.Show("Save");
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
Trans.Rollback();
MessageBox.Show(ex.Message);
}
}